how to deploy a docker container on aws lambda

AWS Lambda is easy to use and manage because it has an execution environment with a specific runtime on a known environment. The problems come in when you have a use case outside these predetermined environments.

To address these issues AWS introduced…

AWS Lambda is easy to use and manage because it has an execution environment with a specific runtime on a known environment. The problems come in when you have a use case outside these predetermined environments.

To address these issues AWS introduced Lambda layers, but Lambda layers still had limitations. In December 2020, AWS Lambda released some basic Docker container support. The Serverless framework has expanded on this with the following example designed to make it easier to use this new feature.



Create template with serverless create

We will use the added starter template to generate a boilerplate with some basic setup already configured in our serverless.yml.

serverless create --template aws-nodejs-docker --path ajcwebdev-docker-lambda



serverless.yml

The Serverless Framework lets you define a Dockerfile, point at it in your serverless.yml and have the Framework do all the work of making sure the container is available in ECR and that it is all setup and configured as needed with Lambda.

service: ajcwebdev-docker-lambda
frameworkVersion: '2'

Under provider is an ecr section for defining images that will be built locally and uploaded to ECR. The entire packaging process now occurs in the context of the container. AWS uses your Docker configuration to build, optimize and prepare your container for use in Lambda.

provider:
  name: aws
  lambdaHashingVersion: 20201221
  ecr:
    images:
      appimage:
        path: ./

The functions section tells the framework what the image reference name is (appimage) that we can use elsewhere in our configuration, and where the content of the docker image resides with the path property. A Dockerfile of some type should reside in the specified folder containing the executable code for our function.

functions:
  hello:
    image:
      name: appimage

We use the same value for image.name as we do for the image we defined, appimage. It can be anything you want as long as you use the same value to reference it.



dockerfile

The easiest way to setup a Lambda ready Docker image is to rely on base images provided by AWS. The AWS ECR Gallery contains a list of all available images. We are using a Node v12 image.

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

The CMD property defines a file called app.js with a function called handler.



app.js

In our service’s directory, we have a file called app.js with the function inside.

'use strict'

module.exports.handler = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: `Cause I don't want a server, but I do still want a container`,
      },
      null,
      2
    ),
  }
}

app.js returns a JSON object containing a message clarifying exactly why anyone would ever want this thing in the first place.



Deploy with serverless deploy

We are now able to generate our container, deploy it to ECR and execute functions. In order to build images locally and push them to ECR, you need to have Docker installed and running on your local machine.

cd ajcwebdev-docker-lambda
serverless deploy

The serverless deploy command deploys your entire service via CloudFormation. After the deployment finishes you will be provided the information for your service.

Service Information

service: ajcwebdev-docker-lambda
stage: dev
region: us-east-1
stack: ajcwebdev-docker-lambda-dev
resources: 6
api keys:
  None
endpoints:
  None
functions:
  hello: ajcwebdev-docker-lambda-dev-hello
layers:
  None



Invoke the hello function with serverless invoke

The serverless invoke command invokes deployed function and allows sending event data to the function, reading logs and displaying other important information about the function invocation.

serverless invoke --function hello

If your function/container was deployed correctly then you will receive the following message:

{
  "statusCode": 200,
  "body": "{\n  \"message\": \"Cause I don't want a server, but I do still want a container\"\n}"
}

Print Share Comment Cite Upload Translate
APA
anthony-campolo | Sciencx (2024-03-29T11:20:49+00:00) » how to deploy a docker container on aws lambda. Retrieved from https://www.scien.cx/2021/09/02/how-to-deploy-a-docker-container-on-aws-lambda/.
MLA
" » how to deploy a docker container on aws lambda." anthony-campolo | Sciencx - Thursday September 2, 2021, https://www.scien.cx/2021/09/02/how-to-deploy-a-docker-container-on-aws-lambda/
HARVARD
anthony-campolo | Sciencx Thursday September 2, 2021 » how to deploy a docker container on aws lambda., viewed 2024-03-29T11:20:49+00:00,<https://www.scien.cx/2021/09/02/how-to-deploy-a-docker-container-on-aws-lambda/>
VANCOUVER
anthony-campolo | Sciencx - » how to deploy a docker container on aws lambda. [Internet]. [Accessed 2024-03-29T11:20:49+00:00]. Available from: https://www.scien.cx/2021/09/02/how-to-deploy-a-docker-container-on-aws-lambda/
CHICAGO
" » how to deploy a docker container on aws lambda." anthony-campolo | Sciencx - Accessed 2024-03-29T11:20:49+00:00. https://www.scien.cx/2021/09/02/how-to-deploy-a-docker-container-on-aws-lambda/
IEEE
" » how to deploy a docker container on aws lambda." anthony-campolo | Sciencx [Online]. Available: https://www.scien.cx/2021/09/02/how-to-deploy-a-docker-container-on-aws-lambda/. [Accessed: 2024-03-29T11:20:49+00:00]
rf:citation
» how to deploy a docker container on aws lambda | anthony-campolo | Sciencx | https://www.scien.cx/2021/09/02/how-to-deploy-a-docker-container-on-aws-lambda/ | 2024-03-29T11:20:49+00:00
https://github.com/addpipe/simple-recorderjs-demo