Setting Up a Lambda Function With a Docker Image

Kyle Rood
Resultid Blog
Published in
7 min readJul 27, 2021

If you’re like me, then you saw the AWS dashboard for the first time and had a full-blown panic attack.

Is there a difference between CodePipeline and CodeDeploy? What’s an EC2 instance?

So many buttons, so many services, how do you make use of it all?

This article may not be able to answer that monster of a question, but I will attempt to show you how to easily set up a Lambda function that uses a Docker image as the deployment strategy.

AWS Lambda functions are useful because we can put one set of code into a function, and then we can hit that function with requests as many times as we want. AWS will automatically scale the requests for us, which means our site will not lag for requests, no matter how many requests come in. Eery busy site needs this; this is the ideal middleground between cost and performance that allows a small startup –like us at Resultid– to handle the same volume of requests as a large scale application.

Docker is really advantageous because everything that your program will need (from Environment Variables to downloads) is contained within the Docker image. This makes deployment straightforward, and keeps everything contained within one environment. As compared to doing this in a typical deployment by just uploading Python or Java code, Docker can make use of the command line inside of the Lambda to do some really powerful things. Docker makes it easy to rebuild an entire environment around an application. This makes it easy to deploy the first time, and every time after that, as it’s just a matter of uploading a new image to AWS.

The first part of this tutorial will be getting the Docker image set up and pushed to AWS. The second half will be about getting the Lambda set up in AWS, and making sure that we also set up some of the load balancing tools.

In this how-to, I will reference and use the code located in this github repository. Clone the repository, and follow along with these steps to set up your very own “test-lambda” function!

Step 1: Setup the Docker Image

If you have never used Docker before, go over to this link to download Docker Desktop. This will download the necessary tools you need to build your image. The “Dockerfile” in the repository which you cloned contains the necessary code to build the image with the right specifications.

If you clone the repository, you should be able to build the image with:

docker build -t test-lambda

Setting up your own Docker Image is not easy, but the Dockerfile in this repo is very simple, and only installs the bare-bone specifications needed. This is a good basis for a Dockerfile, and you can do more research (or use this Docker cheatsheet) if you would like to add further customizations.

Step 2: Setup AWS Infrastructure

First, we need to set up an AWS ECR instance to push our image to. If you go to ECR and click “Create Repository”, you will be brought to this screen.

The above settings are all the default settings except the name and the “Scan on Push” function. Just change the Lambda name to be whatever you are naming your own version, and you will be in business.

Step 3: Push Image to AWS

Once we have created our ECR instance to push our images to, we can push our newly created Docker image to the server.

To do this, we first need to log into AWS:

#windows powershell

(Get-ECRLoginCommand).Password | docker login — username AWS — password-stdin <AWS USER ECR URL>

#max/linux

aws ecr get-login-password — region <REGION> | sudo docker login — username AWS — password-stdin <AWS USER ECR URL>

These commands will log you into the AWS ERC database, and this will allow you to actually tag and push your image to an ECR instance. To push to an instance correctly, you need to build the image, tag it correctly, and then push. These commands are below and should be modified based on your URL and Lambda name:

docker build -t <LAMBDA NAME> .

docker tag <LAMBDA NAME> <AWS USER ECR URL>/<LAMBDA NAME>:prod

docker push <AWS USER ECR URL>/<LAMBDA NAME>:prod

These commands are contained within the file build_and_upload.sh, and this can be easily converted into a shell script. This makes it really easy to rebuild the image and upload it all in one go.

Once we push to the ECR instance, we should be able to see it showing up, with the correct tag that we assigned (in this case, prod).

Now that we have our Docker image uploaded; copy that image URL and head over to Lambda.

Step 4: Create a Lambda Function

Go to Lambda in AWS and press the “Create Function” button.

From here, create the Lambda from a container image. This will allow you to then pick the image you uploaded to ECR.

Once you press create function, you should see the main screen of your Lambda function, like below:

Step 5: Adding API Gateway and Load Balancer

Voila! You have a Lambda function. Now we need to enable the ability to hit it with requests from an external point, and this is done by adding an API Gateway and Load Balancer. The API Gateway allows for external requests to come in, and the load balancer is built on top of this to automatically scale the number of Lambdas being deployed. These two elements are critical to making the Lambda function correctly, and as efficiently as possible.

On the main page of your Lambda, click the “Add Trigger” button in the “Function Overview” section.

Select API trigger from the dropdown and create a new API. The only section which matters to us is selecting “REST API” in the API type section.

Once you add the API Gateway, you just need to add the load balancer. You will need to click the “create a new Application Load Balancer”.

This next part will be a series of screenshots showing the different settings applied for creating the load balancer. Try to not change too many values, and if you do, only adjust the things which are truly necessary. AWS gives you tons of customization, but sometimes checking the wrong button can set something up in an entirely different way.

The steps:

Just add the name, and select the availability zones you would like to use

Make sure to add two Availability Zones, or it will yell at you!

Select security group, or create one if none exist (not showing mine that do exist)

Connecting the Load Balancer to the lambda that we have set up:

Click through to the review page and check over the settings. If everything looks good, you can make your new load balancer!

Step 6: Let’s test it out…

Testing lambda inside of the AWS Dev environment:

This should simply return back to us the same JSON that we pass in (if you used the same Lambda function as me). The JSON does not matter, as long as it is formatted correctly. Below, you can see the result of a successful call.

When we are finished with this, your Lambda function homescreen should look like this:

This means we have set up the function, the API Gateway, and the Load Balancer. We have pointed our Lambda to an image, but let’s make sure it is the most up-to-date version.

Run a command to update the function and the image it points to:

aws lambda update-function-code — function-name <LAMBDA NAME> — image-uri <AWS USER ECR URL>/<LAMBDA NAME>:prod

There you have it! You now have a Lambda function that is capable of auto-scaling to the number of requests.

But, DON’T FORGET ABOUT ENVIRONMENTAL VARIABLES

Double check, Triple Check, Quadruple check that you have all the necessary environment variables, and that they are fully up to date. This has caused several afternoons to be wasted away checking logs only to discover the environment variable wasn’t there. It’s simple, but it could save you a lot of time.

--

--

Kyle Rood
Resultid Blog

Lead Developer at ResultID, MS in CS from GWU, Passionate about AI and making people smile (at least trying to)