AWS Lambda
Node.js
Serverless
Cloud Computing
API Integration

Invoke amazon lambda function from node app

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Invoking an AWS Lambda function from a Node.js application is mostly an AWS SDK task: build a Lambda client, send an invoke command, and decode the response if you are using synchronous invocation. The parts that usually matter in practice are the invocation type, IAM permissions, and how the payload is serialized.

Use the AWS SDK for JavaScript v3

The current AWS SDK for Node applications is modular. Install only the Lambda client package:

bash
npm install @aws-sdk/client-lambda

A basic synchronous invocation looks like this:

javascript
1import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";
2
3const client = new LambdaClient({ region: "us-east-1" });
4
5const payload = {
6  userId: 123,
7  action: "send-welcome-email"
8};
9
10const command = new InvokeCommand({
11  FunctionName: "process-user-event",
12  InvocationType: "RequestResponse",
13  Payload: Buffer.from(JSON.stringify(payload))
14});
15
16const response = await client.send(command);
17const responseText = Buffer.from(response.Payload).toString("utf8");
18const result = JSON.parse(responseText);
19
20console.log(result);

Important points:

  • 'FunctionName identifies the Lambda function'
  • 'InvocationType: "RequestResponse" waits for completion'
  • 'Payload must be serialized bytes, not a plain JavaScript object'

Use Asynchronous Invocation for Fire-and-Forget Work

If your Node application only needs to trigger background work and does not need the function's return value, use InvocationType: "Event":

javascript
1import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";
2
3const client = new LambdaClient({ region: "us-east-1" });
4
5await client.send(
6  new InvokeCommand({
7    FunctionName: "process-user-event",
8    InvocationType: "Event",
9    Payload: Buffer.from(JSON.stringify({ userId: 123 }))
10  })
11);
12
13console.log("Invocation accepted");

This queues the invocation and returns quickly. It is appropriate for notifications, audit jobs, and other asynchronous workflows.

Credentials and Permissions

Your Node application still needs AWS credentials and the right IAM permission. The calling identity must be allowed to invoke the target function:

json
1{
2  "Version": "2012-10-17",
3  "Statement": [
4    {
5      "Effect": "Allow",
6      "Action": "lambda:InvokeFunction",
7      "Resource": "arn:aws:lambda:us-east-1:123456789012:function:process-user-event"
8    }
9  ]
10}

If the credentials or region are wrong, the code structure may look fine while the call still fails immediately.

Decode the Response Carefully

For synchronous invocations, Lambda returns payload bytes. That means this pattern is normal:

javascript
const text = Buffer.from(response.Payload).toString("utf8");

Only parse JSON if your function actually returns JSON:

javascript
1if (text) {
2  const data = JSON.parse(text);
3  console.log(data);
4}

If the function returns plain text or an empty body, JSON.parse() would be the wrong next step.

Also inspect:

  • 'response.FunctionError'
  • HTTP status metadata
  • your function's own result contract

A Small Wrapper Function

In a real application, wrap the SDK call so the rest of the codebase does not duplicate the invoke logic:

javascript
1import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";
2
3const client = new LambdaClient({ region: "us-east-1" });
4
5export async function invokeLambda(functionName, payload) {
6  const response = await client.send(
7    new InvokeCommand({
8      FunctionName: functionName,
9      InvocationType: "RequestResponse",
10      Payload: Buffer.from(JSON.stringify(payload))
11    })
12  );
13
14  const text = Buffer.from(response.Payload ?? []).toString("utf8");
15  return text ? JSON.parse(text) : null;
16}

This makes testing and error handling easier to centralize.

When Lambda Invocation Is the Wrong Boundary

Just because one AWS service can invoke another does not mean it always should. Direct Lambda-to-Lambda or app-to-Lambda invocation can be fine, but if the workflow is truly event-driven, a queue or event bus may be a better boundary than a direct invoke call.

That is an architecture question, not an SDK question.

Common Pitfalls

  • Sending a plain object as Payload instead of serialized bytes.
  • Using InvocationType: "Event" and then expecting a real return value.
  • Forgetting region or IAM configuration and debugging the wrong layer.
  • Assuming every Lambda returns JSON and calling JSON.parse() blindly.
  • Duplicating invoke logic throughout the codebase instead of wrapping it once.

Summary

  • Use LambdaClient and InvokeCommand from the AWS SDK v3.
  • Serialize the payload with JSON.stringify() and Buffer.from(...).
  • Use RequestResponse when you need the function result.
  • Use Event when you only need to trigger background work.
  • Confirm credentials, IAM permission, and region before blaming the code.

Course illustration
Course illustration

All Rights Reserved.