DEV Community

Dealing with CDK Custom Resources and failures.

The AWS CustomResource construct simplifies AWS SDK calls during CloudFormation deployments, commonly used for tasks like fetching Parameter Store values or invoking Lambda functions. It employs a singleton Lambda function to execute SDK calls during CloudFormation lifecycle events (CREATE, UPDATE, DELETE), storing responses in S3. However, if the invoked Lambda function fails, the CloudFormation deployment might succeed unexpectedly. This occurs because the singleton Lambda's success or failure only reflects the API call's status, not the invoked Lambda's. To address this, a custom provider replaces the default singleton Lambda. This custom provider, using either the Provider Framework or a direct Lambda approach, offers finer control over deployment success or failure based on the custom resource Lambda's outcome. The Provider Framework simplifies response handling by automatically sending responses to the S3 bucket; direct Lambda handling requires manual PUT requests to a pre-signed S3 URL. AWS recommends the Provider Framework. Custom resources, by default, only execute on UPDATE when properties change; a timestamp can force execution. The provider Lambda function executes on all lifecycle events, requiring conditional logic within the Lambda function itself. Careful consideration of lifecycle events (CREATE, UPDATE, DELETE) is crucial for proper function behavior.
favicon
dev.to
dev.to
Image for the article: Dealing with CDK Custom Resources and failures.