How to create an AWS Lambda Node.js container image with dependencies?

Xolile SpeelmanXolile Speelman
4 min read

Description

In this article we will be going through the steps of packaging our Lambda function Node.js code and dependencies into a container image. For illustration purposes we will be using Lodash library as our dependency for our Lambda function code. Lodash is a JavaScript library which provides utility functions for common programming tasks such as working with working with arrays, numbers, objects, strings, etc.

How to create Node.Js Container

  1. On your local machine, create one folder with three files: Dockerfile, package.json with libraries, and index.js with require() statements.
    Note : Ensure you already have Docker platform installed on your local machine.

  2. Firstly we need to create a Dockerfile. The Dockerfile will be using the Node.js 18 lambda base image.

    This example Dockerfile uses Node.js 18:

FROM public.ecr.aws/lambda/nodejs:18 

#Assumes your function is named "index.js", and there is a "package.json file" in the directory you've stored you're files in.
#The COPY command copies the "index.js" and  "package.json" file to the  ${LAMBDA_TASK_ROOT} directory also know as the var/task/ directory.  
COPY index.js package.json ${LAMBDA_TASK_ROOT}/  

#Install NPM dependencies for function
#The RUN command runs the provided command to install all the dependencies mentioned in the package.json file.
RUN npm install

#Set the CMD to your handler
 CMD [ "index.handler" ]

Install any dependencies under the ${LAMBDA_TASK_ROOT} directory alongside the function handler to ensure that the Lambda runtime can locate them when the function is invoked. The Lambda runtime for Node.Js will look for your dependencies in the "node_modules" folder present in your var/task/ directory. This folder should contain all the dependencies that are required by your Lambda function to execute successfully. The "node_modules" folder is created when you run the "npm install" command in your lambda container var/task/ directory. which downloads and installs all the dependencies specified in the "package.json" file

This example index.js file includes an example require() statement:

const lodash = require('lodash');

const numbers = [2, 5, 7, 10];

exports.handler = async (event) => {
    console.log('event', event);

 // Get the sum of array elements
   const sum = lodash.sum(numbers);
   console.log(sum); // 24
   const responseBody = `Result is ${sum.toString()}`;

    const response = {
        statusCode: 200,
        body: responseBody
    };
    return response;
};

This basic package.json file showcases our lodash library as a dependency for our Node.js function.

{
  "dependencies": {
    "lodash": "^4.17.21"
  }
}

3. Build your Docker image with the docker build command and an image name.

This is a docker build command with a “hello-world” example:

docker build -t hello-world .

4. Start the Docker image with the docker run command.

This is a docker run command with a “hello-world” example:

docker run -p 9000:8080 hello-world

5. Test your application by using the Lambda Runtime Interface Emulator (RIE). From a new terminal window, post an event to the following endpoint using a curl command:

curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'

This command invokes the function running in the container image and then returns a response. In our example the response will be {"statusCode":200,"body":"Result is 24"}

6. Authenticate the Docker CLI to your Amazon ECR registry. Change the account ID and the AWS Region in the command to make sure that it meets your requirements.

This is an example of authenticating the Docker CLI to the Amazon ECR registry:

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com

7. Create a repository in Amazon ECR using the create-repository command.

This is an example create-repository command:

aws ecr create-repository --repository-name hello-world --image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE

8. Tag your image to match your repository name using the docker tag command. Then, deploy the image to Amazon ECR using the docker push command.

Change the account ID and the AWS Region in these commands to ensure that they meet your requirements.

This is an example docker tag command:

docker tag hello-world:latest 123456789012.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest

This is an example docker push command:

docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest

9. With your container image in the Amazon ECR container registry, create and run a Lambda function. For more information, see Creating the function.

0
Subscribe to my newsletter

Read articles from Xolile Speelman directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Xolile Speelman
Xolile Speelman

I'm an AWS Cloud Support Engineer with solid experience in cloud infrastructure architecture, troubleshooting, and problem solving within the AWS cloud platform. I have comprehensive understanding of serverless architectures, and used that understanding to create scalable and cost-effective serverless applications.