AWSTemplateFormatVersion: '2010-09-09' Transform: 'AWS::Serverless-2016-10-31' Resources: DynamoDBToSQSStateMachine: Type: AWS::Serverless::StateMachine Properties: DefinitionSubstitutions: ddbTableName: !Ref DDBTable sqsQueueUrl: !GetAtt SQSQueue.Arn seedingFunctionArn: !GetAtt SeedingFunction.Arn DefinitionUri: statemachine/statemachine.asl.json Role: !GetAtt DynamoDBToSQSExecutionRole.Arn Metadata: SamResourceId: DynamoDBToSQSStateMachine DynamoDBToSQSExecutionRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: states.amazonaws.com Action: sts:AssumeRole Path: / Policies: - PolicyName: DynamoDBAndSQSAccessPolicy PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: - dynamodb:GetItem Resource: - Fn::GetAtt: - DDBTable - Arn - Effect: Allow Action: - sqs:SendMessage Resource: - Fn::GetAtt: - SQSQueue - Arn - Effect: Allow Action: - lambda:invokeFunction Resource: - Fn::GetAtt: - SeedingFunction - Arn Metadata: SamResourceId: DynamoDBToSQSExecutionRole DDBTable: Type: AWS::DynamoDB::Table Properties: ProvisionedThroughput: ReadCapacityUnits: '10' WriteCapacityUnits: '10' AttributeDefinitions: - AttributeName: MessageId AttributeType: S KeySchema: - AttributeName: MessageId KeyType: HASH Metadata: SamResourceId: DDBTable SQSQueue: Type: AWS::SQS::Queue Properties: SqsManagedSseEnabled: true Metadata: SamResourceId: SQSQueue SeedingFunction: Type: AWS::Lambda::Function Properties: Handler: index.lambda_handler Role: Fn::GetAtt: - SeedingFunctionExecutionRole - Arn Code: ZipFile: Fn::Sub: - "console.log('Loading function');\nconst AWS = require('aws-sdk');\n\n\ exports.lambda_handler = (event, context, callback) => {\n var docClient\ \ = new AWS.DynamoDB.DocumentClient();\n var messageIds = []\n\n \ \ for (var i = 0; i < 10; i++) {\n console.log(\"Adding a new item...\"\ );\n var messageId = \"MessageNo\" + i.toString();\n messageIds.push(messageId);\n\ \n var message = \"Hi! This is message no \" + i.toString();\n \ \ var params = {\n TableName: \"${ddbTableName}\",\n \ \ Item:{\n \"MessageId\": messageId,\n \"\ Message\": message\n }\n };\n docClient.put(params,\ \ function(err, data) {\n if (err) {\n console.error(\"\ Unable to add item. Error JSON:\", JSON.stringify(err, null, 2));\n \ \ } else {\n console.log(\"Added item:\", JSON.stringify(data,\ \ null, 2));\n }\n });\n };\n messageIds.push(\"DONE\"\ );\n callback(null, messageIds);\n};" - ddbTableName: Ref: DDBTable Runtime: nodejs14.x Timeout: '60' Metadata: SamResourceId: SeedingFunction SeedingFunctionExecutionRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: sts:AssumeRole Policies: - PolicyName: SeedingFunctionExecutionPolicy PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: - dynamodb:PutItem Resource: Fn::GetAtt: - DDBTable - Arn Metadata: SamResourceId: SeedingFunctionExecutionRole Outputs: StateMachineArn: Value: Ref: DynamoDBToSQSStateMachine ExecutionInput: Description: Sample input to StartExecution. Value: '{} '