Lambda functions challenge with nodejs

Lambda functions challenge with nodejs

Lambda functions, nodejs and github actions series

He didn’t have much understand of the serverless offering from cloud providers…

Those are the words from the feedback I got from my last interview as a Backend Developer (by the way, I was rejected). So, I've decided to turn this feedback into a challenge/opportunity to improve my knowledge about AWS.

First things first, if you are new on aws lambda and github actions, please read the key concepts from the introduction article.

Setup our cloud environment

User and group

Create a user for deploy and execution access. Select "Access key - Programmatic access" for AWS credential type (don't forget copy or download the user security credentials, we need it late)

Lambda function

In the AWS console, create a lambda function using a simple Hello World example.

For this very example we will use the default values except for information architecture, for those set arm64 value (Advantages of using arm64 architecture)

Right after that, create a trigger for add an API Gateway that route HTTP requests to our Lambda function:

In the next screen you can copy the API endpoint (we need it late). If you visit the endpoint you should get the following message:

"Hello from Lambda!"

Your lambda function should look like this:

GitHub Repository

For this very example, we are going to create a git repository in GitHub. Make sure the repository contains a master branch and your user (or personal access token) has permissions for run GitHub workflows.

Then, create the action secrets for AWS security credentials (Repository Settings -> Secrets and Variables -> Actions -> New repository secret):

  • AWS_ACCESS_KEY_ID: Access key ID

  • AWS_SECRET_ACCESS_KEY: Secret access key

  • AWS_REGION: us-east-1 (default AWS region, you can use a different value)

Project Files

Clone the project and create the files below:

index.js:

.gitignore:

main.yml:

Pipelines

We are going to create our pipeline using GithubActions because it is include in our git repository. Before start with our pipeline, we should know some definitions from the official GitHub documentation first:

Github Actions

For this project, we are going to use Github Actions with the following actions:

  • actions/checkout: This action checks-out your repository under $GITHUB_WORKSPACE, so your workflow can access it.

  • actions/setup-node: This action downloading and caching distribution of the requested Node.js version, and adding it to the PATH. Caching dependencies and configure authentication for npm.

  • aws-actions/configure-aws-credentials: Configure AWS credential and region environment variables for use in other GitHub Actions. The environment variables will be detected by both the AWS SDKs and the AWS CLI to determine the credentials and region to use for AWS API calls.

For update lambda function

In our "deploy" step we can create the zip file for upload and update our lambda function using the AWS Command Line Interface inside the runner:

Create a zip file:

zip -j deploy.zip ./app/*

Upload the zip file and update the function code:

aws lambda update-function-code --function-name=lambda-nodejs-github --zip-file=fileb://deploy.zip

Right after that, commit and push the changes to the repository. The pipeline starts automatically, and you can see the progress in the Actions option:

Last but no least, visit the API endpoint and see the new messages:

"Hello AWS lambda from my aws-lambda-nodejs-github"

Conclusion

You can get the source files from my GitHub repository

Now, the lambda functions are familiar to me, I have learnt about build a function, update the source code, permissions, serverless and GitHub actions.

Finally, I have found different ways to create a lambda function including AWS Console, AWS SAM CLI and Infrastructure as Code (IaC) while I researched information about Serverless and Lambda function. During the investigation I conducted, I made manual mistakes trying to create a solution that involves NodeJS code and pipeline for deploy. Therefore, I think that the IaC might be a good way to avoid the manual process.

Resources

https://aws.amazon.com/serverless/

https://aws.amazon.com/lambda/

https://aws.amazon.com/lambda/features/

https://docs.aws.amazon.com/lambda/latest/dg/lambda-permissions.html

https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started-hello-world.html

https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions

https://docs.github.com/en/actions/security-guides/encrypted-secrets

https://github.com/marketplace/actions/checkout

https://github.com/marketplace/actions/setup-node-js-environment

https://github.com/marketplace/actions/configure-aws-credentials-action-for-github-actions

https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html

Cover Image by storyset on Freepik

Did you find this article valuable?

Support Señor Developer by becoming a sponsor. Any amount is appreciated!