Skip to content

Instantly share code, notes, and snippets.

@johncmckim
Last active November 29, 2020 19:54
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johncmckim/5d149fb2416f38957c2d0e30f56c6aba to your computer and use it in GitHub Desktop.
Save johncmckim/5d149fb2416f38957c2d0e30f56c6aba to your computer and use it in GitHub Desktop.
Garden Aid - IoT Hub - IoT Resources
SensorThing:
Type: AWS::IoT::Thing
Properties:
AttributePayload:
Attributes:
SensorType: soil
SensorThingPolicy:
Type: AWS::IoT::Policy
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: ["iot:Connect"]
Resource: ["${{custom.sensorThingClientResource}}"]
- Effect: "Allow"
Action: ["iot:Publish"]
Resource: ["${{custom.sensorThingSoilTopicResource}}"]
SensorPolicyPrincipalAttachmentCert:
Type: AWS::IoT::PolicyPrincipalAttachment
Properties:
PolicyName: { Ref: SensorThingPolicy }
Principal: ${{custom.iotCertificateArn}}
SensorThingPrincipalAttachmentCert:
Type: "AWS::IoT::ThingPrincipalAttachment"
Properties:
ThingName: { Ref: SensorThing }
Principal: ${{custom.iotCertificateArn}}
IoTRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: Allow
Principal:
Service:
- iot.amazonaws.com
Action:
- sts:AssumeRole
IoTRolePolicies:
Type: AWS::IAM::Policy
Properties:
PolicyName: IoTRole_Policy
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: Allow
Action:
- dynamodb:PutItem
Resource: "*"
-
Effect: Allow
Action:
- lambda:InvokeFunction
Resource: "*"
Roles: [{ Ref: IoTRole }]
# AWS IoT SQL Reference
# http://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-functions.html
SensorThingRule:
Type: AWS::IoT::TopicRule
Properties:
TopicRulePayload:
RuleDisabled: false
Sql: "SELECT Level FROM '${{opt:stage}}/garden/soil/moisture'"
Actions:
-
DynamoDB:
TableName: { Ref: MoistureData }
HashKeyField: "ClientId"
HashKeyValue: "${clientId()}"
RangeKeyField: "Timestamp"
RangeKeyValue: "${timestamp()}"
PayloadField: "Data"
RoleArn: { Fn::GetAtt: [ IoTRole, Arn ] }
-
Lambda:
FunctionArn: { Fn::GetAtt: [ checkMoistureLevel, Arn ] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment