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

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

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

With CORS

Performance Configuration

High Memory Function

ARM Architecture

Event Sources

The LambdaFunction 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 no tags prop. Apply tags through the CDK Tags aspect after construction.

Best Practices

  1. Keep functions small and focused on single tasks
  2. Use layers for shared dependencies
  3. Set appropriate timeouts (not always the maximum)
  4. Use environment variables for configuration
  5. Use PolicyStatement arrays for least-privilege IAM
  6. Consider ARM (Graviton2) for cost savings
  7. 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.