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
Basic Usage
Standard Function
Singleton Function
Configuration Options
Core Properties
Function Configuration
Function URL Configuration
Secrets Configuration
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
With CORS
Performance Configuration
High Memory Function
ARM Architecture
Event Sources
TheLambdaFunction class provides three built-in event-source methods: addSqsEventSource, addDynamoDbEventSource, and addS3EventSource.
SQS Trigger
DynamoDB Stream Trigger
S3 Trigger
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
Custom Resource Handler
Methods
Get Function URL
Get Execution Role
Get Function ARN
Get Function Name
Outputs
The Lambda Function creates these CloudFormation outputs:${id}FunctionArn- Function ARN${id}FunctionUrl- Function URL (when enabled)
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
- Use layers for shared dependencies
- Set appropriate timeouts (not always the maximum)
- Use environment variables for configuration
- Use PolicyStatement arrays for least-privilege IAM
- Consider ARM (Graviton2) for cost savings
- Use secrets for sensitive values rather than environment variables
Performance Tips
- Minimise package size to reduce cold starts
- Reuse connections outside 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.
Messaging Factory
Create SQS queues to use as Lambda event sources.
Load Balancer
Front your function with an Application Load Balancer.