Block 2 — Deploy Shadow Update Job¶
Duration: 60 min
What the Job Does¶
- Adds
app-deploymentshadow reporting — reads current Docker Compose version from file, publishes to shadow - Adds
device-healthshadow reporting — a periodic heartbeat every 30 seconds with system metrics
Steps¶
- Upload the job script to S3:
aws s3 cp job-scripts/add-shadows.sh \
s3://workshop-platform-000000000000/job-scripts/ws-slot00/add-shadows.sh
-
Create the IoT Job in the console:
- Job type: Create custom job
- Thing group:
ws-slot00-devices -
Job document: from S3 URL:
-
In-progress timeout: 15 minutes
AWS CLI equivalent
cat > /tmp/add-shadows-job-doc.json << 'EOF' { "version": "1.0", "steps": [ { "action": { "name": "add-shadows", "type": "runHandler", "input": { "handler": "run-script.sh", "args": ["s3://workshop-platform-000000000000/job-scripts/ws-slot00/add-shadows.sh"] }, "runAsUser": "" } } ] } EOF aws s3 cp /tmp/add-shadows-job-doc.json \ s3://workshop-platform-000000000000/ws-slot00/job-docs/add-shadows-job-doc.json JOB_ID="ws-slot00-add-shadows-$(date +%s)" aws iot create-job \ --job-id "$JOB_ID" \ --targets "$(aws iot describe-thing-group \ --thing-group-name ws-slot00-devices \ --query thingGroupArn --output text)" \ --document-source \ s3://workshop-platform-000000000000/ws-slot00/job-docs/add-shadows-job-doc.json \ --timeout-config '{"inProgressTimeoutInMinutes":15}' \ --output jsonMonitor per-device status at IoT Core → Manage → Jobs, or poll with the CLI:
-
The job script installs two
systemdtimer units that fire every 30 seconds: report-app-deployment.timer— reports compose version and deploy status-
report-device-health.timer— reports CPU/mem/disk %, container count, uptime,last_heartbeat -
Observe the new shadows appearing in IoT Core → Manage → Things → (device) → Shadows
-
Use Fleet Indexing to query device health across the fleet via Advanced thing search:
AWS CLI equivalent
for _i in $(seq 1 12); do RESULT=$(aws iot search-index --index-name AWS_Things \ --query-string 'attributes.deploymentId:ws-slot00 AND shadow.name.device-health.reported.cpu_pct:[0 TO *]' \ --output json | jq '{"totalResults": (.things | length)}') echo "$RESULT" echo "$RESULT" | jq -e '.totalResults > 0' > /dev/null 2>&1 && break echo "Waiting for fleet index to update... (attempt $_i/12)" sleep 15 done
Discussion¶
- What happens if a device loses connectivity for 5 minutes and then reconnects? Does the shadow reflect the last reported state or the current state?
- How would you use the
app-deploymentshadow to detect that a device hasn't received the latest update?