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;};
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 payloadif (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.
InvokeCommand — asynchronous invocation
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
ListFunctionsCommand — list Lambda functions
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);}
GetFunctionCommand — describe a function
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,});
CreateFunctionCommand — create a function
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 fileconst 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);
UpdateFunctionCodeCommand — deploy new code
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 fileconst zipBuffer = readFileSync("./function.zip");await client.send( new UpdateFunctionCodeCommand({ FunctionName: "my-function", ZipFile: zipBuffer, }));// Or deploy from an S3 objectawait client.send( new UpdateFunctionCodeCommand({ FunctionName: "my-function", S3Bucket: "my-deployment-bucket", S3Key: "deployments/function-v2.zip", }));
DeleteFunctionCommand — delete a function
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", }));