2
votes

I have two Cloudformation stacks. One of them defines the Dynamodb.

I want the second stack to use the Dynamodb referenced in the first stack. The second stack has a Lambda function which will receive dynamodb stream from stack 1.

How should I export and import dynamodb from one stack to another for Lambda stream consumption?

1

1 Answers

10
votes

In the DynamoDB table stack, export the StreamArn attribute of the table. Use that as the value of the EventSourceArn to define an EventSourceMapping resource in your Lambda stack.

# DynamoDB stack
Resources:
  Table:
    Type: AWS::DynamoDB::Table
    Properties:
      StreamSpecification: NEW_AND_OLD_IMAGES  
      # etc.

Outputs:
  TableStreamArn:
    Value: !GetAtt Table.StreamArn
    Export:
      Name: StreamArn

# Lambda stack
Resources:
  Function:
    Type: AWS::Lambda::Function
    # etc.

  EventMapping:
    Type: AWS::Lambda::EventSourceMapping
    Properties:
      EventSourceArn: !ImportValue StreamArn
      FunctionName: !GetAtt Function.Arn
      # etc.

You could also do it the other way around - import the Lambda function ARN to the DynamoDB stack and define the event mapping there, but that feels less logical to me.