Skip to content

approbotic/approbotic-aws-iot-python

 
 

Repository files navigation

aws-iot-jobs-python

This code sample demonstrates how a user can create/handle custom AWS IoT jobs in Python.

License Summary

This sample code is made available under the MIT-0 license. See the LICENSE file.

Setup

Cloud9 IDE

Compatible regions

  • ap-northeast-1 (Tokyo)
  • ap-southeast-1 (Singapore)
  • eu-central-1 (Frankfurt)
  • eu-west-1 (Ireland)
  • us-east-1 (N Virginia)
  • us-east-2 (Ohio)
  • us-west-2 (Oregon)

Download zip and template

Upload c9_bootstrap_lambda.zip to S3

  • Upload c9_bootstrap_lambda.zip to a bucket of your choosing (must be in same region as Cloud9 deployment)
  • Remember the S3 bucket name and S3 key, you will use this in the next step.

Configure and launch stack

  • Upload the aws-iot-jobs-python.yaml template to CloudFormation.

  • Supply the S3 bucket and S3 key parameters using values from the previous step, as well as the preferred instance type.

  • Step through remaining configuration, acknowledge creation of IAM resources, and launch stack!

Launch Cloud9 IDE

Click the Cloud9 IDE link: Stack Output

Manual

Compatible regions

See the AWS Region Table for the current list of regions for AWS IoT Core and AWS IoT Device Management.

Required tools

  • Linux/macOS (this solution has not been tested on Windows)
  • Python 3.6 or newer
  • awscli
  • jq

Setup environment

NOTE: Make sure you execute this from the aws-iot-jobs-python directory.

export REGION=<enter your region>
export S3_BUCKET=<enter your bucket name>
./bin/create.sh

Start job agent

In one terminal tab/window, run the following command:

cd aws-iot-jobs-python
python3 jobsSample.py -j ./config.json

config.json
{
    "thingName": "<THING-NAME>",
    "thingArn": "<THING-ARN>",
    "region": "<REGION>",
    "deviceCertificatePath": "",
    "privateKeyPath": "",
    "rootCaPath": "",
    "endpoint": "<ENDPOINT>",
    "credentialsEndpoint": "<CREDENTIAL-ENDPOINT-PREFIX>",
    "roleAlias": "<ROLE-ALIAS>",
    "useWebsocket": "false",
    "port": 8883,
    "s3Bucket": "<BUCKET-NAME>
}
Key Description
thingName provides identifier for thing; used as MQTT client ID
thingArn Amazon Resource Name for thing
region AWS region thing resides in
deviceCertificatePath Path of device X.509 certificate
privateKeyPath Path of device private key
rootCaPath Path of Amazon CA certificate
endpoint MQTT broker endpoint (in AWS IoT Core)
credentialsEndpoint credentials endpoint (in AWS IoT Core) used to retrieve temporary credentials
roleAlias used to retrieve temporary credentials with credentials endpoint
useWebsocket determines if WS should be used
port MQTT port
s3Bucket used for uploading files
jobsSample.py

About

Based on jobsSample.py from aws-iot-device-sdk-python. Modified to include jobExecutor, which handles the execution of specific job documents.

jobExecutor.py

About

Module referenced by jobsSample.py to handle specific job documents. Can be modified to handle your custom jobs!

Create jobs

In a separate terminal tab/window, run the following command:

cd aws-iot-jobs-python
JOB_ID=$(uuidgen)
aws iot create-job --job-id $JOB_ID --targets $(cat config.json | jq -r '.thingArn') --document file://jobs/pip-list.json

Documentation

Example job definitions

You can use the JSON job documents to schedule a new job execution. You can find more info on each job type here:

Basic

Advanced

Job Targets

Summary

For the --targets parameter, you can use:

Targeting a IoT Thing/Things Group Arn inline

aws iot create-job --targets {THING_OR_THINGS_GROUP_ARN} --document file://jobs/{JSON_JOB_DOCUMENT} --job-id $(uuidgen)

Targeting with a JSON file

aws iot create-job --targets file://etc/target-thing.json --document file://jobs/{JSON_JOB_DOCUMENT} --job-id $(uuidgen)

Evaluate job status/execution

Execute the following to retrieve status details for your job:

aws iot describe-job --job-id $JOB_ID

Execute the following to retrieve specific job execution details for your thing:

aws iot describe-job-execution --job-id $JOB_ID --thing-name $(cat config.json | jq -r '.thingName')

After executing the pip-list job, notice that the device's shadow has been updated in AWS Management Console:

Or via CLI:

aws iot-data get-thing-shadow --thing-name $(cat config.json | jq -r '.thingName') shadow.txt
cat shadow.txt

Clean up

Cloud9 IDE

Go to CloudFormation, and delete the 'aws-iot-jobs-python' stack.

Manual

Delete environment

NOTE: Make sure you execute this from the aws-iot-jobs-python directory. This script depends on values in config.json, which are populated from the create.sh script.

export REGION=<enter your region>
./bin/delete.sh

About

Sample Python scripts and JSON documents that demonstrate how to create and execute jobs in AWS IoT

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 80.0%
  • Shell 20.0%