Skip to content

A Node.js Lambda function that uses a Docker container image

Notifications You must be signed in to change notification settings

danielhelen/nodejs-docker-lambda

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Node.js Docker Lambda

This uses the AWS-provided base image for Node.js to build a container image that will run a function in Lambda using the Docker runtime.

Local Development

The Lambda Runtime Interface Emulator is included with all AWS-provided base images. This provides a proxy for the Lambda Runtime API that allows you to run your Lambda function packaged as a container image locally.

Build the image:

docker build -t nodejs-docker-lambda .

Then run the container:

docker run -p 9000:8080 nodejs-docker-lambda:latest

(You can also use yarn start to run the above two commands.)

This runs a lightweight web server locally. Make the following HTTP request to invoke the Lambda function and observe the response:

curl -X POST -H "Content-Type: application/json" -d '{"input": "test"}' http://localhost:9000/2015-03-31/functions/function/invocations

Deployment

Deploying a container image is a bit fiddly. Here we use CloudFormation templates to create the AWS resources - you could also use the AWS Management Console or CLI. Ensure you have Docker Desktop and the AWS CLI installed.

  1. Create the AWS ECR private repository using the repository.json CloudFormation template.

  2. Create the IAM user using the user.json CloudFormation template and note the access key and secret access key available as outputs. Specify the above stack name as a parameter.

  3. Add a new profile to your AWS CLI:

    aws configure --profile nodejs-docker-lambda
    
  4. Build the image (you can also run yarn build-image):

    docker build -t nodejs-docker-lambda .
    
  5. Tag the image (specifiy your AWS account number and region):

    docker tag nodejs-docker-lambda:latest AWS_ACCOUNT_ID.dkr.ecr.AWS_REGION.amazonaws.com/nodejs-docker-lambda:latest
    
  6. Before you can push that tag you need to authenticate the Docker CLI to the Amazon ECR private repository (specify the AWS account number and region again):

    aws ecr get-login-password --profile nodejs-docker-lambda | docker login --username AWS --password-stdin AWS_ACCOUNT_ID.dkr.ecr.AWS_REGION.amazonaws.com
    
  7. Push the tagged image to the ECR repository (again specify your AWS account number and region):

    docker push AWS_ACCOUNT_ID.dkr.ecr.AWS_REGION.amazonaws.com/nodejs-docker-lambda:latest
    
  8. Create the Lambda function using the lambda.json CloudFormation template. Specify the URI of the image in ECR as a parameter and note the function name in the outputs.

  9. Invoke the Lambda function from your local machine (specify the function name) and observe the output response.json file:

    aws lambda invoke --profile nodejs-docker-lambda --function-name FUNCTION_NAME --cli-binary-format raw-in-base64-out --payload '{ "input": "test" }' response.json
    

About

A Node.js Lambda function that uses a Docker container image

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published