AWSTemplateFormatVersion: "2010-09-09" Transform: AWS::Serverless-2016-10-31 Description: > aws-stepfunction-waitable-pattern Sample SAM Template for aws-stepfunction-waitable-pattern Resources: WaitableStateMachine: Type: AWS::Serverless::StateMachine Properties: DefinitionUri: statemachine/waitable-stepfunction.asl.json DefinitionSubstitutions: DDBTable: !Ref StatusTable Policies: - DynamoDBWritePolicy: TableName: !Ref StatusTable StatusTable: Type: AWS::DynamoDB::Table Properties: AttributeDefinitions: - AttributeName: "Id" AttributeType: "S" KeySchema: - AttributeName: "Id" KeyType: "HASH" ProvisionedThroughput: ReadCapacityUnits: 1 WriteCapacityUnits: 1 ApiGatewayStepFunctionsRole: Type: AWS::IAM::Role Properties: Path: !Join ["", ["/", !Ref "AWS::StackName", "/"]] AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Sid: AllowApiGatewayServiceToAssumeRole Effect: Allow Action: - 'sts:AssumeRole' Principal: Service: - apigateway.amazonaws.com Policies: - PolicyName: CallStepFunctionsAndScanDynamo PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: - 'states:StartExecution' - 'dynamodb:Scan' Resource: - !Ref WaitableStateMachine - !GetAtt StatusTable.Arn ApiGateway: Type: AWS::ApiGateway::RestApi Properties: Name: !Sub ${AWS::StackName}-api ApiGatewayRootMethod: Type: AWS::ApiGateway::Method Properties: AuthorizationType: NONE HttpMethod: POST OperationName: "StartWorkflow" MethodResponses: - StatusCode: 200 ResponseModels: application/json: Empty Integration: IntegrationHttpMethod: POST Type: AWS Credentials: !GetAtt ApiGatewayStepFunctionsRole.Arn Uri: !Sub arn:aws:apigateway:${AWS::Region}:states:action/StartExecution PassthroughBehavior: WHEN_NO_TEMPLATES RequestTemplates: application/json: !Sub - |- { "input": "{\"Id\" : \"$context.requestId\", \"StartTimestamp\": \"$input.path('$.StartTimestamp')\"}", "stateMachineArn": "${StateMachineArn}" } - { StateMachineArn: !Ref WaitableStateMachine } IntegrationResponses: - StatusCode: 200 ResponseTemplates: application/json: '' ResourceId: !GetAtt ApiGateway.RootResourceId RestApiId: !Ref ApiGateway StatusResource: Type: AWS::ApiGateway::Resource DependsOn: - ApiGateway Properties: RestApiId: !Ref ApiGateway ParentId: !GetAtt ApiGateway.RootResourceId PathPart: "status" StatusMethod: Type: AWS::ApiGateway::Method Properties: AuthorizationType: NONE HttpMethod: GET OperationName: "GetStatus" MethodResponses: - StatusCode: 200 ResponseModels: application/json: Empty Integration: IntegrationHttpMethod: POST Type: AWS Credentials: !GetAtt ApiGatewayStepFunctionsRole.Arn Uri: !Sub arn:aws:apigateway:${AWS::Region}:dynamodb:action/Scan PassthroughBehavior: WHEN_NO_TEMPLATES RequestTemplates: application/json: !Sub - |- { "TableName": "${TableName}" } - { TableName: !Select [1, !Split ['/', !GetAtt StatusTable.Arn]] } IntegrationResponses: - StatusCode: 200 ResponseTemplates: application/json: | #set($inputRoot = $input.path('$')) { "items": [ #foreach($elem in $inputRoot.Items) { "Id": "$elem.Id.S", "StartActionAt": "$elem.StartActionAt.S" "LastUpdated": "$elem.LastUpdated.S", "ProcessStatus": "$elem.ProcessStatus.S" }#if($foreach.hasNext),#end #end ] } ResourceId: !Ref StatusResource RestApiId: !Ref ApiGateway apiGatewayDeployment: Type: AWS::ApiGateway::Deployment DependsOn: - ApiGatewayRootMethod - StatusMethod Properties: RestApiId: !Ref ApiGateway StageName: 'dev' Outputs: WaitableStateMachineArn: Description: "Waitable state machine ARN" Value: !Ref WaitableStateMachine WaitableStateMachineRole: Description: "IAM Role created for waitable state machine based on the specified SAM Policy Templates" Value: !GetAtt WaitableStateMachineRole.Arn