This code sample demonstrates how a user can create/handle custom AWS IoT jobs in Python.
This sample code is made available under the MIT-0 license. See the LICENSE file.
Cloud9 IDE
- 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 Lambda zip package
- Download CloudFormation template <-- (right click, and save as 'aws-iot-jobs-python.yaml')
- 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.
- 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!
Manual
See the AWS Region Table for the current list of regions for AWS IoT Core and AWS IoT Device Management.
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
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
Based on jobsSample.py from aws-iot-device-sdk-python. Modified to include jobExecutor, which handles the execution of specific job documents.
jobExecutor.py
Module referenced by jobsSample.py to handle specific job documents. Can be modified to handle your custom 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
You can use the JSON job documents to schedule a new job execution. You can find more info on each job type here:
Job Targets
For the --targets parameter, you can use:
- An IoT Thing Arn
- An IoT Things Group Arn
- A local JSON file targeting IoT Thing(s), targeting Things Group Arn(s), or targeting a combination of both!
aws iot create-job --targets {THING_OR_THINGS_GROUP_ARN} --document file://jobs/{JSON_JOB_DOCUMENT} --job-id $(uuidgen)
aws iot create-job --targets file://etc/target-thing.json --document file://jobs/{JSON_JOB_DOCUMENT} --job-id $(uuidgen)
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