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
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:
- AWS Node Handler
- Sample of the code inside my personal repo
- This is one is super useful