Skip to main content

Function

Creates and manages AWS Lambda functions with support for Node.js runtimes, custom handlers, environment variables, and function URLs. Handles deployment packaging, IAM role stabilization, and function updates.

Props

bundle
Bundle
required
Bundle for the function. Use Bundle() from alchemy/esbuild to create a code bundle.
roleArn
string
required
ARN of the IAM role that Lambda assumes when executing the function.
handler
string
required
Function handler in the format ‘file.function’. For Node.js this is typically ‘index.handler’ or similar.
functionName
string
Name of the Lambda function.Default: ${app}-${stage}-${id}
runtime
Runtime
Lambda runtime environment for the function.Default: nodejs20.x
architecture
Architecture
CPU architecture for the function.Default: x86_64
description
string
Description of the function’s purpose.
timeout
number
Maximum execution time in seconds.Default: 3
memorySize
number
Amount of memory available to the function in MB.Default: 128
environment
Record<string, string>
Environment variables available to the function code.
tags
Record<string, string>
Resource tags for the function.
url
object
Function URL configuration for direct HTTP(S) invocation.
layers
string[]
Lambda layers for the function. Use the layer ARN.

Output

arn
string
ARN of the Lambda function.
functionName
string
Name of the Function.
lastModified
string
Timestamp of the last function modification.
version
string
Function version.
qualifiedArn
string
ARN with version suffix.
invokeArn
string
ARN for invoking the function through API Gateway.
sourceCodeHash
string
SHA256 hash of the function code.
sourceCodeSize
number
Size of the function code in bytes.
ephemeralStorageSize
number
Size of ephemeral storage (/tmp) in MB.
architectures
string[]
List of supported CPU architectures.
masterArn
string
ARN of the master function (Lambda@Edge only).
revisionId
string
Unique identifier for the current function code/config.
state
string
Current state of the function.
stateReason
string
Reason for the current state.
stateReasonCode
string
Code for the current state reason.
lastUpdateStatus
string
Status of the last update operation.
lastUpdateStatusReason
string
Reason for the last update status.
lastUpdateStatusReasonCode
string
Code for the last update status reason.
packageType
string
Function package type (Zip or Image).
signingProfileVersionArn
string
ARN of the signing profile version.
signingJobArn
string
ARN of the signing job.
functionUrl
string
Function URL if configured.

Examples

Basic Lambda function with minimal configuration

import { Function, Role } from "alchemy/aws";
import { Bundle } from "alchemy/esbuild";

const role = await Role("lambda-role", {
  assumeRolePolicy: {
    Version: "2012-10-17",
    Statement: [{
      Effect: "Allow",
      Principal: { Service: "lambda.amazonaws.com" },
      Action: "sts:AssumeRole"
    }]
  }
});

const bundle = await Bundle("api-bundle", {
  entryPoint: "./src/index.ts",
  outdir: ".out",
  format: "esm",
  platform: "node",
  target: "node20"
});

const basicFunction = await Function("api-handler", {
  bundle,
  roleArn: role.arn,
  handler: "index.handler",
  tags: {
    Environment: "production"
  }
});

Function with environment variables and custom memory/timeout

import { Function } from "alchemy/aws";
import { Bundle } from "alchemy/esbuild";

const bundle = await Bundle("worker-bundle", {
  entryPoint: "./src/worker.ts",
  outdir: ".out",
  format: "esm"
});

const configuredFunction = await Function("worker", {
  bundle,
  roleArn: role.arn,
  handler: "worker.process",
  memorySize: 512,
  timeout: 30,
  environment: {
    QUEUE_URL: queue.url,
    LOG_LEVEL: "info"
  }
});

Function with a public URL endpoint, CORS and response streaming

import { Function } from "alchemy/aws";
import { Bundle } from "alchemy/esbuild";

const bundle = await Bundle("api-bundle", {
  entryPoint: "./src/api.ts",
  outdir: ".out"
});

const apiFunction = await Function("public-api", {
  bundle,
  roleArn: role.arn,
  handler: "api.handler",
  url: {
    authType: "NONE",
    invokeMode: "RESPONSE_STREAM",
    cors: {
      allowOrigins: ["*"],
      allowMethods: ["GET", "POST"],
      allowHeaders: ["content-type"],
      maxAge: 86400
    }
  }
});

Build docs developers (and LLMs) love