Search code examples
pythonazure-iot-hubazure-iot-edge

Azure IoT Edge API module cannot be accessed . Request timed out


I'm implementing API on azure IoT edge runtime installed on a Ubuntu hyper-v VM. I have a flask API module implemented and up and running in the edge runtime on Ubuntu vm. So i have a requirement to access this API from the host machine which is my local machine running on windows 10. IP address the API module running on is http://172.20.36.197:5000/predict.

this works in the guest(Ubuntu hyper-v VM) machine but request times out every time i try to invoke this call in host machine. I have disabled even the firewalls of host VM. But no luck. seems I have to access the IoT edge runtime running on Ubuntu VM from my host machine.

I just ran the same flask API in a docker container not within the IoT edge runtime as an edge module but within docker inside Ubuntu Hyper-v VM and i can access that API with from the host without any issue. Following is my code for flask API edge module running on IoT edge runtime

import os
from azure.iot.device.aio import IoTHubModuleClient
from flask import Flask

port =int(os.environ.get("port",5000))
print("port is ",port)

# Initialize flask app
app = Flask(__name__)

# Create an endpoint
@app.route('/predict')
def predict_chiller_condition():
    print("api func called")
    predictedVal=3
    return 'predicted value is '+str(predictedVal)

if __name__ == "__main__":
    app.run(host='0.0.0.0',port=port)

Solution

  • For anyone who is struggling to solve such a scenario, you should expose your edge module container running on a guest VM by adding ExposedPorts in createOptions section in deployment.template.json as following. And also IPv4 address of the VM should be used with the relevant port.

            "modules": {
            "test_flask_api": {
            "version": "1.0",
            "type": "docker",
            "status": "running",
            "restartPolicy": "always",
            "settings": {
              "image": "${MODULES.test_flask_api}",
              "createOptions": {
                "ExposedPorts": {
                  "5000/tcp": {}
                },
                "HostConfig": {
                  "PortBindings": {
                    "5000/tcp": [
                      {
                        "HostPort": "5000"
                      }
                    ]
                  }
                }
              }
            }
          }
        }