Skip to main content

Overview

The Lambda Function resource provides serverless compute with automatic scaling, built-in high availability, and pay-per-use pricing. It covers both standard functions and singleton functions for AWS Custom Resources.

Resource Classes

The deep lib/... path is a published subpath of the package. The Lambda and ECS constructs are not re-exported from the package root (the EC2 construct is), so this import is the only one that compiles for LambdaFunction.
This page covers using the construct class directly. The scaffolded fjall/<app>/infrastructure.ts that fjall create app writes never does that: it imports App and the *Factory symbols from the package root, and fjall add edits it through the codemod engine.

Basic Usage

Standard Function

Singleton Function

uuid is optional. Omit it and the construct derives a stable one from the construct path. See Singleton Function Pattern.

Configuration Options

Core Properties

Function Configuration

SingletonFunction defaults lambdaDescription to ${id} singleton lambda instead. Passing vpc also picks the subnet type: PRIVATE_WITH_EGRESS when the VPC has NAT gateways, otherwise PRIVATE_ISOLATED.

Execution Role

Set rolePath and roleName together when an SCP exemption needs to match a predictable role ARN.
A fixed roleName is account-global. Only set one on a Lambda confined to a single account and region stack, or regional duplicates collide on the role name at deploy time.

Function URL Properties

Async Failure Handling

See Async failure queue for when the queue is created.

Secrets Configuration

The ssmSecretsPath default only resolves when both appName and functionName are supplied. With secrets set and neither an explicit path nor both names available, the construct throws at synth.

Alarms

Alarms are only created when alertsTopic is set and alarms is not false. LambdaAlarmThresholds accepts errorRateThreshold and durationThresholdPercent.

Runtimes

Both constructs reject deprecated runtimes at synth, before any deploy, and fail with:
Rejected runtime names: This guard shifts the posture scan’s LAMBDA_DEPRECATED_RUNTIME finding left. Without it the deploy succeeds and the finding surfaces days later. A deprecated runtime receives no security patches, and AWS eventually blocks function creation on it.

Code Sources

From Local Directory

From Inline Code

From S3 Bucket

From Docker Image

IAM Policies

The inlinePolicy property accepts an array of PolicyStatement objects. Each statement is added directly to the Lambda function’s execution role.

S3 Access

DynamoDB Access

Multiple Policy Statements

Environment Variables

Function URL Configuration

Basic HTTP Endpoint

FunctionUrlAuthType.NONE publishes an unauthenticated endpoint. Cap it with reservedConcurrentExecutions, which bounds both the function’s own spend and the share of account-wide concurrency an anonymous caller can take from every other function in the account.
Omitting reservedConcurrentExecutions on a NONE Function URL raises a synth-time CDK warning: “exposes a public Function URL (authType NONE) without reservedConcurrentExecutions”. The synth still succeeds, so the warning is easy to miss in CI output.

With CORS

Performance Configuration

High Memory Function

ARM Architecture

Event Sources

LambdaFunction provides three built-in event-source methods: addSqsEventSource, addDynamoDbEventSource, and addS3EventSource.

SQS Trigger

Defaults: batchSize 10, reportBatchItemFailures true. maxBatchingWindow is in seconds.

DynamoDB Stream Trigger

Defaults: startingPosition "LATEST", batchSize 100, reportBatchItemFailures true. retryAttempts and parallelizationFactor are also accepted.

S3 Trigger

Accepted events values: OBJECT_CREATED, OBJECT_REMOVED, OBJECT_CREATED_PUT, OBJECT_CREATED_POST, OBJECT_CREATED_COPY, OBJECT_REMOVED_DELETE. Defaults to ["OBJECT_CREATED"].

Async failure queue

S3 event notifications invoke the function asynchronously, and a failed event is dropped silently after the default two retries. To stop that, the first addS3EventSource call also creates an SQS queue and wires it as the function’s dead-letter destination. Reuse an existing queue or opt out entirely:
Read the wired queue back with processor.getAsyncFailureQueue().

Scheduling

The construct has no built-in scheduling method or prop. To run a function on a schedule, attach a standalone EventBridge Rule that targets the function.

Singleton Function Pattern

uuid is optional. When omitted, the construct derives one from a sha256 of the construct path (scope.node.path plus id), so the logical ID SingletonLambda<uuid> stays stable across synths. A random default would recreate the singleton, and re-run any custom resource fronted by it, on every deploy.
Changing a supplied uuid changes the logical ID. CloudFormation replaces the singleton and re-runs any custom resource behind it. Pick a value once and leave it, or omit uuid and let the construct derive it.

Custom Resource Handler

SingletonFunction supports the core props, timeout, ephemeralStorageSize, logGroupRetention, lambdaDescription, and the execution-role props. Function URLs, event sources, secrets, and alarms are LambdaFunction only.

Methods

Get Function URL

Get Async Failure Queue

Get Execution Role

Get Function ARN

Get Function Name

Outputs

LambdaFunction creates these CloudFormation outputs, keyed on the PascalCased construct ID:
  • <PascalCaseId>FunctionArn - Function ARN
  • <PascalCaseId>FunctionUrl - Function URL (only when enableFunctionUrl is true)

Complete Example

Tagging

The construct takes no tags prop. Apply tags through the CDK Tags aspect after construction.

Best Practices

  1. Keep functions small and focused on single tasks
  2. Set appropriate timeouts rather than defaulting to the maximum
  3. Cap public endpoints with reservedConcurrentExecutions
  4. Keep the async failure queue on any function fed by S3 events
  5. Use PolicyStatement arrays for least-privilege IAM
  6. Consider ARM (Graviton2) for cost savings
  7. Use secrets and secretsImport for sensitive values rather than plain environment variables

Performance Tips

  • Minimise package size to reduce cold starts
  • Reuse connections outside the handler function
  • Use provisioned concurrency for predictable traffic
  • Choose appropriate memory (CPU scales with memory)
  • Maximum memory is 10240 MB (10 GB)

Next Steps

Compute Factory

Provision compute through the higher-level factory pattern.

IAM Role

Scope execution permissions for your function.

SQS Queue

Configure the queues behind event sources and dead-letter destinations.

Secrets Manager

Inject secrets into your function at runtime.