AWS Lambda - The Easy Way !

First, we need to understand what is lambda function and serverless.

What is AWS Lambda function?

A lambda function is simply a code that you upload to AWS Lambda and it runs automatically when an event occurs. For example, when we push any object or any file to AWS S3 Bucket, it is an Event.

What is Serverless?

Serverless applications are ones that don’t need any server provision and do not require managing servers.

What is Lambda?

AWS lambda executes code only when it is needed or triggered. It runs code or lambda functions on the highly available infrastructure and can also scale up or down automatically. Therefore you don’t need to worry about which AWS resources to launch, or how will you manage them. Instead, you need to put the code on Lambda, and it runs automatically.

AWS Lambda supports only GO, C#, JAVA, PYTHON, RUBY, NODE, and POWERSHELL languages for the writing lambda function.

Additionally, AWS Lambda can only be used to run Background tasks.

Lamda is used to run code that can run b/w 3 - 15 seconds of timespan otherwise it will cost much more than EC2 or any dedicated server.

DOWNSTREAM RESOURCES - It is the resources the Lambda function calls when it is triggered. Such as Dynamo DB, S3, etc.

TYPES OF EVENTS THAT CAN TRIGGER LAMBDA FUNCTION - PUSH, PULL, EVENT Based.

WORKING OF LAMBDA IN REAL LIFE, FOR EXAMPLE:

Suppose we have to insert data in a table of DynomoDB automatically every time when any file is pushed into the S3 bucket.

STEP 1: Create an IAM role that has AWS Service selected and has a use case of Lambda

And has the following permissions selected for the IAM role:

  1. AmazonS3ReadOnlyAccess

  2. AmazonDynamoDBFullAccess

And then name your IAM role as per the need.

STEP 2: Create Lambda function, Go to Search > Lambda > Create from scratch

Here, name the function as per the need and select the language in which you want to write the code of the Lambda function

Now, we have to choose permissions for Lambda that what it can do. That is why we created IAM beforehand which has required permissions.

And here we are using "USE AN EXISTING ROLE" because we have created otherwise we can make a new role with basic Lambda permissions or choose from an existing template that AWS provides by default.

STEP 3: Now The Lambda function is created

Now if we scroll down this page we have a type of code editor in which we can write the code for the Lambda function.

CODE:

import boto3 # AWS SDK for python
from uuid import uuid4

def lambda_handler(event, context): # event contains information about the event that triggered the function, and context contains information about the execution environment of the function.

# creating S3 and dynamodb client objects which is used for interaction with the resources
    s3 = boto3.client("s3")
    dynamodb = boto3.resource('dynamodb')

# entries to do in 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') # specifing table 
# NOTE: the table should be created with the same name as specified here in dynamoDB 

        dynamoTable.put_item(
            Item={'unique': str(uuid4()), 'Bucket': bucket_name, 'Object': object_key,'Size': size, 'Event': event_name, 'EventTime': event_time}) # Putting item in dynameDB as dictionary format as it accepr only this, with .put_item function.

and save this code.

STEP 3:

Now, We have to add a trigger i.e. for which event the Lambda function should run.

Now select the S3 form drop-down menu and select the bucket by which you want to trigger the Lambda function.

Now select the Event type on which we have to trigger the function. We have various options;

We can also select specific "Prefix" or "Sufix" on which the lambda should be triggered.

Our S3 is now set as a trigger.

PERFECT !

As, we now have a dynamo DB table, Lambda, and S3 set all in place.

Now, if any object is uploaded to the S3 bucket we can see the entry in a dynamo DB table.

  • File is uploaded

  • Corresponding Entry in the table created.

Thank you! This is My First Blog, Hope you liked it!

Did you find this article valuable?

Support Sukhpreet Singh by becoming a sponsor. Any amount is appreciated!