Amazon Web Services Lambda (AWS Lambda)

In this tutorial, we are going to learn about Amazon Lambda. Let’s learn this with a simple realtime scenario.

One day in the growing IT company there was a question going on between two IT personals. It was about the new role that this girl Rose has taken. Rose the girl sitting here wants to know something about the job role. And Rose is new to the company.

Rose’s job was different, dynamic, and interesting too. She scaled and managed servers and operating systems. And also applied security patches to them and monitored all of these at the same time to ensure the best quality of applications for the users. And Jack was the supervisor and supervised the work which Rose was doing.

Rose was a very dynamic person and very suitable for the job. It was easy to handle all of the tasks assigned to her. But that easiness of the job did not last long as the environment grew more and more Rose was getting drained. And she was not really happy about the job she used to be. Rose’s manual way of scaling the environment did not last long. And she was also finding out that and she missed to scale down some of the resources and it was costing her a lot. She needed to pay for those services that she was not using. She felt that she was ended up the role. And there was no way to handle those manual tasks. She was very desperate.

And then Jack suggested a service called AWS Lambda that can easily work.

Lambda is a greater solution and it solves the manual repeated work. So Jack started to explain about the following things that we gonna learn about this tutorial.

  • The features of AWS Lambda
  • What is AWS Lambda
  • Where AWS Lambda is used
  • How AWS Lambda works
  • Use-Case — Backing up data

The features of AWS Lambda?

Lambda automatically runs our code without requiring us to provision or manage servers. Just write the code and upload it to Lambda and Lambda will take care of it. This also means that you can stop worrying about provisioning and managing servers. The only thing Lambda expects from you is the code that is working.

AWS Lambda has also scaled our application by running code and response to each trigger. Our code runs in parallel and processors each trigger individually. Scaling is done automatically based on the size of the workload. Lambda can scale the application running the code in response to each and every trigger that it receives.

Billing in the Lambda is metering on the second. We only pay for the amount of time that our code is running. Which means that you’re not charged for any of the servers. The only payment required is for the amount of time the code is computed.

What is AWS Lambda?

AWS Lambda is one of the services that fall under the “Compute” domain of services that AWS provides. Amazon EC2, Amazon EBS, Amazon Elastic Load Balancing, and AWS Lambda are also the services that come under the bigger umbrella compute services in AWS.

Lambda allows us to execute code for any type of application. With AWS Lambda we can run code for any type of application backend services. All we need to do is supply our code in one of the languages that AWS Lambda supports. The supported languages are NodeJS, Java, C#, Go, and Python. Lambda can be used to run code and respond to certain events from other services. And based on the event we can run functions and those functions can be in any language.

Where is Lambda used?

There is a huge number of use-cases for Lambda. There are many ways AWS Lambda is using specifically in the business world.

  • AWS Lambda is used to process images when it’s uploaded an S3 bucket

Let's say the object gets uploaded in an S3 bucket in a format that we don’t expect to be. Which means the file needs to be formated. So it gets uploaded in a row format. And AWS Lambda is triggered anytime the new object is added to the bucket. And images of process converted into thumbnails based on the devices. So based on the devices different formats Lambda can trigger and convert the picture into the different formats that it requires.

  • AWS Lambda is used to analyze social media data

Let’s say we are collecting hashtags and trending data. Those data added to the Amazon Kinesis stream to feed into the Amazon environment. Then Lambda's action gets triggered and received the data. Then the data is stored into a database that can be used by businesses or later analysis.

How AWS Lambda works

How the complicated functions behind the scenes work in a simple and seamless way. Here Client(Client could be anyone who’s sending requests to AWS Lambda. It could be an application or other Amazon Services) sends data to Lambda. Then Lambda receives the requests. Depending on the size, amount, and volume of the data it runs on the defined number of containers.

If it is a single request or less request it runs on a single container. The requests are given to the container and the container which contains the code the user has provided to satisfy the query would run.

Containers are lightweight standalone executable packages of a piece of software that includes everything that needs to run it like the codes(system libraries). Containers isolated software on its surroundings. For example that could be the difference between development and staging environment. So that this reduces the conflicts between the teams running in the different software on the same infrastructure.

If there are few requests that send to a single container and also if requests grow there actually create multiple containers share the multiple requests. And depending on the volume, size, and the sessions the more number of containers are provision to handle those requests. And when the requests reduce the number of containers reduce as well. And that helps to save costs.

In fact, you're changing for the amount of the time that unction is running inside these containers.

Now consider the situation you have to set up temporary storage and the system to back up the data as soon as the data is uploaded. Here the manual backups are nearly impossible and not efficient. We can use AWS Lambda to set things up. So AWS Lambda manually handles the backup.

Steps to create a backup

  • Create two S3 buckets — One would be the source bucket (Where the data is uploaded) and the other one is Destination bucket (Where the data will be backed up from the source bucket)
  • Create IAM roles and policies — For these buckets to talk to themselves its gonna require an IAM role
  • Create a Lambda function to copy files between buckets — From the source bucket to the destination bucket

Now, what triggers the Lambda function? The Lambda function is triggered every time there’s upload into the bucket. This data is then uploaded into the backup bucket.

  • Testing it out — Finally, we can test this by putting data in a source bucket and that is gonna automatically copy from the source bucket to destination bucket.

How we can replicate data between two buckets?

We have a feature called Cross-region replicate in S3. If you wanna replicate two different buckets or two different accounts, in those cases you can you Lambda to replicate the data between the buckets. Use the above steps to replicate.

In just a couple of clicks, we have set up a temporary backup system that can run seamlessly without any manual operation and that can be as near realtime as possible.

Okay! We have learned deep things about AWS Lambda and we will learn more things about AWS S3 in next tutorial.

Thank You!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kasun Dissanayake

Senior Software Engineer at IFS R & D International || Former Software Engineer at Pearson Lanka || Former Associate Software Engineer at hSenid Mobile