AWS LAMBDA Setup Process with S3 Bucket

  1. Login to your AWS management console and create an IAM role.

2. Then click on create role and choose AWS service and in common use case u will see lambda select it and click on next: permissions.

3. Now we have to create a policy for DynamoDB. For this session I am taking DynamoDB full access and click on next.

4. Now in the next tab you can give tags.

5. After this enter the name for your role and click on create role.

6. Once a role is created now go to lambda in AWS services compute section.

7. Now first we need to create a function click on create function.

8. Now we have 3 options to create a function. I am choosing the Author from scratch option.

9. Now here we need to give our lambda name and runtime language in which we want to create our lambda. Also select the role which we created earlier and click on the create function.

10. Once lambda is created successfully you will receive a success message on the next window.

11. Now we need to add a python code in a function code section.

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})

12. Rest of the settings you can choose default and click on deploy.

13. Now we have to add a trigger for this first we need to create a bucket .So go to S3 and create a bucket and allow public access of bucket.

14. Now go back to lambda and click on add trigger and choose S3.

 15. Now select bucket name and event type as all object create events and click on Add.

16. Now on the next window it is visible to you that a bucket is added as a trigger.

17. Now we need to create a DynamoDB table so go to AWS services and create a DynamoDB table.

18. Now create a DynamoDB table by clicking create table and fill all the details like table name and primary key same as you used in your function code then click on create.

19.

20. Now check in items columns you will not find any entries.

21. Now go back to the s3 bucket and upload a file in your s3 bucket.

22. Once you upload a file go and check in the DynamoDB table you will find an entry of that image in the table.

23. In the DynamoDB table you will find all the entries related to uploaded file like file name, date & time etc.

If you need help in configuring this cluster, feel free to email us vic@teamitserve.com or call us at +1 810 214 2572.

Leave a Comment

Your email address will not be published.