0
0
AWScloud~5 mins

Lambda with DynamoDB Streams in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want your app to react automatically when data changes in a database. AWS Lambda with DynamoDB Streams lets you run code right after data updates happen in DynamoDB tables, without needing a server always running.
When you want to send a notification every time a new item is added to your DynamoDB table.
When you need to update a search index automatically after data changes in your database.
When you want to keep data in sync between DynamoDB and another system without manual steps.
When you want to process or filter data changes in real-time for analytics or monitoring.
When you want to trigger workflows or other AWS services based on database updates.
Config File - lambda-dynamodb-streams.yaml
lambda-dynamodb-streams.yaml
Resources:
  MyDynamoDBTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: example-table
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      BillingMode: PAY_PER_REQUEST
      StreamSpecification:
        StreamViewType: NEW_IMAGE

  MyLambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: example-lambda
      Runtime: python3.9
      Handler: index.handler
      Role: arn:aws:iam::123456789012:role/lambda-execution-role
      Code:
        ZipFile: |
          def handler(event, context):
              for record in event['Records']:
                  print('DynamoDB Record:', record['dynamodb'])
              return 'Processed'

  MyEventSourceMapping:
    Type: AWS::Lambda::EventSourceMapping
    Properties:
      EventSourceArn: !GetAtt MyDynamoDBTable.StreamArn
      FunctionName: !Ref MyLambdaFunction
      StartingPosition: TRIM_HORIZON

MyDynamoDBTable: Defines a DynamoDB table with a stream enabled to capture new item images.

MyLambdaFunction: A simple Lambda function in Python that processes DynamoDB stream events.

MyEventSourceMapping: Connects the DynamoDB stream to the Lambda function so it triggers on data changes.

Commands
This command creates the DynamoDB table, Lambda function, and the event source mapping connecting them. It uses CloudFormation to deploy all resources together.
Terminal
aws cloudformation deploy --template-file lambda-dynamodb-streams.yaml --stack-name example-stack --capabilities CAPABILITY_NAMED_IAM
Expected OutputExpected
Waiting for stack create/update to complete... Successfully created/updated stack - example-stack
--capabilities CAPABILITY_NAMED_IAM - Allows creation of IAM roles needed by Lambda
Adds a new item to the DynamoDB table to trigger the Lambda function via the stream.
Terminal
aws dynamodb put-item --table-name example-table --item '{"id": {"S": "123"}, "name": {"S": "Test Item"}}'
Expected OutputExpected
{}
Checks the Lambda function logs to see the output from processing the DynamoDB stream event.
Terminal
aws logs filter-log-events --log-group-name /aws/lambda/example-lambda --limit 5
Expected OutputExpected
{"events":[{"message":"DynamoDB Record: {\"Keys\": {\"id\": {\"S\": \"123\"}}, \"NewImage\": {\"id\": {\"S\": \"123\"}, \"name\": {\"S\": \"Test Item\"}}, \"SequenceNumber\": \"000000000000000000001\", \"SizeBytes\": 50, \"StreamViewType\": \"NEW_IMAGE\"}","timestamp":1680000000000}]}
--limit 5 - Limits output to the 5 most recent log events
Key Concept

If you remember nothing else from this pattern, remember: DynamoDB Streams capture data changes and Lambda runs your code automatically when those changes happen.

Common Mistakes
Not enabling the DynamoDB stream on the table.
Without the stream enabled, Lambda will never receive events to trigger on.
Always set StreamSpecification with StreamViewType when creating the DynamoDB table.
Using the wrong ARN or function name in the event source mapping.
If the event source mapping points to the wrong resource, Lambda won't trigger on the stream events.
Use the DynamoDB table's StreamArn and the exact Lambda function name or ARN.
Not giving Lambda permission to read from the DynamoDB stream.
Lambda needs an IAM role with permissions to access the stream; otherwise, it will fail to run.
Attach an IAM role to Lambda with the AWS managed policy AWSLambdaDynamoDBExecutionRole or equivalent.
Summary
Create a DynamoDB table with streams enabled to capture data changes.
Create a Lambda function that processes events from the DynamoDB stream.
Set up an event source mapping to connect the DynamoDB stream to the Lambda function.
Add data to the table to trigger the Lambda and verify processing via logs.