Open In App

Serverless Computing With AWS Lambda And Docker: Running Custom Containers

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

AWS Lambda is a serverless computing service that runs the code without any management of servers by the user. On the other hand, Docker is a tool that helps to encapsulate applications with its dependency into docker containers. AWS Lambda and Docker are some of the most important services used by organizations. Here in this, I will first explain what is AWS Lambda. Then I will discuss what is Docker. After this, I will walk you through the steps to run a containerized Python application on AWS Lambda.

What is AWS Lambda?

AWS Lambda is a serverless computing service that runs the code without any management of servers. AWS Lambda supports various programming languages such as Python, Golang, Node.js, Java, and many more. The Lambda function can be triggered automatically on the occurrence of any specific event or at a particular scheduled time. AWS Lambda only charges you when someone executes the code and if the AWS Lambda does not receive any request for execution then there will be no cost incurred on your AWS account. When you are running your code on the AWS EC2 Instance, you have to add an additional autoscaling group to scale up or down on the basis of the request. But in AWS Lambda, it will automatically scale up or down on the amount of traffic it receives. In summary, we can say AWS Lambda simplifies the development of serverless applications and provides a flexible, scalable, and cost-effective platform to run code without any management of servers.

What is Docker?

Docker is a tool that is used to containerize applications along with their dependencies. This means packing the application and its dependencies into compact units called docker containers. These containers contain all the application code, run time, dependencies, and other libraries to run the application. Here the developers first write a dockerfile mentioning the base image, working directory, build commands, and commands to run the application. This docker file is then built to generate a docker image using the docker build command. These docker images are lightweight and portable. The docker images can run on any operating system, only condition is that the docker should be installed on that system. In this way, it allows cross-platform compatibility. The docker containers use fewer resources to run on a system. This means we can run multiple docker containers on a single machine. This results in the maximum utilization of resources in a machine and also it decreases overall infrastructure cost. In summary we can docker has become a very important tool for the organization for software application developments and delivery pipelines.

Pre-requisites

Before moving to the next section make sure that you have installed docker on your system. If not installed then follow these detailed geeks for geeks articles to install docker on your system.

Steps To Run A Docker Container On AWS Lambda

Step 1 : Create a sample python app .

python-app

Step 2 : Write necessary requirements to install on the docker container in requirements.txt .

requirement

Step 3 : Create a dockerfile which will help to containerize the python application .

dockerfile-app

Step 4 : Now to go AWS Console and then create an ECR repository . Here in this repository you will store the docker image .

repo

Step 5 : Now search for AWS IAM service. Here go to security credential to create access key and security key.

cli-access

Give a name to the credentials .

a-name

Step 6 : Now use this command below to configure your AWS on the local environment . After this enter the access key , secret key and region that is created in Step 5 .

aws configure


aws-configure

Step 7 : Now go to AWS ECR . Here enter inside the demo repository . Here click on the view push commands .

docker-push-commands

Step 8 : Now run all the push commands on the local editor where your python application and dockerfile is stored .

run-the-commands

Step 9 : After step 8 , your docker image will be pushed to ECR repo . Now you copy image URI .

copy-image-uri

Step 10 : Create a AWS Lambda function using container image . Here paste the image URI and then click create function .

lambda-function-create

Step 11 : After this Lambda function will be created . Now click test to execute the code and then observe the output .

test-the-function

Conclusion

Combining AWS Lambda with Docker containers for serverless computing offers a powerful and flexible development approach.

Serverless Computing with AWS Lambda and Docker: Running Custom Containers – FAQ’s

What are the different runtimes supported for containers on AWS Lambda ?

It almost supports all types of runtime . For example python , golang , node.js , java and many more .

How to monitor performance of containerize application running on AWS Lambda ?

You can monitor the performance using the CloudWatch metrics and logs .

How does AWS Lambda works ?

Users upload their code onto AWS Lambda . Then they can run their code without managing any servers . Also the AWS Lambda will automatically scale up or down on the basis of request it receives . AWS Lambda function will trigger on any specific event or on a scheduled time .

What is the maximum execution time in AWS Lambda ?

Maximum execution time in AWS Lambda is 15 minutes .

What is the key difference between running a code on AWS EC2 Instance and AWS Lambda service ?

When you are running code on EC2 Instance you basically install all dependencies and manage the server yourself , but in AWS Lambda you have to only write the code and execute it without any management of server .



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads