Skip to main content

Package

@aws-sdk/client-lambda

Installation

npm install @aws-sdk/client-lambda

Creating the client

import { LambdaClient } from "@aws-sdk/client-lambda";

const client = new LambdaClient({ region: "us-east-1" });
When using the SDK inside a Lambda function, initialize the client outside of the handler to reuse the connection across invocations. Make API calls from within the handler so requests are signed at execution time.
import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";

// Client initialized outside the handler (reused across warm invocations)
const client = new LambdaClient({ region: "us-east-1" });

export const handler = async (event: unknown) => {
  const response = await client.send(
    new InvokeCommand({
      FunctionName: "my-other-function",
      Payload: JSON.stringify({ key: "value" }),
    })
  );
  return response;
};

Commands

Synchronous invocation waits for the function to complete and returns the response payload.
import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";

const client = new LambdaClient({ region: "us-east-1" });

const response = await client.send(
  new InvokeCommand({
    FunctionName: "my-function",
    InvocationType: "RequestResponse", // Default, waits for result
    Payload: JSON.stringify({ action: "processOrder", orderId: "order-123" }),
  })
);

// Decode the response payload
if (response.Payload) {
  const result = JSON.parse(Buffer.from(response.Payload).toString());
  console.log(result);
}

// Check for function-level errors (HTTP 200 but function threw an error)
if (response.FunctionError) {
  console.error("Function returned an error:", response.FunctionError);
}
response.StatusCode is 200 for a successful invocation. A function error (e.g., unhandled exception inside the function) sets response.FunctionError to "Handled" or "Unhandled" and puts the error object in Payload.
Asynchronous (fire-and-forget) invocation queues the event and returns immediately with status code 202.
import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";

const client = new LambdaClient({ region: "us-east-1" });

const response = await client.send(
  new InvokeCommand({
    FunctionName: "my-background-worker",
    InvocationType: "Event", // Fire-and-forget
    Payload: JSON.stringify({ task: "sendEmail", to: "[email protected]" }),
  })
);

console.log(response.StatusCode); // 202 on success
import { LambdaClient, ListFunctionsCommand } from "@aws-sdk/client-lambda";

const client = new LambdaClient({ region: "us-east-1" });

const response = await client.send(
  new ListFunctionsCommand({
    MaxItems: 50,
  })
);

for (const fn of response.Functions ?? []) {
  console.log(fn.FunctionName, fn.Runtime, fn.LastModified);
}
import { LambdaClient, GetFunctionCommand } from "@aws-sdk/client-lambda";

const client = new LambdaClient({ region: "us-east-1" });

const response = await client.send(
  new GetFunctionCommand({
    FunctionName: "my-function",
    // Optional: specify a version or alias
    // Qualifier: "$LATEST",
  })
);

console.log({
  arn: response.Configuration?.FunctionArn,
  runtime: response.Configuration?.Runtime,
  handler: response.Configuration?.Handler,
  memorySize: response.Configuration?.MemorySize,
  timeout: response.Configuration?.Timeout,
  lastModified: response.Configuration?.LastModified,
});
import {
  LambdaClient,
  CreateFunctionCommand,
  Runtime,
  PackageType,
} from "@aws-sdk/client-lambda";
import { readFileSync } from "fs";

const client = new LambdaClient({ region: "us-east-1" });

// Deploy from a zip file
const zipBuffer = readFileSync("./function.zip");

const response = await client.send(
  new CreateFunctionCommand({
    FunctionName: "my-new-function",
    Runtime: Runtime.nodejs22x,
    Role: "arn:aws:iam::123456789012:role/lambda-execution-role",
    Handler: "index.handler",
    Code: {
      ZipFile: zipBuffer,
    },
    Description: "My new Lambda function",
    Timeout: 30,
    MemorySize: 256,
    Environment: {
      Variables: {
        NODE_ENV: "production",
        TABLE_NAME: "my-table",
      },
    },
  })
);

console.log(response.FunctionArn);
import {
  LambdaClient,
  UpdateFunctionCodeCommand,
} from "@aws-sdk/client-lambda";
import { readFileSync } from "fs";

const client = new LambdaClient({ region: "us-east-1" });

// Deploy from a local zip file
const zipBuffer = readFileSync("./function.zip");

await client.send(
  new UpdateFunctionCodeCommand({
    FunctionName: "my-function",
    ZipFile: zipBuffer,
  })
);

// Or deploy from an S3 object
await client.send(
  new UpdateFunctionCodeCommand({
    FunctionName: "my-function",
    S3Bucket: "my-deployment-bucket",
    S3Key: "deployments/function-v2.zip",
  })
);
import { LambdaClient, DeleteFunctionCommand } from "@aws-sdk/client-lambda";

const client = new LambdaClient({ region: "us-east-1" });

await client.send(
  new DeleteFunctionCommand({
    FunctionName: "my-old-function",
    // Optional: delete a specific version (omit to delete $LATEST and all versions)
    // Qualifier: "3",
  })
);

Parsing the invocation response payload

InvokeCommand returns Payload as a Uint8Array. Convert it to a string before parsing:
import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";

const client = new LambdaClient({ region: "us-east-1" });

const response = await client.send(
  new InvokeCommand({
    FunctionName: "my-function",
    Payload: JSON.stringify({ input: "data" }),
  })
);

if (response.Payload) {
  const payloadString = Buffer.from(response.Payload).toString("utf-8");
  const result = JSON.parse(payloadString);
  console.log(result);
}

Pagination

Use paginateListFunctions to iterate all functions without managing NextMarker:
import { LambdaClient, paginateListFunctions } from "@aws-sdk/client-lambda";

const client = new LambdaClient({ region: "us-east-1" });

for await (const page of paginateListFunctions({ client }, {})) {
  for (const fn of page.Functions ?? []) {
    console.log(fn.FunctionName);
  }
}

Build docs developers (and LLMs) love