top of page

Auto Scaling with AWS Lambda

Lambda : AWS provide a serverless service named Lambda that runs our code and manges the

triggred services for ususing the resources.it is manage the services parallaly and manage scalling. we

can create lambda functions to manage concurrent traffic. we can use our own logics to handle the

services by our own methods.

Creating a lambda function:

Our Aim : first wee need to create a s3 bucket.

then we create lambda function

provide lmabda function to work with s3 bucket so we will give permission from IAM role.

trigger point : When we will upload a file into bucket it will trigger the function that will show us that

our file is uploaded successfully on cloudwatch.


Step 1: create a s3 bucket named “s3withlambda-tbt” leave all option as defaults ----> create bucket


Step 2: Now go to IAM to give permission to the s3 and lambda.


Role---> Create role--->select lambda---> click on next--->attach policies as given

Amazons3FullAccess

AWSLambdaFull_Access

CloudWatchFullAccess

click on Next.


Step 3: Cleck on Create role.


We have create IAM role and S3 now lets move onto the lambda.

Step 4: find lambda on Services on AWS --> Create Function--->name your lambda function as

workinglambdas3-tbt”.

Code ---> Node js latest version.(14.x)

Choose permission ---> go to execution role(use an existing role)--->> select role that you have created

on IAM “lambdas3policy”. then next.

add this code and deploy it.



exports.handler = function(event, context, callback) {
console.log("Incoming Event: ", event);
const bucket = event.Records[0].s3.bucket.name;
const filename = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
const message = `File is uploaded in - ${bucket} -> ${filename}`;
console.log(message);
callback(null, message);
};

Add trigger as show in Above function ---> S3-->

choose event type all object created events ---> add




Now We have done all things lets check wheather its working or not.

Step 5: Go to s3 and upload any objects in your “s3withlambda-tbt”



Step 6: TO check our lambda function will trigger or not go throug the lambda monitor section.



Yes its working our function will trigger as shown in dots. now click on view logs on cloudwatch.

IN the cloud watch we can see all things that we have done so far is logged here .

· CloudWatch >> Log groups >> /aws/lambda/workinglambdas3-tbt >>

2021/05/18/[$LATEST]596020f309224f11a2ec29bc5270bbe4


Now find your file that name that you have uplaoded. It will look like below

2021-05-18T03:14:53.419Z b3348b52-4e0d-44a1-a45d-5b6efe03c20d

uploaded in - s3withlambda-tbt -> encryption.py


Now we have completely done with lambda function

lambda fuction will allow us a serverless execution that perform verious task in the backgroud.

Example : a client logged into the webapp the data will trigger the lambda fuction and lambda can do

what a developer wants. he can checks the google plugin that email is valid or not or password is right or not such this without direct sql interection of with the database. it will work as intermediate fuction between the client and database.


hope you learn more...

6 views0 comments

Comments


©2022 www.theblackthreat.in All right reserved.
bottom of page