How to Setup S3 Trigger with AWS Lambda ?

Reading Time: 3 minutes

What is AWS Lambda?

In Aws, we can run code using the computation service Lambda without setting up or maintaining servers.

In essence, this suggests that AWS Lambda development may be done with no concern for setting up servers or other infrastructure.

When data in an Amazon S3 bucket or an Amazon DynamoDB table changes, for example, your code can be launched in response using Lambda.

Create an AWS Lambda:

  1. Click on the Lambda Console
  2. Choose Create Function
  3. Click Create from scratch
  4. Give your AWS Lambda a name
  5. Select Python 3.6
  6. Click Create Function
To start writing from scratch, select Create Function.( To write your own code)

Give the run time, the basic information (such as the function name), and, most importantly, the language you chose to construct your function.

Click on save after choosing the Execution role and the permissions, then choose the current role you’ve given the lambda function.
Once you have finished the first phase, then The function is created and ready to use. additionally, you now need to write the following code.
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')
        dynamoTable.put_item(
            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:

Trigger Configuration:

Create an S3 bucket and give it a name. Then, when the AWS Lambda is ready, select the Event type as All Object Create Trigger and click Create.
The trigger has now been added to the lambda function, so check Dynamodb.

Create a Dynamo DB table first.

The partition key will be the same as Unique and the table name should match the AWS Lambda code that was previously written. The dynamo DB table is generated after clicking save.
Now that the table has been built, we can see whether or not the go-to items that we triggered in the S3 bucket have been changed on the dynamo database. visit Items and review every detail there.

Dynamo DB, which is created by the s3 bucket, is where the information is triggered. Its distinct identifier, bucket, event, event time, object, and will also indicate the size

Conclusion:-

This blog post demonstrated how to set up an S3 Trigger in AWS with Lambda and DynamodDB.

To read more about DevOps.

Happy Learning!!

Reference:

https://docs.aws.amazon.com/

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading