# vim: set ts=2 sw=2 et: # # Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 # AWSTemplateFormatVersion: 2010-09-09 Description: Setup AWS IoT + SQS w/ multiple consumers Parameters: TopicName: Type: String Resources: SQSQueue: Type: AWS::SQS::Queue Properties: QueueName: !Sub "${ TopicName }Queue" SQSQueueLambdaRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Principal: Service: - "lambda.amazonaws.com" Action: "sts:AssumeRole" Policies: - PolicyName: !Sub "${ TopicName }RecipientPolicy" PolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Action: - "sqs:DeleteMessage" - "sqs:ChangeMessageVisibility" - "sqs:ReceiveMessage" - "sqs:GetQueueAttributes" Resource: !GetAtt SQSQueue.Arn - Effect: Allow Action: - "logs:*" Resource: "*" IoTSQSPublisherRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Principal: Service: - "iot.amazonaws.com" Action: "sts:AssumeRole" Policies: - PolicyName: !Sub "${ TopicName }PublisherPolicy" PolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Action: - "sqs:SendMessage" Resource: !GetAtt SQSQueue.Arn IoTSQSPublishRule: Type: AWS::IoT::TopicRule Properties: RuleName: !Sub "${ TopicName }SQSPublisher" TopicRulePayload: AwsIotSqlVersion: "2016-03-23" RuleDisabled: false Sql: !Sub "SELECT * FROM '${ TopicName }'" Actions: - Sqs: QueueUrl: !Ref SQSQueue RoleArn: !GetAtt IoTSQSPublisherRole.Arn UseBase64: false SQSProcessLambda: Type: AWS::Lambda::Function Properties: Code: ZipFile: | 'use strict' const aws = require('aws-sdk'); exports.handler = function(event,context) { console.log(JSON.stringify(event)); } Role: !GetAtt SQSQueueLambdaRole.Arn MemorySize: 256 Handler: index.handler Runtime: nodejs6.10 Timeout: 30 Environment: Variables: TOPIC: !Sub "{ TopicName }" SQSLambdaEventSource: Type: AWS::Lambda::EventSourceMapping Properties: BatchSize: 1 Enabled: true EventSourceArn: !GetAtt SQSQueue.Arn FunctionName: !Ref SQSProcessLambda Outputs: QueueName: Value: !GetAtt SQSQueue.QueueName QueueURL: Value: !Ref SQSQueue QueueARN: Value: !GetAtt SQSQueue.Arn