Custom Resources
What you'll learn: How to extend CloudFormation beyond AWS's built-in resources using Lambda functions for operations CloudFormation doesn't natively support.
The Core Idea
CloudFormation provides hundreds of resource types (EC2 instances, S3 buckets, IAM roles, etc.), but sometimes you need to do something AWS hasn't built in yet—like calling a third-party API, performing custom logic during stack creation, or managing non-AWS resources. Custom Resources let you fill these gaps by invoking your own Lambda function as part of the CloudFormation lifecycle.
Think of it like hiring a specialist contractor: when your standard construction crew (CloudFormation's native resources) can't handle a unique requirement, you bring in an expert (Lambda function) who knows exactly what to do.
How It Works
When you declare a custom resource in your template, CloudFormation sends a request to an AWS Lambda function or SNS topic during Create, Update, or Delete operations. Your Lambda function performs whatever custom logic you need—calling external APIs, complex calculations, conditional operations—then signals back to CloudFormation whether it succeeded or failed.
The custom resource declaration looks like a regular resource but uses type AWS::CloudFormation::CustomResource or Custom::YourName. You specify the Lambda function ARN in the ServiceToken property.
Your Lambda function receives event data with the request type (Create/Update/Delete) and must send a response back to a CloudFormation-provided URL, signaling SUCCESS or FAILED.
Common Use Cases
- Managing resources in third-party services (Datadog, PagerDuty)
- Generating random passwords or unique identifiers
- Looking up values from external systems
- Cleaning up resources CloudFormation can't delete automatically
- Implementing complex validation logic
Key Takeaway: Custom Resources extend CloudFormation's capabilities by letting you write Lambda functions that handle Create, Update, and Delete operations for anything CloudFormation doesn't natively support.