Run a node app into AWS lambda

I create this post because I didn’t found any information on this when I need it, or the information was disperse around the web, so I hope this post help someone. This is going to be straight and I’m assuming that you already have a private repository…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Federico Roman

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

Steps

  • Create your code and ensure that a handler exist.
  • Setup & upload your Dockerfile with the right params.
  • Configuring AWS.
  • 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 verify, you can find the supported versions here).

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

export.handler = async (even) => {
 ... some awesome logic
}

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 . .

RUN npm install --omit=dev

ENV MODE="production"
CMD node -e 'require("./index").handler()' MODE=${MODE}

In this case we're using node:14.20 as image (notice the repository), we install all the packages ignoring devs and in the CMD we call the function handler.

Then build the container.
docker build <name>:<tag> .

Push the files to your AWS private repository.

Lambda is going to run this every time that is being invoke. 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 you repository, set the right permissions. Once finish the process try to invoke the test, you can set an API Gateway layer so your function can be call from an endpoint.

Some other helpful resources:


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Federico Roman


Print Share Comment Cite Upload Translate Updates
APA

Federico Roman | Sciencx (2022-09-06T00:41:56+00:00) Run a node app into AWS lambda. Retrieved from https://www.scien.cx/2022/09/06/run-a-node-app-into-aws-lambda/

MLA
" » Run a node app into AWS lambda." Federico Roman | Sciencx - Tuesday September 6, 2022, https://www.scien.cx/2022/09/06/run-a-node-app-into-aws-lambda/
HARVARD
Federico Roman | Sciencx Tuesday September 6, 2022 » Run a node app into AWS lambda., viewed ,<https://www.scien.cx/2022/09/06/run-a-node-app-into-aws-lambda/>
VANCOUVER
Federico Roman | Sciencx - » Run a node app into AWS lambda. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/06/run-a-node-app-into-aws-lambda/
CHICAGO
" » Run a node app into AWS lambda." Federico Roman | Sciencx - Accessed . https://www.scien.cx/2022/09/06/run-a-node-app-into-aws-lambda/
IEEE
" » Run a node app into AWS lambda." Federico Roman | Sciencx [Online]. Available: https://www.scien.cx/2022/09/06/run-a-node-app-into-aws-lambda/. [Accessed: ]
rf:citation
» Run a node app into AWS lambda | Federico Roman | Sciencx | https://www.scien.cx/2022/09/06/run-a-node-app-into-aws-lambda/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.