DEV Community

Federico Roman
Federico Roman

Posted on

Run a node app into AWS lambda

I create this post because I didn't find any information on this when I needed it, or the information was dispersed around the web, so I hope this post helps someone. This is going to be straight and I'm assuming that you already have a private repository in AWS.

Steps

  1. Create your code and ensure that a handler exists.
  2. Setup & upload your Dockerfile with the right parameters.
  3. Configuring AWS.
  4. Close the rest of your browser tabs.

If you are reading this before you write any code make sure to check the repository of AWS and confirm the node version (Some of the node versions are not verified, you can find the supported versions here).

Once you have your Node JS code you need to make sure that inside your main file exists a handler function this is a requisite for lambda functions no matter the programming language.

const https = require('https')
let url = "https://docs.aws.amazon.com/lambda/latest/dg/welcome.html" 

export.handler = async (event) => {

  const promise = new Promise((resolve, reject) => {
    https.get(url, (res) => {
        resolve(res.statusCode)
      }).on('error', (e) => {
        reject(Error(e))
      })
    })

  return promise

}
Enter fullscreen mode Exit fullscreen mode

After you have all your logic we are going to create a container with docker. Your Dockerfile should be similar to this:

FROM public.ecr.aws/bitnami/node:14.20.0

COPY ./source .

RUN npm install --omit=dev


CMD [ "app.handler" ] 
Enter fullscreen mode Exit fullscreen mode

In this case, we're using node:14.20 as an image (notice the repository), we install all the packages ignoring devs dependencies (we don't need linters or other tools into the container) and in the CMD we call the function handler.

For testing purpose you can also use node -e 'require("./index").handler()' in CMD.

Then build the container.

docker build -t <name>:<tag> .
Enter fullscreen mode Exit fullscreen mode

Push the files to your AWS private repository using the CLI of AWS (be aware that the following are just dummy data, you need to replace them with your values).

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com   
Enter fullscreen mode Exit fullscreen mode
docker tag  <name>:<tag> 123456789012.dkr.ecr.us-east-1.amazonaws.com/<name>:<tag>
docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/<name>:<tag>
Enter fullscreen mode Exit fullscreen mode

Lambda is going to run this every time that is being invoked. Now you need to create a repository inside AWS.

After you push your files go into lambda and select create a lambda container function, once there just pick the container from your repository, and set the correct permissions. Once finish the process try to invoke the test, you can place an API Gateway layer so your function can be called from an endpoint.

Some other helpful resources:

AWS Node Lambda container documentation.
Sample of the code inside my personal repo
This one is a video tutorial for python

Top comments (0)