The world’s leading publication for data science, AI, and ML professionals.

Building AWS Lambda Container Images

Packing your Lambda code and dependencies

Image from Unsplash by Frank Mckenna
Image from Unsplash by Frank Mckenna

AWS Lambda functions are incredibly powerful and one of the core building blocks for serverless applications. They can be used to stitch together your entire application and often times on Lambda functions you will need to install additional dependencies for your project. Certain packages/libraries are not supported out of the box with Lambda and you need to find a way to install these dependencies in that environment.

One method around this is creating a deployment package/zip file that contains your function’s dependencies. This could work, but a much cleaner and intuitive, alternative is packaging all of this up in a container image. We can use AWS provided base images to further streamline this process. In this article, we’ll look at taking one of these base images and building off of it to install a package for our Python Lambda function. We’ll build a simple NumPy example that you can adjust for your own example.

NOTE: For those of you new to AWS, make sure you make an account at the following link if you want to follow along. Before getting started, make sure to also have Docker installed. This article will also assume basic familiarity with Python and AWS.

1. Setup

For our example we need three main files: Lambda Python Function, requirements file, and our Dockerfile.

Files Needed (Screenshot by Author)
Files Needed (Screenshot by Author)

For our requirements.txt all we need is the NumPy package. We’ll be performing a simple NumPy operation with the sample payload we pass in.

Next up we have our Python Lambda function. In this Lambda function we’ll set it up to take a sample JSON with an integer as the input. Using NumPy we’ll capture the square root of this integer and return it when the Lambda function is invoked.

In reality your problem’s use case will be much more complex, we just take a simple NumPy operation here to show that our Lambda function is working with our additional dependencies.

We can capture our code and dependencies in a Dockerfile. First we grab the AWS base Python image.

The base image provides an environment variable in LAMBDA_TASK_ROOT. We can copy our Python code and install our dependencies in this directory.

We then set the CMD to our handler function, this should be . with your respective values.

2. Container Image Build

Using our Dockerfile we can build our Lambda container image "numpy-lambda-container" with the following command.

We then start the image using the Docker run command.

Now open another terminal in the same directory, we can curl a sample data point to invoke our Lambda function locally.

We then see the result in our other Shell.

Sample Result (Screenshot by Author)
Sample Result (Screenshot by Author)

As next steps, you can now push this local image to the Elastic Container Registry (ECR). To push all of these resources to AWS you can use a AWS CloudFormation/SAM template to define and deploy your infrastructure.

3. Additional Resources & Conclusion

GitHub – RamVegiraju/Lambda-Container: Example of creating a Lambda container image

For the code for the example check out the link above. Lambda container images make it easier than ever to customize your Lambda functions to suit your workflow. This was a very simple use case that you can use as a basis to build your own complex functions. If you want to see how to deploy ML models for inference on Lambda check out this AWS samples repository. Here is another great example of deploying a BERT model on AWS Lambda for serverless inference.

I hope this article was a good introduction to utilizing one of the many features of AWS Lambda. As always feel free to leave any questions and feedback in the comments.


If you enjoyed this article feel free to connect with me on LinkedIn and subscribe to my Medium Newsletter. If you’re new to Medium, sign up using my Membership Referral.


Related Articles