What is AWS Lambda Container Image?

What is AWS Lambda Container Image along with an example to help understand the workflow

Oussema Miled
AWS in Plain English

--

Prerequisites

  • Basic understanding of containers
  • An AWS account
  • Basic Knowledge of AWS such as the console and roles

What is Lambda Container Image?

Before we dive into Lambda Container Image (Let’s refer to it with LCI from now on), we need to understand Lambda service first. So what is it?

Lambda is a serverless computing service provided by AWS which we can use to run functions, self-contained applications written in one of the supported languages (Node.js, Python, Go, Java, and more). Serverless doesn’t mean that there are no servers but it means that developers can write and execute code without worrying about administrative tasks such as maintaining servers.

It’s also important to understand that each Lambda function runs in its own container. So when we create a function, Lambda will package it in a container and then run that container in a cluster.

Now with LCI, we can package our Lambda function and the needed dependencies in an image using Docker CLI for example. Then we upload that image to AWS ECR (Elastic Container Registry). After that, we can go ahead and deploy the Lambda function based on the image we’ve created.

You can learn more about details such as the requirements from the official documentation. But for now, enough of theories, let’s actually build something!

LCI Example

Let’s build a pretty simple LCI, it takes a name as payload and prints Hi + name . We’re going to use Node.js to write the code and Docker to build the image.

In the same directory let’s create two files, one for our code app.js and Dockerfile for the image.

app.js

The above code is the Lambda handler which will be executed when we invoke the function.

Dockerfile

Here we use the lambda-node base image from AWS, we copy the file containing the Lambda handler app.js in the container and finally run it.

To build the image, run:

docker build -t example .

Now, we need to create an ECR repo or use an existing one to host our image. To do so, we open the AWS console and visit the ECR service, you can follow this link for quick access. Choose the region you want, click on Create repository button, name your repo and leave the rest as it is then click the Create repository button. To finish, we need to push our image to this repo. Just follow the steps provided when you click View push commands button.

Let’s now create the Lambda function, keep in mind that the Lambda function and the ECR repo should be created in the same AWS account.

Visit the Lambda service and click Create function , choose Container image option, give it a name, browse images and select the one we just created, finally hit Create function button.

Now we are ready to go and test our work. In the Test tab, write the event which will be sent to the Lambda and click the Test button.

Creating Test Event

Here we go, The Lambda works as intended. Good job my friend 🙌

Execution Result

You can also check the CloudWatch logs to see the executed steps and some details by clicking on logs link.

That’s it for today, I wanted to keep it as straightforward as possible. I hope you liked it, feel free to ask any question in the comments section.

Thanks! 😄

PS: If you like what I do and want to support me, you can do that by becoming a medium member using this link

--

--

A Computer Science Engineer who loves to talk about Web Development and sometimes other stuff!