Skip to content

Commit cd10937

Browse files
author
Bret Ambrose
committed
V2 shadow sample and usage guide
1 parent defefac commit cd10937

File tree

9 files changed

+882
-588
lines changed

9 files changed

+882
-588
lines changed

codebuild/samples/shadow-linux.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pushd $CODEBUILD_SRC_DIR/samples/
1010
ENDPOINT=$(aws secretsmanager get-secret-value --secret-id "ci/endpoint" --query "SecretString" | cut -f2 -d":" | sed -e 's/[\\\"\}]//g')
1111

1212
echo "Shadow test"
13-
python3 shadow.py --endpoint $ENDPOINT --key /tmp/privatekey.pem --cert /tmp/certificate.pem --thing_name CI_CodeBuild_Thing --is_ci true
14-
python3 shadow_mqtt5.py --endpoint $ENDPOINT --key /tmp/privatekey.pem --cert /tmp/certificate.pem --thing_name CI_CodeBuild_Thing --is_ci true
13+
python3 deprecated/shadow.py --endpoint $ENDPOINT --key /tmp/privatekey.pem --cert /tmp/certificate.pem --thing_name CI_CodeBuild_Thing --is_ci true
14+
python3 deprecated/shadow_mqtt5.py --endpoint $ENDPOINT --key /tmp/privatekey.pem --cert /tmp/certificate.pem --thing_name CI_CodeBuild_Thing --is_ci true
1515

1616
popd

samples/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
* [MQTT5 Shared Subscription](./mqtt5_shared_subscription.md)
1010
* [MQTT5 PKCS#11 Connect](./mqtt5_pkcs11_connect.md)
1111
* [MQTT5 Custom Authorizer Connect](./mqtt5_custom_authorizer_connect.md)
12-
* [MQTT5 Shadow](./shadow_mqtt5.md)
1312
* [MQTT5 Jobs](./jobs_mqtt5.md)
1413
* [MQTT5 Fleet Provisioning](./fleetprovisioning_mqtt5.md)
1514
## MQTT311 Samples
@@ -22,10 +21,10 @@
2221
* [Custom Authorizer Connect](./custom_authorizer_connect.md)
2322
* [Cognito Connect](./cognito_connect.md)
2423
* [X509 Connect](./x509_connect.md)
25-
* [Shadow](./shadow.md)
2624
* [Jobs](./jobs.md)
2725
* [Fleet Provisioning](./fleetprovisioning.md)
2826
## Other
27+
* [Shadow](./shadow.md)
2928
* [Greengrass Discovery](./basic_discovery.md)
3029
* [Greengrass IPC](./ipc_greengrass.md)
3130

samples/deprecated/shadow.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Shadow
2+
3+
[**Return to main sample list**](./README.md)
4+
5+
This sample uses the AWS IoT [Device Shadow](https://docs.aws.amazon.com/iot/latest/developerguide/iot-device-shadows.html) Service to keep a property in sync between device and server. Imagine a light whose color may be changed through an app, or set by a local user.
6+
7+
Once connected, type a value in the terminal and press Enter to update the property's "reported" value. The sample also responds when the "desired" value changes on the server. To observe this, edit the Shadow document in the AWS Console and set a new "desired" value.
8+
9+
On startup, the sample requests the shadow document to learn the property's initial state. The sample also subscribes to "delta" events from the server, which are sent when a property's "desired" value differs from its "reported" value. When the sample learns of a new desired value, that value is changed on the device and an update is sent to the server with the new "reported" value.
10+
11+
Your IoT Core Thing's [Policy](https://docs.aws.amazon.com/iot/latest/developerguide/iot-policies.html) must provide privileges for this sample to connect, subscribe, publish, and receive. Below is a sample policy that can be used on your IoT Core Thing that will allow this sample to run as intended.
12+
13+
<details>
14+
<summary>Sample Policy</summary>
15+
<pre>
16+
{
17+
"Version": "2012-10-17",
18+
"Statement": [
19+
{
20+
"Effect": "Allow",
21+
"Action": [
22+
"iot:Publish"
23+
],
24+
"Resource": [
25+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/get",
26+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/update"
27+
]
28+
},
29+
{
30+
"Effect": "Allow",
31+
"Action": [
32+
"iot:Receive"
33+
],
34+
"Resource": [
35+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/get/accepted",
36+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/get/rejected",
37+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/update/accepted",
38+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/update/rejected",
39+
"arn:aws:iot:<b>region</b>:<b>account</b>:topic/$aws/things/<b>thingname</b>/shadow/update/delta"
40+
]
41+
},
42+
{
43+
"Effect": "Allow",
44+
"Action": [
45+
"iot:Subscribe"
46+
],
47+
"Resource": [
48+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/$aws/things/<b>thingname</b>/shadow/get/accepted",
49+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/$aws/things/<b>thingname</b>/shadow/get/rejected",
50+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/$aws/things/<b>thingname</b>/shadow/update/accepted",
51+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/$aws/things/<b>thingname</b>/shadow/update/rejected",
52+
"arn:aws:iot:<b>region</b>:<b>account</b>:topicfilter/$aws/things/<b>thingname</b>/shadow/update/delta"
53+
]
54+
},
55+
{
56+
"Effect": "Allow",
57+
"Action": "iot:Connect",
58+
"Resource": "arn:aws:iot:<b>region</b>:<b>account</b>:client/test-*"
59+
}
60+
]
61+
}
62+
</pre>
63+
64+
Replace with the following with the data from your AWS account:
65+
* `<region>`: The AWS IoT Core region where you created your AWS IoT Core thing you wish to use with this sample. For example `us-east-1`.
66+
* `<account>`: Your AWS IoT Core account ID. This is the set of numbers in the top right next to your AWS account name when using the AWS IoT Core website.
67+
* `<thingname>`: The name of your AWS IoT Core thing you want the device connection to be associated with
68+
69+
Note that in a real application, you may want to avoid the use of wildcards in your ClientID or use them selectively. Please follow best practices when working with AWS on production applications using the SDK. Also, for the purposes of this sample, please make sure your policy allows a client ID of `test-*` to connect or use `--client_id <client ID here>` to send the client ID your policy supports.
70+
71+
</details>
72+
73+
## How to run
74+
75+
To run the Shadow sample from the `samples` folder, use the following command:
76+
77+
``` sh
78+
# For Windows: replace 'python3' with 'python' and '/' with '\'
79+
python3 shadow.py --endpoint <endpoint> --cert <file> --key <file> --thing_name <name>
80+
```
81+
82+
You can also pass a Certificate Authority file (CA) if your certificate and key combination requires it:
83+
84+
``` sh
85+
# For Windows: replace 'python3' with 'python' and '/' with '\'
86+
python3 shadow.py --endpoint <endpoint> --cert <file> --key <file> --thing_name <name> --ca_file <file>
87+
```

0 commit comments

Comments
 (0)