AWS Serverless | Deploy Lambda on Docker Container Image

 

AWS Serverless | Deploy Lambda on Docker Container Image

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.

Prerequisite

This post has been prepared for the audience who : 
  1. You should have an AWS account with access to create a Lambda Function, ECR and IAM Role.
  2. Have basic hands-on on Python Scripting language.
  3. Have worked on Docker and has basic knowledge of Dockerfile.
  4. 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.
AWS Serverless | Deploy Lambda on Docker Container Image
Create a new ECR repository with a unique name and some basic configurations.

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.
FROM public.ecr.aws/lambda/python:3.8
MAINTAINER The Revised Context
COPY hello_lambda.py ./
WORKDIR /tmp
RUN yum install tar xz unzip -y
COPY requirements.txt ./
RUN pip3 install -r requirements.txt
RUN chmod -R 777 ./
CMD [ "hello_lambda.handler" ]

ii.    Create a file with name requirements.txt and add all the required libraries on your python code.
boto3 numpy requests

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 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 + '!'

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
AWS Serverless | Deploy Lambda on Docker Container Image

v.    Push this Docker image to ECR with below command. Replace the ECR ARN and Docker Image tag with your values.
$ aws ecr get-login-password --region eu-central-1 | docker login --username AWS --password-stdin 12345320323.dkr.ecr.eu-central-1.amazonaws.com
$ docker push 12345320323.dkr.ecr.eu-central-1.amazonaws.com/lambda-trc-docker-images
:hello_lambda-vr1

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.
You can also provide Container image overrides like Docker ENTRYPOINT, CMD and WORKDIR in this step (optional).

AWS Serverless | Deploy Lambda on Docker Container Image

AWS Serverless | Deploy Lambda on Docker Container Image

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.
AWS Serverless | Deploy Lambda on Docker Container Image

From the list, select the desired Image.

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







Comments