Industrial IoT/ML Workshop

Inference at the Edge

Include the Inference Lambda function in the AWS Greengrass Group

We will now add the ML Model as well as a Lambda function which will do the prediction based on that model. To do this, please return to the AWS Greengrass tab open in the browser, open the Lambdas menu and click “Add Lambda”:

  • Add the Lambda function with the name “PredictAnomalies” to the Greengrass group. 
  • Use the “latest version” alias.
  • Ensure to configure enough memory (>200MB), otherwise the function will not start
  • This lambda will run on demand and therefore the timeout needs to be set to a proper value. In this case, 10 seconds should be sufficient. We will configure the subscriptions in such a way that any output coming from the CombineEvents Lambda is passed to the prediction Lambda. The model we generated above will then make a prediction and indicate whether the drilling process was normal or not.

To actually use the model, we need to define it as a resource the lambda may access. This is done as follows:

  • Go to “Resources”
  • Click “Add Resource”

Click “Add machine learning resource” to connect the AWS Greengrass group with our learned model.

Since our model was stored in S3, we have to locate it and connect the file containing our model with the Greengrass resource:

  • Provide a name for the resource
  • Select “Locate or upload a model in S3
  • Below “Model from S3” select the bucket that starts with “iiotws-iiotwsgreengrass”
  • Select the model “folder”
  • This folder should have a file called “DrillingPrediction.model.tar.gz” (this file was created by the last step of the Jupyter Notebook)
  • Select this file
  • As “Local path” put in “/trained_model”
  • Since our lambda only reads this model file, it is sufficient to have “read-only” access to this folder.

Funnel the aggregated events to the prediction function

The prediction lambda needs to be triggered whenever a new output by the “CombineEvents” lambda is generated. Hence, we need to configure the following subscriptions:

  • The PredictAnomalies Lambda has to subscribe to the the CombineEvents lambda. CombineEvents publishes the relevant messages to data/aggregated


  • IoT Core needs to be subscribed to the output of the PredictAnomalies

The final subscription configuration of the AWS Greengrass group has to be similar to the following:

Finally, the new AWS Greengrass group has to be deployed.

On the AWS IoT Core Test Console you should see data from the prediction lambda. If you open the code of the prediction lambda in Cloud9, you will see that it will make the prediction by loading the model and the publishing the result along with the sensor data to MQTT topic data/prediction.

Subscribe to the topic “” in order to be able to filter just for those events.

You should see the prediction result within the Prediction attribute.

With the prediction result you could just signal this to the worker or even control the drill → this is up to you. In the example above, the ML model has detected a potential anomaly. If the drilling process has been completed normally, the model would indicate this by returning “OK” instead of “POSSIBLE_FAILURE”.