Hi @Roshan Thomas(UST,IN) Have you tried to access the App service Rest API end point from within the Python code of your module using HTTP request? I have set up a Python Edge module and accessed the Rest API end point hosted in Azure App. I have set up the App Service Rest API using the tutorial Host a RESTful API with CORS in Azure App Service. I do not have to open any ports explicitly on the IoT Edge module container.
Here is my docker file
FROM amd64/python:3.7-slim-buster
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
CMD [ "python3", "-u", "./main.py" ]
I have left the create options as default. Please find the below snippet showing the create options section from deployment.template.json
file
"createOptions": {
"HostConfig": {
"PortBindings": {
"5671/tcp": [
{
"HostPort": "5671"
}
],
"8883/tcp": [
{
"HostPort": "8883"
}
],
"443/tcp": [
{
"HostPort": "443"
}
]
}
}
}
Within the main method of the main.py, I have called the Rest API endpoint using requests and could access the data. Please find the below snippet to find the code I have tested
api_url = "http://<app-name>.azurewebsites.net/api/todo"
# Set the request headers
headers = {"Content-Type": "application/json"}
# Set the request body (if applicable)
body = {"key": "value"}
response = requests.get(api_url, headers=headers, json=body)
if response.status_code == 200:
# Read the response content as a string
response_json = response.json()
for item in response_json:
print(item["id"], item["name"], item["isComplete"])
else:
# Handle the error response in your Python script
print(f"Error calling API: {response.status_code}")
I have created a IoT Hub device client within the app and could route the data I have received from the API to an Azure storage account. I appreciate it if you can give the above method a try and let me know if that helps.
If your App service Rest API uses any Authorization or if it is behind a proxy server, test the API from the Postman and make sure you include the correct end point and the authorization headers to the request.
Can you test if you can ping the Azure App service end point from the VM where your IoT Edge modules are deployed? You can run the following command ping <app-name>.scm.azurewebsites.net
from the bash of the VM and see if you can reach the App service end point. If you cannot ping the IP, enable the ICMP protocol on the VM and see if that helps.
If you still see the same error in the module, place time.sleep(0.01)
in the code before right before making the Http request. Please refer the following sample for reference
AI ConvertCopy
time.sleep(0.01)
response = requests.get(api_url, headers=headers, json=body)
Let us know if you run into any issues or need any additional assistance in the comments below.