How to Setup S3 Trigger with Lambda and Dynamodb?

Reading Time: 3 minutes

What is AWS Lambda?

AWS Lambda is a compute service that lets you run code without provisioning or managing servers. In essence, this implies that you can develop code in Lambda without having to worry too much about configuring or providing servers or infrastructure.

You can use AWS Lambda to run your code in response to events, such as changes to data in an Amazon S3 bucket or an Amazon DynamoDB table.

Create an AWS Lambda:

  1. Open the Lambda Console
  2. Choose Create Function
  3. Click Create from scratch
  4. Give your Lambda a name
  5. Select Python 3.6
  6. Click Create Function
Click on Create Function -> Author from scratch( To write your own code)

Provide the Basic information as the Function name, and most especially the run time to select the language used to write your function.

Select the permissions and Execution role, select the current role that you have created for the lambda function, and then click on save.
Here once you have completed the step for the lambda. The function is created and ready to use. and further now you have to write the code for the following.
import boto3
from uuid import uuid4
def lambda_handler(event, context):
    s3 = boto3.client("s3")
    dynamodb = boto3.resource('dynamodb')
    for record in event['Records']:
        bucket_name = record['s3']['bucket']['name']
        object_key = record['s3']['object']['key']
        size = record['s3']['object'].get('size', -1)
        event_name = record ['eventName']
        event_time = record['eventTime']
        dynamoTable = dynamodb.Table('newtable')
            Item={'unique': str(uuid4()), 'Bucket': bucket_name, 'Object': object_key,'Size': size, 'Event': event_name, 'EventTime': event_time})

Once done, make sure the name for the Dynamo table = “new table” and the Partition key should be Unique and similar to the name of the dynamo table.


Trigger Configuration:

Create an S3 Bucket as the name for which Lambda will select the Event type = All object create Trigger then click on create and the trigger will be added to the Lambda.
Now the trigger is added to the lambda function and let’s check on Dynamodb

Firstly create a Dynamo DB table.

Table name should be the same as the code which was written earlier in AWS lambda and the partition key will be the same as Unique. click on save and the dynamo DB table is created.
The table is created and now the go-to items that we have triggered in the S3 bucket are updated on the dynamo database or not. go to Items and check all the details in it.

The information is trigged in dynamo DB which is created through the s3 bucket. Its unique id, bucket, event, event time, object, and will tell the size also.


In this blog, we have seen how to set up S3 Trigger with Lambda and DynamodDB in Aws.

To read more about DevOps.

Happy Learning!!