Running Docker Container on AWS Lambda to manage AWS Services
Published: 2023-12-01 09:25:18



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mknc3f2r6ejitm3ak4iy.PNG)



We are going to manage AWS service EC2 using AWS Lambda Containers.
1.	We have used python language to manage aws service using lambda container.
2.	We have official documentation of aws boto3 to manage all aws service using python3.

Pre-requisites:

•	AWS CLI
•	AWS Services (LAMBDA, ECR Registry and EC2)
•	Docker
•	Python

We have a python script to describe list of ec2 instances in AWS
**Python Script**
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qv60qxlw0gbof2s0g9ks.png)


Docker file
We have taken a Docker file from AWS official documentation which is supported to AWS lambda service python image.
 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zowf34rqfey6j69taf2u.png)


To create a container image from an AWS base image for Python
1.	Create a directory for the project, and then switch to that directory and copy Docker file and lambda_function.py file into this directory.
2.	For example, here's what your requirements.txt should look like if your function uses the AWS SDK for Python (Boto3).
3.	To generate Docker image from Docker file run the following command     
                     docker build  -t lambda-image:latest .



To upload the image to Amazon ECR and create the Lambda function.
1.	Run the get-login-password command to authenticate the Docker CLI to your Amazon ECR registry.
2.	Create a New ECR registry to store Docker images.
3.	Let’s Tag the Docker image with registry name and push the Image to  ECR registry by using aws cli command.

Start Deploying Docker image into AWS Lambda !
let’s create a EC2 machine with required installed services like, Docker, Python and AWS(aws-cli).
 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qx4p7ycvv102mho5jyos.png)


1.	Create a directory for the project, and then switch to that directory and copy Docker file and lambda_function.py file into this directory.
2.	For example, here's what your requirements.txt should look like if your function uses the AWS SDK for Python (Boto3).

 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3u5okgjdp68jgr7zub7o.png)




3.To generate Docker image from Docker file run the following command     
                     docker build  -t lambda-image:latest .

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/orq9hhigrbp68o0t47uj.png)



 
4.Create a New ECR registry to store Docker images.

 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9mgtpvv5p588t1grxwjg.png)







5.Let’s Tag the Docker image with registry name and push the Image to  ECR registry by using aws cli command.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u06t3z6yyx51tdudkz8u.png)


 



6.Now create a lambda function and provide url of the docker image from ECR and add required IAM role permissions to access EC2 services using lambda.

 



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fmdutpdbykwr3y4s47l5.png)



Now let’s run the lambda funtion to test it. Observe the instance I’d in the Log output.
 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hcd34yndm2btokudbgot.png)