AWS Lambda Nodejs: Simplified 101

By: Published: April 25, 2022

aws lambda nodejs: FI

AWS Lambda was created to handle tasks like uploading images or objects to Amazon S3, updating DynamoDB tables, responding to website clicks, and reacting to sensor readings from an IoT device. AWS Lambda can also be used to automatically provision back-end services triggered by custom HTTP requests, as well as “spin down” those services when they are not in use to save resources.

Nodejs is a JavaScript runtime environment that is open-source and cross-platform. It’s a well-liked tool for almost any project. Outside of the browser, Nodejs runs the V8 JavaScript engine, which is at the heart of Google Chrome. As a result, Nodejs is extremely fast.

This article extensively describes AWS Lambda Nodejs Functions and Applications. It also talks about AWS Lambda and Nodejs. 

Table Of Contents

What is AWS Lambda?

AWS Lambda is a serverless compute service that is triggered by events that allow you to run code for virtually any application or backend service without having to provision or manage servers. You can call Lambda from more than 200 AWS services and SaaS apps, and you only pay for what you use.

The term “serverless” computing refers to the fact that you don’t need to run these functions on your servers. AWS Lambda is a fully managed service that handles all of your infrastructure requirements.

AWS Lambda users write functions, which are self-contained applications written in one of the supported languages and runtimes, and upload them to AWS Lambda, which then executes them in a fast and flexible manner. 

Lambda functions can be used to do anything from serving web pages to processing data streams to calling APIs and integrating with other AWS services.

It also integrates with a variety of other AWS services, including API Gateway, DynamoDB, and RDS, and forms the foundation for AWS Serverless solutions. Lambda is compatible with a wide range of popular languages and runtimes, making it a good fit for Serverless developers.

Key Features of AWS Lambda

  • Concurrency and Scaling Controls: Concurrency and scaling controls, such as concurrency limits and provisioned concurrency, allow you to fine-tune the scalability and reactivity of your production applications.
  • Functions Defined as Container Images: To develop, test, and deploy your Lambda functions, it lets you use your favorite container image tooling, processes, and dependencies.
  • Code Signing: Code signing for Lambda offers trust and integrity controls, allowing you to ensure that only unmodified code published by authorized developers is deployed in your Lambda services.
  • Lambda Extensions: Lambda extensions may be used to enhance your Lambda functions. You can use extensions, for example, to make it easier to combine Lambda with your chosen tools for monitoring, observability, security, and governance.
  • Blueprints of Function: A function blueprint contains sample code that demonstrates how to utilize Lambda in conjunction with other AWS services or third-party applications. Sample code and function setup settings for the Node.js and Python run-times are included in the blueprints.

Set up Seamless Data Ingestion Pipelines from AWS Sources using Hevo

Hevo Data, a Fully-managed Automated Data Pipeline solution, can help you automate, simplify & enrich your data flow from various AWS sources such as AWS S3 and AWS Elasticsearch in a matter of minutes. Hevo’s end-to-end Data Management offers streamlined preparation of Data Pipelines for your AWS account. Additionally, Hevo completely automates the process of not only extracting data from AWS S3 and AWS Elasticsearch but also enriching the data and transforming it into an analysis-ready form without having to write a single line of code.

Get Started with Hevo for Free

With Hevo’s out-of-the-box connectors and blazing-fast Data Pipelines, you can extract & aggregate data from 100+ Data Sources (including 40+ Free Sources) including AWS S3 and AWS Elasticsearch straight into your Data Warehouse, Database, or any destination. To further streamline and prepare your data for analysis, you can process and enrich Raw Granular Data using Hevo’s robust & built-in Transformation Layer without writing a single line of code!

Experience an entirely automated hassle-free Data Pipeline from AWS Services using Hevo. Try our 14-day full access free trial today!

What is Nodejs?

Nodejs is a scalable network application builder that uses an asynchronous event-driven JavaScript runtime. Nodejs is a cross-platform, open-source back-end JavaScript runtime environment that uses the V8 engine to execute JavaScript code outside of a web browser. Nodejs allows developers to use JavaScript to create command-line tools and server-side scripting, which involves running scripts on the server before sending the page to the user’s browser. As a result, Nodejs represents a “JavaScript everywhere” paradigm, bringing web application development together around a single programming language rather than separate languages for server-side and client-side scripts.

The Nodejs distributed development project was previously governed by the Nodejs Foundation, but that organization has now merged with the JS Foundation to form the OpenJS Foundation, which is supported by the Linux Foundation’s Collaborative Projects program.

Official support for Nodejs is available for Linux, macOS, Microsoft Windows 8.1, and Server 2012 (and later), with tier 2 support for SmartOS and IBM AIX and experimental support for FreeBSD. OpenBSD is also supported, with IBM I (AS/400) LTS versions available.

Key Features of Nodejs

NodeJS provides a plethora of robust features. Let’s have a look at these in detail below:

  • Asynchronous & Event-Driven: The APIs of the NodeJS library are completely asynchronous. A server designed in NodeJS never has to wait for data from an API. The server goes on to the next API after visiting one. It employs a notification system called Events to receive and monitor replies to earlier API queries.
  • Highly Scalable:  NodeJS can control and manage concurrent requests efficiently. It contains a cluster module that handles load balancing across all active CPU cores. The ability of NodeJS to horizontally split applications lets businesses provide several app versions to different target groups.
  • Cross-Platform Compatibility: It is compatible with a wide range of operating systems, including Windows, Unix, Linux, Mac OS X, and mobile devices. It can be used in conjunction with the proper package to create a self-sufficient executable.

Understanding AWS Lambda Nodejs

To understand AWS Lambda Nodejs Functions and Applications, read the following concepts:

AWS Lambda Nodejs: Creating Functions

In AWS Lambda Nodejs can be used to execute JavaScript code. Lambda provides Nodejs runtimes for processing events with your code. Your code runs in an AWS SDK for JavaScript environment, using credentials from an AWS Identity and Access Management (IAM) role that you manage.

The runtimes for Nodejs are supported by Lambda.

To write logs to Amazon CloudWatch Logs and access other services and resources, Lambda functions require an execution role. Create a role for function development execution if you don’t already have one.

To create a role for execution,follow the steps below:

  • Step 1: Navigate to the IAM console’s roles page.
  • Step 2: Create the role you should choose.
  • Step 3: Make a role that has the properties listed below.
    • Trusted Entity: Lambda.
    • Permissions : AWSLambdaBasicExecutionRole.
    • Role Name:  lambda-role.

The AWSLambdaBasicExecutionRole policy grants the function the permissions it requires to log to CloudWatch Logs.

You can later add permissions to the role or replace it with a role that is only used for one function.

To make an AWS Lambda Nodejs function, follow these steps.

  • Step 1: Activate the Lambda console.
  • Step 2: Select the Create function.
  • Step 3: Set the following options:
    • Name: my-function.
    • Runtime: Node.js 14.x.
    • Role: Choose an existing role.
    • Existing Role: lambda-role.
  • Step 4: Select the Create option.
  • Step 5: Choose Test to create a test event.
  • Step 6: Enter the test as the name of the event.
  • Step 7: Select the option to Save Changes.
  • Step 8: To use the function, select Test.

A Lambda function with a single source file named index.js is created by the console. The built-in code editor allows you to edit this file and add new ones. Select Save to save your modifications. After that, select Test to run your code.

The handler function, which takes an event object and a context object, is exported by the index.js file. When Lambda executes the function, it calls this handler function. Lambda invocation events are passed to the handler by the AWS Lambda Nodejs function runtime. The index.handler value is used in the function configuration.

The Lambda console produces a.zip file archive deployment package when you save your function code. You’ll need to create a deployment package to upload your function code to the Lambda function if you’re developing it outside of the console (using an SDE).

Deploy one of the sample applications from this guide’s GitHub repository to get started with app development in your local environment.

AWS Lambda Nodejs Applications Examples

  • blank-nodejs: Logging, environment variables, AWS X-Ray tracing, layers, unit tests, and the AWS SDK are all demonstrated in this AWS Lambda Nodejs function.
  • nodejs-apig: A function that processes an API Gateway event and returns an HTTP response with a public API endpoint.
  • rds-mysql: A function that sends RDS database queries to a MySQL server. AWS Secrets Manager is used to configure a private VPC and database instance with a password.
  • efs-nodejs: In an Amazon VPC, this function uses the Amazon EFS file system. This sample comes with a Lambda-ready VPC, file system, mount targets, and access point.
  • list-manager: In an Amazon VPC, this function uses the Amazon EFS file system. This sample comes with a Lambda-ready VPC, file system, mount targets, and access point.
  • error-processor:  A function that updates aggregate lists in Amazon DynamoDB by processing events from an Amazon Kinesis data stream. Each event is saved in the function.

Additionally to the invocation event, the function runtime sends a context object to the handler. Additional information about the invocation, function, and execution environment can be found in the context object. Variables in the environment can provide more information.

A CloudWatch Logs log group is included in your Lambda function. Each invocation is logged in CloudWatch Logs by the function runtime. Any logs generated by your function during an invocation are relayed. If your function throws an error, Lambda formats it and sends it back to the caller.

Because of its unique event loop model, AWS Lambda Nodejs’ initialization behavior differs from that of other runtimes. Nodejs, in particular, employs a non-blocking I/O model that allows for asynchronous operations. This model enables AWS Lambda Nodejs to perform well in the majority of workloads. If an AWS Lambda Nodejs function makes a network call, for example, the request may be marked as asynchronous and placed in a callback queue. The function can continue to process other operations within the main call stack while waiting for the network call to return without being blocked. The callback for the network call is executed and then removed from the callback queue once the network call is returned.

Asynchronous execution is possible for some initialization tasks. The completion of these asynchronous tasks prior to an invocation is not guaranteed. For example, by the time Lambda executes the handler function, the code that makes a network call to fetch a parameter from AWS Parameter Store may not be complete. As a result, during an invocation, the variable may be null. To avoid this, make sure variables and other asynchronous codes are fully initialized before moving on to the function’s core business logic.

Starting with Node 14, you can use top-level await to guarantee completion of asynchronous initialization code prior to handler invocations by declaring your code as an ES module. To use top-level await, you must first designate your function code as an ES module. You can do this in one of two ways: specify the type as a module in the package.json file for the function, or use the.mjs file name extension.

Your function code in the first scenario treats all.js files as ES modules, whereas in the second scenario, only the file you specify with.mjs is an ES module. By naming them.mjs and.cjs, you can mix ES and CommonJS modules, as.mjs files are always ES modules and.cjs files are always CommonJS modules.

What Makes Data Ingestion from AWS Sources using Hevo’s Data Pipeline Unique

Loading data from AWS Sources such as AWS S3 and AWS Elasticsearch can be a mammoth task if the right set of tools is not leveraged. Hevo’s No-Code Automated Data Pipeline empowers you with a fully-managed solution for all your data collection, processing, and loading needs. Hevo’s native integration with S3 and Elasticsearch empowers you to transform and load data straight to a Data Warehouse such as Redshift, Snowflake, BigQuery & more!

This data loading lets you effortlessly connect to 100+ Sources (including 40+ free sources) and leverage Hevo’s blazing-fast Data Pipelines to help you seamlessly extract, transform, and load data to your desired destination such as a Data Warehouse.

Our platform has the following in store for you:

  • Data Transformations: Best-in-class & Native Support for Complex Data Transformation at fingertips. Code & No-code Fexibilty designed for everyone.
  • Smooth Schema Mapping: Fully-managed Automated Schema Management for incoming data with the desired destination.
  • Quick Setup: Hevo with its automated features, can be set up in minimal time. Moreover, with its simple and interactive UI, it is extremely easy for new customers to work on and perform operations.
  • Transformations: Hevo provides preload transformations to make your incoming data from AWS S3 and AWS Elasticsearch fit for the chosen destination. You can also use drag and drop transformations like Date and Control Functions, JSON, and Event Manipulation to name a few.
  • Live Support: The Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
SIGN UP HERE FOR A 14-DAY FREE TRIAL

AWS Lambda Nodejs: Deploy Functions

With .zip File Archives

Scripts or compiled programs and their dependencies make up the code for your AWS Lambda function. To get your function code into Lambda, you use a deployment package. Container images and.zip file archives are both supported by Lambda as deployment packages.

Use a built-in.zip file archive utility or any other.zip file utility (such as 7zip) for your command-line tool to create the deployment package for a.zip file archive. When using a.zip file as a deployment package, keep the following in mind:

  • The.zip file contains the code for your function as well as any dependencies required to run it on Lambda (if any). You don’t need to include these libraries in your.zip file if your function only relies on standard libraries or AWS SDK libraries. The supported Lambda runtime environments come with these libraries pre-installed.
  • If your.zip file is larger than 50 MB, you should upload it to your function using an Amazon Simple Storage Service (Amazon S3) bucket.
  • You can use AWS Serverless Application Model to create your deployment package if it contains native libraries (AWS SAM). To create your deployment package, use the AWS SAM CLI sam build command with —use-container. This option creates a Lambda-compatible deployment package inside a Docker image.
  • To be compatible with the function’s instruction set architecture, you must build the deployment package.
  • Because Lambda works with POSIX file permissions, you may need to set permissions for the deployment package folder before creating the.zip file archive.
Updating a Function with No Dependencies

The UpdateFunctionCode operation is used to update a function using the Lambda API. Create an archive containing your function code and upload it to AWS using the CLI (AWS CLI).

To make a change to a AWS Lambda Nodejs function that has no dependencies:

  • Make a.zip archive file.
zip function.zip index.js
  • Use the update-function-code command to put the package on the server.
aws lambda update-function-code --function-name my-function --zip-file fileb://function.zip

The following output will appear:

{
    "FunctionName": "my-function",
    "FunctionArn": "arn:aws:lambda:us-west-2:123456789012:function:my-function",
    "Runtime": "nodejs12.x",
    "Role": "arn:aws:iam::123456789012:role/lambda-role",
    "Handler": "index.handler",
    "CodeSha256": "Qf0hMc1I2di6YFMi9aXm3JtGTmcDbjniEuiYonYptAk=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "Active"
    },
    "RevisionId": "983ed1e3-ca8e-434b-8dc1-7d72ebadd83d",
    ...
}
Updating a Function with Additional Dependencies

Use npm to include libraries that aren’t part of the AWS SDK for JavaScript in your deployment package if your function requires them. Make sure your local Nodejs version matches the Nodejs version of your function. Use an Amazon Linux environment to create the deployment package if any of the libraries require native code.

If you need a newer version than the one included with the runtime, or if you want to make sure it doesn’t change in the future, you can include the SDK for JavaScript in the deployment package.

You can use AWS Serverless Application Model to create your deployment package if it contains native libraries (AWS SAM). To create your deployment package, use the AWS SAM CLI sam build command with —use-container. This option creates a Lambda-compatible deployment package inside a Docker image.

You can also use an Amazon EC2 instance with an Amazon Linux environment to build the deployment package.

In order to update a AWS Lambda Nodejs function with dependencies, follow these steps:

  • Step 1: Open a terminal or shell with the command line. Make sure your local Nodejs version matches the Nodejs version of your function.
  • Step 2: For the deployment package, make a folder. Assume the folder is called my-function in the steps that follow.
  • Step 3: Using the npm install command, add libraries to your node modules directory.
npm install aws-xray-sdk
  • Step 4: This results in a folder structure that looks like this:
~/my-function
├── index.js
└── node_modules
    ├── async
    ├── async-listener
    ├── atomic-batcher
    ├── aws-sdk
    ├── aws-xray-sdk
    ├── aws-xray-sdk-core
  • Step 5: Make a.zip file with your project folder’s contents. If you want zip to compress the subfolders, use the r (recursive) option.
zip -r function.zip .

The following output will appear:

{
    "FunctionName": "my-function",
    "FunctionArn": "arn:aws:lambda:us-east-2:123456789012:function:my-function",
    "Runtime": "nodejs12.x",
    "Role": "arn:aws:iam::123456789012:role/lambda-role",
    "Handler": "index.handler",
    "CodeSha256": "Qf0hMc1I2di6YFMi9aXm3JtGTmcDbjniEuiYonYptAk=",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "Active"
    },
    "RevisionId": "983ed1e3-ca8e-434b-8dc1-7d72ebadd83d",
    ...
}

With Container Images

You can use a container image to deploy your Lambda function code. To assist you in creating a container image for your AWS Lambda Nodejs function, AWS offers the following resources:

  • Lambda Base Images from AWS:
    • These base images come pre-installed with a language runtime and other components needed to run on Lambda. To assist you in creating your container image, AWS provides a Dockerfile for each of the base images.
    • AWS provides base images for all supported.NET runtimes on the x86 64 architecture, as well as the.NET Core 3.1 and.NET 6.0 runtimes on the arm64 architecture.
  • Open-source Runtime Interface Clients (RIC):
    • If you’re using a community or private enterprise base image, you’ll need to add a Runtime interface client to make it Lambda-compatible.
    • Emulator for open-source runtime interfaces (RIE)
    • You can test your function locally with Lambda’s runtime interface emulator. The RIE has been included in the Lambda base images as well as the base images for custom runtimes. You can use the RIE to test your image locally if you’re using another base image.

The steps in the workflow for a function defined as a container image are as follows:

  • Use the resources listed in this topic to create your container image.
  • Create an Amazon ECR container registry and upload the image.
  • To deploy an image to an existing function, create a Lambda function or update the function code.
  • AWS Base Images for Nodejs: For Nodejs, AWS provides the following base images
  • Using a Nodejs Base Image: Select the usage tab on AWS Lambda base images for Nodejs in the Amazon ECR repository for instructions on how to use an AWS Lambda Nodejs base image.
  • Nodejs Runtime Interface Clients: The npm package manager can be used to install the runtime interface client for AWS Lambda Nodejs:
npm install aws-lambda-ric
  • Deploy the Container Image: The AWS Lambda Nodejs image is deployed when a new function is created. You must redeploy the image by updating the function code if you rebuild the container image for an existing function.

Conclusion

This article talks about AWS Lambda Nodejs applications and functions in detail. In addition to that, it also gives a brief introduction to AWS Lambda and Nodejs.

visit our website to explore hevo

Hevo Data, a No-code Data Pipeline provides you with a consistent and reliable solution to manage data transfer between a variety of sources and a wide variety of Desired Destinations, with a few clicks. Hevo Data with its strong integration with 100+ sources (including 40+ free sources) allows you to not only export data from your desired data sources & load it to the destination of your choice, but also transform & enrich your data to make it analysis-ready so that you can focus on your key business needs and perform insightful analysis using BI tools.

Want to take Hevo for a spin? Sign Up for a 14-day free trial and experience the feature-rich Hevo suite first hand. You can also have a look at the unbeatable pricing that will help you choose the right plan for your business needs.

Harshitha Balasankula
Former Marketing Content Analyst, Hevo Data

Harshita is a data analysis enthusiast with a keen interest for data, software architecture, and writing technical content. Her passion towards contributing to the field drives her in creating in-depth articles on diverse topics related to the data industry.

No-code Data Pipeline For Your Data Warehouse