[AWS Lab] Lambda Invocation via Polling – DynamoDB Stream

In this lab, we will learn how a lambda function can be invoked via Polling.

  • Overview
  • Source: DynamoDB Stream
    • Create a DynamoDB Table
    • Enable the Stream
  • Target: Lambda – Function
    • logs the received message
    • does not return any value
    • configures the trigger
    • Python

1. IAM – Setup the Lambda execution role

  • Click “Roles” on the left pane
  • Click “Create role
  • Select trusted entity
    • Trusted entity type: “AWS service
    • Use: “Lambda
  • Add Permissions
    • Search “AWSLambdaDynamoDBExecutionRole” and select it
  • Name, review
    • Name: “LambdaDynamoDBExecutionRole
  • Click “Create role

Lambda service needs to poll SQS. Therefore you need to provide the execution permission to the Lambda function.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "dynamodb:DescribeStream",
                "dynamodb:GetRecords",
                "dynamodb:GetShardIterator",
                "dynamodb:ListStreams",
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "*"
        }
    ]
}

2. Lambda – Create a Function

  • Function Name: “logMessage
  • Runtime: Python 3.9
  • Permissions
    • Check “Use an existing role
    • Select “LambdaDynamoDBExecutionRole
    • Click “Create function

3. Lambda – Function Code (Python)

  • Type the following code
  • Click “Deploy
import json
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
    logger.info(json.dumps(event))

4. DynamoDB – Create a table

  • Click “Create table
    • Table name: “Orders
    • Partition key: “OrderId“, “Number
    • Sort Key: “Item“, “String
  • Click “Create table

5. DynamoDB – Enable the Stream

  • Click “Tables” on the left pane
  • Select the “Orders” table
  • Click the “Exports and streams” tab
  • DynamoDB stream details
    • Click “Enable
    • Check “New and old images
    • Click “Enable stream

6. Lambda – Configure the DynamoDB Stream as a Lambda trigger

  1. On the “logMessage” function page, “Function overview” -> “Add trigger
  2. Select “DynamoDB” as a source
    • Table: “Orders
    • Batch size: 5
  3. Click “Add

7. DynamoDB – Test

  1. On the “Orders” table page, click “Explore table items
  2. Click “Create Item”
    • OrderId: 100
    • Item: Computer
    • Price: 1000 (Add new attribute – Number)

9. CouldWatch – Check the Lambda logs

  1. On the Lambda function page, click the “Monitor” tab
  2. Check some metrics on the page
  3. Click the “View logs in the CloudWatch” button
  4. On the CloudWatch page, click the “Log stream
  5. Check the logs

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s