What is an AWS Lambda ?
AWS is one of the most popular and broadly adopted Cloud Services Providers which provide over 200 featured services. Lambda is a managed service by AWS and is serverless, i.e. you don't have to manage any servers. In December 2020, AWS announced the support of Container Images for Lambda service. In Lambda, we can put our code with required permissions and triggers without managing any servers, its resources, configuration.
Today's Agenda
In this post, we will learn how to run a Lambda function on a Docker Container. We will also see how we can make changes to our code and keep Docker Image versions stored. We will deploy Python code to our Lambda function.
You can also read Lambda Function to track Open Security Groups in AWS.
Prerequisite
This post has been prepared for the audience who :
- You should have an AWS account with access to create a Lambda Function, ECR and IAM Role.
- Have basic hands-on on Python Scripting language.
- Have worked on Docker and has basic knowledge of Dockerfile.
- And finally, are eager to learn and try something new.
Let's get started
Step 1: Create a new ECR repository on AWS to store all your Docker images.
Go to AWS home page > Services > Containers > Elastic Container Registry.
We have to authenticate to above ECR to push docker images. We will do it afterwards. Before that we will create a new Lambda function and Dockerfile to create Image.
Step 2 : Create a Docker image using Dockerfile.
We have to create a file structure like this:
~/trc/
|
|
|--------Dockerfile
|
|--------requirements.txt
|
|--------hello_lambda.py
i. Create a file with name Dockerfile and add the build steps inside it. You can change the python version in the line 1. AWS provides several options for Lambda base images to select from.
ii. Create a file with name requirements.txt and add all the required libraries on your python code.
iii. Create a file with name hello_lambda.py and add your python code to it. We have used Python3.8 in this blog.
import sys
import numpy
import numpy
import boto3
import requests
def handler(event, context):
url = "https://www.therevisedcontext.com/"
r = requests.get(url, allow_redirects=True)
return 'response status:' + str(r) + '\n Hello from AWS Lambda using Python' + sys.version + '!'
import requests
def handler(event, context):
url = "https://www.therevisedcontext.com/"
r = requests.get(url, allow_redirects=True)
return 'response status:' + str(r) + '\n Hello from AWS Lambda using Python' + sys.version + '!'
iv. Build Docker image for the code written. Run the command on your terminal inside the main project folder.
Values in color denote the ARN of ECR repository and Docker Image tag created above.
Replace them to your values.
$ docker build -t 12345320323.dkr.ecr.eu-central-1.amazonaws.com/lambda-trc-docker-images:hello_lambda-vrn1 .
Once the build is complete, check if the docker image is created.
$ docker images
Note: These commands in steps iv and v are also provided in ECR View Push Commands.
Now, go to your AWS ECR service page and you should now see the new Image in your repository that you pushed.
Step 3 : Create a Lambda function with Container Image.
Go to AWS home page > Services > Compute > Lambda.
Create a new Lambda Function, select Container image option. Provide function name, ECR repository URI (select from Browse Image list) and new basic Lambda permissions Role.
Finally, its time to test your work. Test your code and you can see the logs under Cloudwatch.
Step 4 : Update the Code for any changes.
Make changes in your files that are required. Build your code using docker commands and push the Image with new tag to the ECR (step 2).
Once the Image is pushed to ECR successfully, Select the Deploy New Image option on Lambda under Image section.
You can also run the below command to update the Lambda Function with new Docker Image from ECR.
Replace the Lambda Function name, ECR repository ARN and Docker Image unique Tag in the below command.
$ aws lambda update-function-code --function-name Lambda_TRC_Docker_Container_POC --image-uri 12345320323.dkr.ecr.eu-central-1.amazonaws.com/lambda-trc-docker-images:hello_lambda-vr2
Thats all, You did a great job !!
If you face any issues of need any suggestions, please comment down below and hit the like button to appreciate the efforts.
Further readings,
You can also read Lambda Function to track Open Security Groups in AWS.
Comments
Post a Comment