Streamlining AWS Lambda Functions with Docker

Streamlining AWS Lambda Functions with Docker

Introduction

In computer stuff where you don't need to worry about managing servers, AWS Lambda is super strong. But to make it work really well, you often have to package your code in a certain way that Lambda likes. That's where Docker comes in. Docker is a tool that makes it easy to put your code into little packages that can be moved around easily. When you use Docker with Lambda, you can do even more cool things. You can put your functions into these little packages and deploy them smoothly. In this guide, we'll show you how to make a Lambda function using Docker, so you can take advantage of all the good stuff it brings.

Note:

If you are getting permission denied error while executing docker commands then use sudo (root user) before docker command.

Step-1: Organizing Your Project

First things first, let's structure our Python project in a way that facilitates easy containerization:

my-lambda-function/
├── data_handler/
│   ├── __init__.py
│   └── data_handler.py
├── utils/
│   ├── __init__.py
│   └── helper.py
├── api_handler/
│   ├── __init__.py
│   └── api_handler.py
└── app.py

Ensure your app.py script contains the Lambda handler function and imports necessary modules. You can create your own packages instead of data handler, utils and api handler to write your own business logic

import data_handler
import utils
import api_handler

def handler(event, context):
    return {
        "status_code": 200,
        "body" : {"success": True}
    }

Step-2: Crafting the Dockerfile

Next, we'll create a Dockerfile at the project's root to define the environment for our Lambda function:

Dockerfile:

FROM public.ecr.aws/lambda/python:3.8

COPY . /var/task

RUN pip install -r requirements.txt

Step-3: Building Docker image

With our Dockerfile in place, it's time to build the Docker image that encapsulates our Lambda function:

docker build -t my-lambda-function .

Step 4: Local Testing (Optional)

Before we deploy to the cloud, let's ensure everything works smoothly by testing our Docker image locally:

docker run -p 9000:8080 my-lambda-function:latest

Step 5: Pushing to Amazon ECR

Now, let's push our Docker image to Amazon Elastic Container Registry (ECR) for seamless deployment to Lambda:

aws ecr get-login-password --region ap-south-1 | sudo docker login --username AWS --password-stdin <ACCOUNT_ID>.dkr.ecr.ap-south-1.amazonaws.com
aws ecr create-repository --repository-name my-lambda-function

By using above command my-lambda-function repository will get create in your AWS ECR service

Use below commands to tag and push your docker image my-lambda-function with latest tag. Replace ECR_REPO_URI with the URI which is shown in above table (your AWS account)

docker tag my-lambda-function:latest <ECR_REPO_URI>/my-lambda-function:latest
docker push <ECR_REPO_URI>/my-lambda-function:latest

Step 6: Creating the Lambda Function

Navigate to the AWS Lambda console and create a new function, selecting "Container image" as the runtime and linking it to the ECR repository.

After creation of Lambda function go to Image configuration edit and change CMD to app.handler

Step 7: Configuring Lambda

While setting up the Lambda function, fine-tune memory and timeout to match your requirements.

If you need more information about Lambda configuration then you can refer my previous article Building-efficient-aws-lambda-functions.

Step 8: Testing and Beyond

Once deployed, test your Lambda function using either the AWS Lambda console or AWS CLI, ensuring smooth execution.

Key Considerations:

As you embark on your Lambda journey, keep these considerations in mind for optimal performance and security:

  • Memory and Timeout

  • Concurrency

  • Monitoring and Logging

  • Security

  • Error Handling

  • Versioning and Aliases

Conclusion

In conclusion, leveraging Docker images for AWS Lambda functions presents a transformative approach, surpassing traditional methods like zipping code. Docker's encapsulation of code, dependencies, and runtime environment streamlines deployment, ensuring consistency across environments and simplifying version control. With Docker, Lambda functions benefit from enhanced portability, rapid scaling, and optimized resource utilization. Moreover, Docker containers offer security through isolation and vulnerability scanning.