A developer explanation about container images in Lambda

Definition

Container image support for AWS Lambda was announced back at AWS re:Invent 2020.
Okay, but what exactly is container image support ?

To understand that, you need to have some context about lambda. When you create a Lambda function, you package your function code into a deployment package. Before the container image support, the only way to deploy your lambda code was through a zip file.

Lambda with zip file

For example, you had to create the actual code of your lambda , let's say in Node.js

// index.js
exports.handler =  async function(event, context) {
  console.log("Hello world")
}

To be able to upload your code, you must create a zip file

zip function.zip index.js

Then upload your package using the update-function-code command

aws lambda update-function-code --function-name my-function --zip-file fileb://function.zip

Lambda with container image

If you use Container image to deploy your function, after creating your image, you can use the Docker CLI build it and push it to AWS ECR (Elastic Container Registry), and then after that create your function from that image.

Let's create an image from the code i showed you above

FROM public.ecr.aws/lambda/nodejs:16
COPY index.js  ./
CMD [ "app.handler" ]

Then build that image

docker build -t lambda-container-image .

And finally, push it to ECR

  • Log in
    aws ecr get-login-password  --region <region> | docker login 
       --username AWS --password-stdin     
       <accountID>.dkr.ecr.<region>.amazonaws.com
    
  • Create the Repository
    aws ecr create-repository 
       --repository-name lambda-container-image 
       --image-tag-mutability IMMUTABLE
       --image-scanning-configuration scanOnPush=true
    
  • Tag the image
    docker tag lambda-container-image:v1 <accountID>.dkr.ecr.<region>.amazonaws.com/lambda-container-image:v1
    
  • Push the image
    docker push <accountID>.dkr.ecr.<region>.amazonaws.com/lambda-container-image:v1
    

And then, create your function from that image

Capture d’écran 2022-09-04 à 16.04.25.png

Conclusion

Now, you know the difference between the two types of deployment packages supported by Lambda.

If you want to know more, once again, check the Lambda Documentation

See you in the next one :)

Did you find this article valuable?

Support Sonia Manoubi by becoming a sponsor. Any amount is appreciated!