DynamoDB streams creation

First make sure your resource includes a StreamSpecifcation so that is setup up the stream

resources:
  Resources:
    MyTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: my_table
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 5
          WriteCapacityUnits: 5
        StreamSpecification:
          StreamViewType: NEW_AND_OLD_IMAGES

See http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_StreamSpecification.html for more information about values.

You also need to make sure your iamRoleStatement has the correct permissions.

provider:
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeStream
        - dynamodb:GetRecords
        - dynamodb:GetShardIterator
        - dynamodb:ListStreams
      Resource: arn:aws:dynamodb:*:*:table/my_table

I also have dynamodb:GetItem and dynamodb:PutItem because my Lambda needs to put and get items so you may or may not need them too.

Deploy now so that it creates the stream then go to the AWS console and get the Stream ARN for your table.

Finally configure your function with a stream event using the Stream ARN from the AWS console.

  streamFunction:
    handler: streamFunction.handler
    events:
      - stream: arn:aws:dynamodb:REGION:ACCOUNT_ID:table/my_table/stream/2016-11-20T22:55:25.566

Deploy again and your Lambda function should be getting called when records change.

The Serverless manual includes a new format:

- stream:
    arn:
      Fn::GetAtt:
        - MyTable
        - Arn

In Serverless 1.1 there is a confirmed bug that prevents this from working. The recommended solution is to use the arn format.

10 Likes