Skip to content
GitHubXDiscord

AWS Lambda Function

The Function resource lets you create and manage AWS Lambda functions with support for Node.js runtimes, environment variables, and function URLs.

Create a basic Lambda function with default settings:

import { Function } from "alchemy/aws";
const func = await Function("api", {
functionName: "my-api",
bundle: bundle,
roleArn: role.arn,
handler: "index.handler",
});

Add environment variables to configure the function:

const func = await Function("api", {
functionName: "my-api",
bundle: bundle,
roleArn: role.arn,
handler: "index.handler",
environment: {
TABLE_NAME: table.name,
QUEUE_URL: queue.url,
},
});

Create a public HTTP endpoint for the function:

const func = await Function("api", {
functionName: "my-api",
bundle: bundle,
roleArn: role.arn,
handler: "index.handler",
url: {
authType: "NONE",
cors: {
allowOrigins: ["*"],
allowMethods: ["GET", "POST"],
allowHeaders: ["content-type"],
},
},
});

Customize memory, timeout and other settings:

const func = await Function("worker", {
functionName: "worker",
bundle: bundle,
roleArn: role.arn,
handler: "worker.process",
runtime: "nodejs20.x",
architecture: "arm64",
memorySize: 512,
timeout: 30,
tags: {
Environment: "production",
},
});