Skip to content

Connector with event source mapping and DLQ requires DependsOn #2534

@hoffa

Description

@hoffa

Consider the following template:

Transform: AWS::Serverless-2016-10-31
Resources:
  MyTable:
    Type: AWS::DynamoDB::Table
    Properties:
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      StreamSpecification:
        StreamViewType: NEW_IMAGE

  MyRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Action: sts:AssumeRole
            Principal:
              Service: lambda.amazonaws.com
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

  MyFunction:
    Type: AWS::Lambda::Function
    Properties:
      Runtime: nodejs16.x
      Handler: index.handler
      Role: !GetAtt MyRole.Arn
      Code:
        ZipFile: |
          exports.handler = async (event, context) => {
            console.log(JSON.stringify(event));
          };

  MyQueue:
    Type: AWS::SQS::Queue

  MyEventSourceMapping:
    Type: AWS::Lambda::EventSourceMapping
    Properties:
      EventSourceArn: !GetAtt MyTable.StreamArn
      FunctionName: !Ref MyFunction
      StartingPosition: TRIM_HORIZON
      DestinationConfig:
        OnFailure:
          Destination: !GetAtt MyQueue.Arn

  MyConnector:
    Type: AWS::Serverless::Connector
    Properties:
      Source: 
        Id: MyTable
      Destination: 
        Id: MyFunction
      Permissions:
        - Read

  LambdaToQueue:
    Type: AWS::Serverless::Connector
    Properties:
      Source: 
        Id: MyFunction
      Destination: 
        Id: MyQueue
      Permissions:
        - Write

Deploying it will fail with:

MyEventSourceMapping
Resource handler returned message: "Invalid request provided: The provided execution role does not have permissions to call SendMessage on SQS (Service: Lambda, Status Code: 400, Request ID: 6ac8d38a-723c-4356-a146-caa6d4d0fef0)" (RequestToken: a66d8170-30dd-29b4-8010-560f8b56c5db, HandlerErrorCode: InvalidRequest)

To fix this, we need to make sure the permission resource (generated by LambdaToQueue) is created first:

@@ -41,6 +41,7 @@
     Type: AWS::SQS::Queue
 
   MyEventSourceMapping:
+    DependsOn: LambdaToQueuePolicy
     Type: AWS::Lambda::EventSourceMapping
     Properties:
       EventSourceArn: !GetAtt MyTable.StreamArn

Would be great if this was simpler.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions