Logo

dev-resources.site

for different kinds of informations.

Running lambdas locally using Javascript/Node.js

Published at
12/29/2024
Categories
aws
javascript
sam
node
Author
rashiagarwal
Categories
4 categories in total
aws
open
javascript
open
sam
open
node
open
Author
12 person written this
rashiagarwal
open
Running lambdas locally using Javascript/Node.js

Tired of deploying to the cloud just to test your AWS Lambda? It’s like mailing yourself a letter just to proofread it—slow, expensive, and totally unnecessary.

What if you could invoke your Lambda locally—right from your JavaScript application? No cloud round-trips, no waiting, just instant feedback. With AWS SAM (Serverless Application Model) and a sprinkle of Node.JS/JavaScript, you can test and debug your Lambdas on your machine, saving time, money🤩.

Pre-Requisites:

I am using a Windows PC and have the following things set-up before proceeding ahead:

  • AWS SDK
aws --version
Enter fullscreen mode Exit fullscreen mode
  • SAM CLI
sam --version
Enter fullscreen mode Exit fullscreen mode
  • Docker
docker --version
Enter fullscreen mode Exit fullscreen mode
  • Node.js
node --version
Enter fullscreen mode Exit fullscreen mode

Let’s get started!

We will start by creating a demo application using init command and then further proceed to invoke lambda within the application code (Nodejs).

1> Navigate to the folder where you want to set up your project.

sam init --runtime nodejs
Enter fullscreen mode Exit fullscreen mode

This will create a basic Hello world application.It has a simple Lambda handler which it takes in the event and returns back the data received from a particular URL, along with a Hello World message.

Let's take a deeper dive into what the files implement and how the code works:

  • SAM Template (template.js) -> it implements HelloWorld Function where it uses the Code URI property set to Hello World SLASH. This implies that the Lambda handler code is inside the Hello World folder.
    • app.js -> This is referred in the Handler property of the SAM template, it contains exported function called Lambda Handler.

You will notice a dependency named 'axios' in package.json, note that the module is simply used to make HTTP requests to the given URL.

2> Install the dependency from package.json by navigating to the folder having the file, then use the command:

npm install
Enter fullscreen mode Exit fullscreen mode

3> Now, start the Lambda service locally by running the command.

sam local start-lambda
Enter fullscreen mode Exit fullscreen mode

So, now the local Lambda service is running on port 3001.

4> We will invoke our lambda functions from within our application code using the AWS SDK. To do that, let's create a sample node.js project. Create a new folder, navigate to the folder and enter the command.

npm init
npm install aws-sdk --save
Enter fullscreen mode Exit fullscreen mode

5> Now create test.js file and reference the AWS SDK, configure the AWS region,instantiate a Lambda object.

const AWS = require('aws-sdk');
AWS.config.update({ region: 'us-west-2' });
const lambda = new AWS.Lambda({
      endpoint: 'http://127.0.0.1:3001'
});

var parameters = {
    FunctionName: 'HelloWorldFunction',
    PayLoad: new Buffer('{}')
};

lambda.invoke(params, (e,data) => {
  if(e){
    console.log("Error: "+e);
  } else{
      console.log(data);
  }
});

Enter fullscreen mode Exit fullscreen mode

The parameter object here, invokes the Lambda Function, replace the function name according to the application code. The endpoint refers to the one on which your local lambda service is started.

6> Now run the command in a new terminal:

node test.js
Enter fullscreen mode Exit fullscreen mode

7> Verification and Testing: When you navigate back to the AWS SAM powershell, you can see it invokes Lambda handler locally, by first loading the function in a Docker container and execute it with our input event data.
Now navigate back to the other terminal, you can see the response from the Lambda function, with status code 200 ✅, message Hello World, and the location IP address😄.

Wrapping Up

And there you have it—a faster, smarter way to test your AWS Lambda functions without ever leaving the comfort of your local setup. By invoking Lambdas locally with JavaScript and NodeJS, you’ve unlocked a whole new level of efficiency. Now you can iterate faster, debug smarter, and build cooler apps—all while keeping your cloud costs low.

Happy coding, and may your Lambdas always run as smoothly locally as they will in the cloud! 🎉

sam Article's
30 articles in total
Favicon
Running lambdas locally using Javascript/Node.js
Favicon
Cut Your AWS Lambda Logging Costs: Filter Logs with AWS SAM
Favicon
Building a "Real-Time" Data Integration Platform on AWS
Favicon
Using Amazon Cognito with the user-password flow
Favicon
SAM Registration and Maintenance Ensuring Your Business Stays Compliant
Favicon
Utilizing the System for Award Management SAM for Government Contracting Success
Favicon
Secure API Gateway with Amazon Cognito using SAM
Favicon
Resources and Properties for AWS SAM
Favicon
Adding Cognito Authentication to our Serverless Dash App
Favicon
Using YAML anchors and aliases in a SAM template
Favicon
First impressions of CloudFormation’s IaC generator and CDK migrate
Favicon
Building Scalable Serverless Applications with AWS SQS and Lambda using SAM
Favicon
How to add CI/CD to my SAM project
Favicon
How to create serverless applications with AWS SAM (Serverless Application Model)
Favicon
Introduction to AWS SAM (Serverless Application Model)
Favicon
Help! How do I set DeletionPolicy to Retain for production only?
Favicon
An efficient way to build your serverless microservices. Part 3. CI/CD with AWS SAM.
Favicon
Leveraging Infrastructure as Code (IaC) for AWS Lambda: A Comparative Analysis of AWS SAM, Terraform, and Serverless Framework
Favicon
AWS Lambda with Rust and SAM
Favicon
Deploying Lambdas with AWS SAM & GitHub Actions: Step by Step
Favicon
Speed up new serverless application development with customized SAM templates
Favicon
Streamline AWS Development with CI/CD, SAM, and GitHub Actions
Favicon
AWS sam #3: sam local + ApiGateway Lambda authorizer
Favicon
✨ Porting Lambda Functions to AWS SAM
Favicon
Store Thumbnails from Your Live Stream Using AWS SAM CLI to Set Up Lambda Function and API Gateway
Favicon
AWS sam #2: sam local + logs
Favicon
AWS sam #1: sam local + DynamoDB
Favicon
Event-driven file management using S3 Notifications and Step Functions
Favicon
Folding as a Service with AWS StepFunctions
Favicon
Elevating Your Serverless Development with AWS SAM

Featured ones: