Skip to content

Block 2 — Deploy Shadow Update Job

Duration: 60 min


What the Job Does

  1. Adds app-deployment shadow reporting — reads current Docker Compose version from file, publishes to shadow
  2. Adds device-health shadow reporting — a periodic heartbeat every 30 seconds with system metrics

Steps

  1. 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
  1. Create the IoT Job in the console:

    • Job type: Create custom job
    • Thing group: ws-slot00-devices
    • Job document: from S3 URL:

      s3://workshop-platform-000000000000/ws-slot00/job-docs/add-shadows-job-doc.json
      
    • 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 json
    

    Monitor per-device status at IoT Core → Manage → Jobs, or poll with the CLI:

    for _i in $(seq 1 30); do
      RESULT=$(aws iot list-job-executions-for-job \
        --job-id "$JOB_ID" \
        --query 'executionSummaries[].{thing:thingArn,status:jobExecutionSummary.status}' \
        --output table)
      echo "$RESULT" | grep -q "SUCCEEDED" && break
      sleep 10
    done
    echo "$RESULT"
    

  2. The job script installs two systemd timer units that fire every 30 seconds:

  3. report-app-deployment.timer — reports compose version and deploy status
  4. report-device-health.timer — reports CPU/mem/disk %, container count, uptime, last_heartbeat

  5. Observe the new shadows appearing in IoT Core → Manage → Things → (device) → Shadows

  6. 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-deployment shadow to detect that a device hasn't received the latest update?

Reference