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
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.
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:
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
TheinlinePolicy 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.
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
batchSize 10, reportBatchItemFailures true. maxBatchingWindow is in seconds.
DynamoDB Stream Trigger
startingPosition "LATEST", batchSize 100, reportBatchItemFailures true. retryAttempts and parallelizationFactor are also accepted.
S3 Trigger
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 firstaddS3EventSource call also creates an SQS queue and wires it as the function’s dead-letter destination.
Reuse an existing queue or opt out entirely:
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.
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 whenenableFunctionUrlis true)
Complete Example
Tagging
The construct takes notags prop. Apply tags through the CDK Tags aspect after construction.
Best Practices
- Keep functions small and focused on single tasks
- Set appropriate timeouts rather than defaulting to the maximum
- Cap public endpoints with
reservedConcurrentExecutions - Keep the async failure queue on any function fed by S3 events
- Use PolicyStatement arrays for least-privilege IAM
- Consider ARM (Graviton2) for cost savings
- Use
secretsandsecretsImportfor 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.