Beginner’s Guide to Running Docker Containers on AWS Lambda — Part 1

Eranda Kudalugodaarachchi
Towards AWS
Published in
4 min readJan 22, 2023

--

Introduction to AWS Lambda and Docker: What they are and why they’re useful.

AWS Lambda and Docker are two powerful technologies that can be used together to build and deploy scalable, serverless applications. Lambda is a powerful service provided by AWS which can be used to run backends without managing the servers. Lambda takes care of managing the execution environment, allowing developers to focus solely on the code they are writing. At the moment, AWS lambda supports a variety of runtimes, including Node.js, Python, Java, .NET, Go and Ruby. It also enables the creation of custom runtimes, extending its capabilities beyond its standard offerings. AWS Lambda allows two ways to deploy lambda functions. One way is to package the function as a .zip and deploy it. And the other way is to containerize the function and then deploy it.

Docker, on the other hand, is the most popular tool for creating containerized applications. It enables developers to package their application and its dependencies in a portable container that can then be run consistently across multiple environments. It also allows us to run our applications in a loosely isolated environment. But if you are familiar with AWS Lambda, you may be wondering why there is a need for an additional isolated environment, as lambda already provides one by default. This capability is advantageous in a range of use cases,

  1. If we want to create a custom runtime not supported by AWS lambda.
  2. If we want to have complete control over our execution environment for legal or regulatory reasons (Trusted Execution Environment, aka TEE).
  3. If we want to use custom libraries that cannot be used in lambda by default.

Setting up an AWS account

If you don’t already have an AWS account, you can create a free tier account here. This free tier gives access to a wide range of services, including compute, storage, database etc., for a limited period of 12 months. After ending this period, you’ll be charged according to the pricing models for each service you use.

Using Docker with AWS Lambda

As stated above, Docker enables developers to run their applications in isolated runtime. These isolated runtimes are known as containers. Once the containers are created, they are then published to a container registry, where they can be accessed and pulled for use in various environments.

There are a variety of popular container registries available, including Docker Hub, Amazon Elastic Container Registry, Azure Container Registry, IBM Cloud Container Registry, and Google Container Registry.

Since we’re working with AWS, we will use Amazon Elastic Container Registry(ECR). Initially, I will walk you through the process of getting a container up and running on AWS Lambda and then demonstrate how to do it.

Refer to the following image to view all the steps required to run a container on AWS Lambda.

Step 01: It is important to ensure that your application runs smoothly in a local environment and that the Dockerfile includes all the necessary commands before proceeding. Then you need to create a docker image using the application code and the Dockerfile. This can take a while, depending on your application.

Step 02: Once the Docker image is built, you must create a repository in AWS Elastic Container Registry (ECR) to store it. This can be done conveniently through either the AWS Command Line Interface (CLI) or the AWS Console. Once the repository is created, you need to publish the docker image. Docker CLI can be used to publish images to ECR. After the image is published to ECR, developers can pull it from the ECR.

Step 03: When the docker image is published to ECR, we can refer to it in our lambda functions. Then once these functions are called, it’ll pull up the docker containers from the ECR and create the execution environment to serve the request.

Creating and deploying a simple web application in a Docker container on AWS Lambda

Now Let’s demonstrate the usage of Docker on AWS Lambda by providing a hands-on example. This will allow us to showcase the steps required to utilize Docker containers within the Lambda environment and how they can aid in package management and dependencies. For this demo, we’ll be creating a file conversion lambda function. This function is able to convert files like .docx, doc, and .txt, .log into pdf files.

The following images show how our application is implemented. A lambda function is configured to listen to the “objectCreated” event on the S3 bucket named “convert-file-uploads”. Once a file is uploaded, AWS will load the docker container created using node.js and the libre office library. This library is responsible for converting files into .pdfs. (for the sake of simplicity in this tutorial, I’ll be using the AWS console to upload the images into the S3 buckets. But in real-world applications, we can use the AWS API gateway to upload the images to S3 using a POST request). Once the file is converted, it will be uploaded into another S3 bucket called “store-converted-files”

I’ve moved the tutorial here since this is a bit lengthy post.

--

--