We are going to manage AWS service EC2 using AWS Lambda Containers.
- We have used python language to manage aws service using lambda container.
- 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
Docker file
We have taken a Docker file from AWS official documentation which is supported to AWS lambda service python image.
To create a container image from an AWS base image for Python
- Create a directory for the project, and then switch to that directory and copy Docker file and lambda_function.py file into this directory.
- For example, here's what your requirements.txt should look like if your function uses the AWS SDK for Python (Boto3).
- 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.
- Run the get-login-password command to authenticate the Docker CLI to your Amazon ECR registry.
- Create a New ECR registry to store Docker images.
- 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).
- Create a directory for the project, and then switch to that directory and copy Docker file and lambda_function.py file into this directory.
- 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 .
4.Create a New ECR registry to store Docker images.
5.Let’s Tag the Docker image with registry name and push the Image to ECR registry by using aws cli command.
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.
Now let’s run the lambda funtion to test it. Observe the instance I’d in the Log output.
Top comments (0)