DEV Community

Running Docker Container on AWS Lambda to manage AWS Services

Image description

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

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

Image description

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

  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

3.To generate Docker image from Docker file run the following command

docker build -t lambda-image:latest .

Image description

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

Image description

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

Image description

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

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

Image description

Top comments (0)