Using AWS Lambda functions on a Node.js app


Using AWS Lambda functions on a Node.js app

Permalink to "Using AWS Lambda functions on a Node.js app"

Lambda is a serverless, event-driven compute service that lets you run code for virtually without provisioning or managing servers.

Lambda runs your code on a high-availability compute infrastructure and performs all of the administration of the compute resources, including server and operating system maintenance, capacity provisioning and automatic scaling, code monitoring and logging. You can trigger Lambda from over 200 AWS services and software as a service (SaaS) applications, and only pay for what you use.

Your code can be organized into Lambda functions. AWS Lambda invokes your code only when needed, and automatically scales to support the rate of incoming requests without any manual configuration. There is no limit to the number of requests your code can handle. AWS Lambda optimizes code execution time and performance with the right function memory size. So if the Lambda functions are not running, there is no charge, you only pay for the compute time that you consume. You can invoke your Lambda functions using the Lambda API, or Lambda can run your functions in response to events from other AWS services.

Three components that comprise AWS Lambda are:

  • A function. This is the actual code that performs the task
  • A configuration. This specifies how your function is executed.
  • An event source (optional). This is the event that triggers the function. You can trigger with several AWS services or a third-party service.

You can use AWS Lambda to create any type of application or new backend service, triggered on demand using the Lambda application programming interface (API) or custom API endpoints built using Amazon API Gateway. Lambda processes custom events instead of servicing these on the client, helping you avoid client platform variations, reduce battery drain, and enable easier updates.

Creating Lambda functions on AWS

Permalink to "Creating Lambda functions on AWS"

You can run JavaScript code with Node.js in AWS Lambda. The code runs in an environment that includes the AWS SDK for JavaScript, with credentials from an AWS Identity and Access Management (IAM) role that you manage. To create the Lambda functions directly on AWS, an execution role is needed, to get permission to access AWS services and write logs to Amazon CloudWatch. If we still don’t have it, first thing we do, is to create an execution role.

To create an execution role:

Permalink to "To create an execution role:"
  1. On the IAM console, we should open the role page
  2. Then we choose Create role.

Image 31.5.22 at 9.23 AM.JPG

  1. The role should be created with the following properties:
  • Trusted entity - Lambda
  • Permissions - AWSLambdaBasicExecutionRole
  • Role name - lambda-role

After we choose to create a new role, on the Select trusted entity, we choose Lambda as a Use case.

Image 31.5.22 at 1.15 AM.JPG

Then on the Add permissions, we choose AWSLambdaBasicExecutionRole. This one has the permissions that the function needs to write logs to CloudWatch Logs.

Untitled

On the role details, we write role name:

Image 31.5.22 at 1.17 AM.JPG

When we complete this step, the role is created. After we created this one, we can start creating a Lambda function.

To create a Node.js function on the AWS side

  • First we should search for Lambda on AWS services

Image 31.5.22 at 12.37 AM.JPG

  • Then we should choose Create function

Untitled

  • To create a new function we should configure those settings:
  1. Name – my-function
  2. Runtime – Node.js 16.x
  3. Role – Choose an existing role.
  4. Existing role – lambda-role

Image 31.5.22 at 1.20 AM.JPG

After this step, the function is created:

Image 31.5.22 at 1.11 AM.JPG

  • We can write some code here finally.

Untitled

  • If we want to test the Lambda function, we should choose Test
  • For Event name, enter test
  • To invoke the function, choose Test

Here we created a Lambda function, with a single file named index.js. This file exports a function named handler. The handler accepts a promise value. This is why we can now assign an async function to the handler, and return a promise directly. When the function is invoked, Lambda calls this function.

When we save our function code, the Lambda console created a .zip file archive deployment package.

An event triggers every function invocation. To invoke our function we first need to jump over to API Gateway in the AWS console. Then we will choose to create a Rest API, which will lead us to this page below, where we can

Untitled

After clicking Create Api button, the API will be created. On Actions dropdown, we will choose Create a Method, and will add a Get method, in which we will choose our Lambda function that we created earlier: helloWorld

Untitled

After saving it, we will get this message, that we can see on the image below:

Untitled

Here on the Method Request, we can see the url that we can use to call this Lambda function, after we deploy the API.

Image 31.5.22 at 10.09 AM.JPG

If we click Deploy API, we will have to add the stage name and other infos. After deploying, we get a url for that service, which we can use as a prefix when calling the functions.

Untitled

Creating Lambda functions on the Node.js app

Permalink to "Creating Lambda functions on the Node.js app"

We can also create Lambda functions directly from code. First we have to install AWS CLI and Serverless package: npm i serverless.

After this on the terminal we configure our AWS access key and secret key. If we want to create a new serverless service we should run this command, where lambda-nodejs-example is name of the function:

serverless create --template aws-nodejs --name lambda-nodejs-example

To create a new Lambda function, we will need two files on this service:

  • serverless.yaml: Defines the AWS resources for your application.
  • hello-world/app.js: Contains the Lambda function logic.

On the serverless.yaml file we can find the function declaration:

                      Type: Api
                      Properties:    Path: /hello
                                      Method: get

This indicates that the Lambda function will execute when a GET request is sent to the /hello route via Api Gateway.

Then on the hello-world/app.js file we can add the Lambda function implementation.

Image 31.5.22 at 12.35 PM.JPG

Then you can test this function locally by running on the terminal:

serverless invoke local --function "lambdaHandler"

If your function works good then you can deploy it with the command: serverless deploy.