Search code examples
amazon-web-servicesaws-lambdaamazon-dynamodbaws-cloudformationserverless-framework

How can I use `!GetAtt` in serverless.yml default value operator?


I have serverless.yml which is to deploy a dynamodb and a lambda function. The lambda function needs to reference the dynamodb table stream arn.

functions:
  onStreamHandler:
    handler: ...
    name: ...
    events:
      - stream:
        type: dynamodb
        arn: ${opt:dbStreamArn, !GetAtt EntityTable.StreamArn}

In the arn: field, I'd like to specify if dbStreamArn is not specified from command line parameter, get the value from EntityTable.StreamArn. But I got this error:


  Serverless Error ---------------------------------------
 
  Missing "arn" property for stream event in function "onStreamHandler" The correct syntax is: stream: <StreamArn> OR an object with an "arn" property. Please check the docs for more info.

what is the right way to reference the arn value?


Solution

  • Could be because cloud formation is trying to create your lambda before your dynamodb table.

    To fix this, add dependsOn to your lambda function e.g:

    functions:
      onStreamHandler:
        handler: ...
        name: ...
        events:
          - stream:
            type: dynamodb
            arn: ${opt:dbStreamArn, !GetAtt EntityTable.StreamArn}
        dependsOn:
          - EntityTable
    

    This will deploy the function once the the EntityTable has been deployed. !GetAtt EntityTable.StreamArn should then resolve correctly.