The LambdaFunctionConstruct provides a comprehensive, production-ready Lambda function with configurable OpenTelemetry support, IAM management, and environment configuration.
- 📈 OpenTelemetry Integration: Configurable AWS OpenTelemetry collector layer with version and architecture support
- 🛡️ IAM Management: Automatic role and policy creation with CloudWatch Logs permissions
- ⚙️ Environment Configuration: Easy environment variable management
- 🔗 Function URLs: Optional HTTP endpoint generation
- 🚀 SnapStart Support: Improved cold start performance for Java runtimes
- 🏷️ Versioning & Aliases: Automatic version management with "live" alias
- 🔑 Lambda Permissions: Multi-target permission management (function, version, alias)
using Amazon.CDK;
using LayeredCraft.Cdk.Constructs;
using LayeredCraft.Cdk.Constructs.Models;
public class MyStack : Stack
{
public MyStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)
{
var lambda = new LambdaFunctionConstruct(this, "MyLambda", new LambdaFunctionConstructProps
{
FunctionName = "my-api",
FunctionSuffix = "prod",
AssetPath = "./lambda-deployment.zip",
RoleName = "my-api-role",
PolicyName = "my-api-policy",
IncludeOtelLayer = true // Enable OpenTelemetry (disabled by default in v2.0+)
});
}
}| Property | Type | Description |
|---|---|---|
FunctionName |
string |
Base name of the Lambda function |
FunctionSuffix |
string |
Suffix appended to function name (e.g., "prod", "dev") |
AssetPath |
string |
Path to Lambda deployment package |
RoleName |
string |
Name of the IAM role |
PolicyName |
string |
Name of the IAM policy |
| Property | Type | Default | Description |
|---|---|---|---|
MemorySize |
double |
1024 |
Memory allocation in MB |
TimeoutInSeconds |
double |
6 |
Function timeout in seconds |
PolicyStatements |
PolicyStatement[] |
[] |
Additional IAM policy statements |
EnvironmentVariables |
IDictionary<string, string> |
{} |
Environment variables |
IncludeOtelLayer |
bool |
false |
Enable OpenTelemetry layer |
OtelLayerVersion |
string |
"0-117-0" |
OpenTelemetry layer version |
Architecture |
string |
"amd64" |
Lambda architecture (amd64/arm64) |
Permissions |
List<LambdaPermission> |
[] |
Lambda invocation permissions |
EnableSnapStart |
bool |
false |
Enable SnapStart for improved cold starts |
GenerateUrl |
bool |
false |
Generate Function URL for HTTP access |
var dynamoPolicy = new PolicyStatement(new PolicyStatementProps
{
Actions = ["dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:Query"],
Resources = ["arn:aws:dynamodb:us-east-1:123456789012:table/MyTable"],
Effect = Effect.ALLOW
});
var lambda = new LambdaFunctionConstruct(this, "MyLambda", new LambdaFunctionConstructProps
{
FunctionName = "my-api",
FunctionSuffix = "prod",
AssetPath = "./lambda-deployment.zip",
RoleName = "my-api-role",
PolicyName = "my-api-policy",
PolicyStatements = [dynamoPolicy],
EnvironmentVariables = new Dictionary<string, string>
{
{ "TABLE_NAME", "MyTable" },
{ "AWS_REGION", "us-east-1" }
}
});var lambda = new LambdaFunctionConstruct(this, "MyLambda", new LambdaFunctionConstructProps
{
FunctionName = "my-api",
FunctionSuffix = "prod",
AssetPath = "./lambda-deployment.zip",
RoleName = "my-api-role",
PolicyName = "my-api-policy",
GenerateUrl = true, // Creates HTTP endpoint
MemorySize = 2048,
TimeoutInSeconds = 30
});
// Access the function URL domain
var functionUrl = lambda.LiveAliasFunctionUrlDomain;var apiPermission = new LambdaPermission
{
Principal = "apigateway.amazonaws.com",
Action = "lambda:InvokeFunction",
SourceArn = "arn:aws:execute-api:us-east-1:123456789012:abcdef123/*"
};
var lambda = new LambdaFunctionConstruct(this, "MyLambda", new LambdaFunctionConstructProps
{
FunctionName = "my-api",
FunctionSuffix = "prod",
AssetPath = "./lambda-deployment.zip",
RoleName = "my-api-role",
PolicyName = "my-api-policy",
Permissions = [apiPermission]
});var lambda = new LambdaFunctionConstruct(this, "MyLambda", new LambdaFunctionConstructProps
{
FunctionName = "my-api",
FunctionSuffix = "prod",
AssetPath = "./lambda-deployment.zip",
RoleName = "my-api-role",
PolicyName = "my-api-policy",
EnableSnapStart = true // Improves cold start performance
});var lambda = new LambdaFunctionConstruct(this, "MyLambda", new LambdaFunctionConstructProps
{
FunctionName = "my-api",
FunctionSuffix = "prod",
AssetPath = "./lambda-deployment.zip",
RoleName = "my-api-role",
PolicyName = "my-api-policy",
IncludeOtelLayer = true, // Enable OpenTelemetry layer
Architecture = "arm64", // Use ARM64 architecture
OtelLayerVersion = "0-117-0" // Specify OTEL layer version
});!!! warning "Breaking Change in v2.0.0"
Starting with version 2.0.0, the OpenTelemetry layer is disabled by default. You must explicitly set IncludeOtelLayer = true to enable it. This change allows for better control over observability costs and layer dependencies.
Access the underlying CDK Lambda function for advanced configuration:
var lambda = new LambdaFunctionConstruct(this, "MyLambda", props);
var underlyingFunction = lambda.LambdaFunction;
// Add additional configuration
underlyingFunction.AddEnvironment("CUSTOM_VAR", "value");Get the domain of the Function URL (if enabled):
var lambda = new LambdaFunctionConstruct(this, "MyLambda", new LambdaFunctionConstructProps
{
// ... other props
GenerateUrl = true
});
var domain = lambda.LiveAliasFunctionUrlDomain; // Returns the domain stringThe Lambda functions use the following runtime configuration:
!!! info "Runtime Details"
- Runtime: PROVIDED_AL2023 (Amazon Linux 2023)
- Handler: bootstrap (for custom runtimes)
- Architecture: Configurable (amd64/arm64, default: amd64)
- Log Retention: 2 weeks
- OpenTelemetry Layer: Configurable AWS managed layer (disabled by default in v2.0+)
The construct automatically creates:
-
CloudWatch Logs Permissions:
logs:CreateLogStreamlogs:CreateLogGrouplogs:TagResourcelogs:PutLogEvents
-
Custom Policy Statements: Any additional policies you provide
-
Lambda Permissions: Applied to function, version, and alias
- Creates a new version on every deployment
- Maintains a "live" alias pointing to the latest version
- Versions have
RemovalPolicy.RETAINto prevent deletion
See the Testing Guide for comprehensive testing utilities and patterns specific to the Lambda Function construct.
For more real-world examples, see the Examples section.