I want to delete my AWS CloudFormation stack, but my Lambda function is stuck in DELETE_FAILED status
Short description
When you attempt to delete an AWS CloudFormation stack containing Lambda@Edge functions, you may encounter the following error that Lambda was unable to delete function because it is a replicated function. Deleting a CloudFormation stack with a Lambda@Edge function can fail because the lambda function is replicated across CloudFront's global edge locations. The Lambda function gets stuck in a DELETE_FAILED state with the following error:
Resource handler returned message: Lambda was unable to delete [FUNCTION_ARN] because it is a replicated function. Please see our documentation for Deleting Lambda@Edge Functions and Replicas.
This error occurs when attempting to delete an AWS Lambda@Edge function that has been replicated across multiple AWS regions and CloudFront edge locations. Lambda@Edge functions are designed to execute at CloudFront edge locations, and deleting a replicated function from the CloudFormation stack requires a specific process to ensure proper cleanup across all replicas.
Resolution
To resolve this issue:
-
Identify the Lambda@Edge Function Resource:
- Navigate to the CloudFormation console and locate the stack
- Find the Lambda@Edge function resource marked as DELETE_FAILED in the stack's resources
- Note down the function details for the next steps
-
Choose Your Deletion Approach:
You have three options to successfully delete the stack:
Option 1: Retain and Delete Later
- Navigate to CloudFormation stack which contains Lambda@Edge functions and failed to delete
- Reinitiate the Deletion process to retry deletion of Lambda function
- When prompted, select the checkbox to retain the Lambda@Edge function resource
- Continue with the stack deletion - this will remove all other resources while keeping the Lambda@Edge function
- After stack deletion completes, manually clean up the retained function:
- Navigate to the AWS Lambda console
- Locate the retained Lambda function
- Select the "Versions" tab to view function versions and replicas
- Delete all replicas and then the original function
Option 2: Delete Replicas First
- Navigate to the AWS Lambda console
- Locate the Lambda function specified in the error message
- Select the "Versions" tab to view function versions and replicas
- Delete all replicas
- Return to CloudFormation console and proceed with stack deletion - the stack deletion should now complete successfully
Option 3: Wait for Automatic Cleanup
Wait for all function replicas to be deleted from edge locations before attempting to delete the stack
Conclusion
When dealing with Lambda@Edge function deletion failures in CloudFormation stacks, it's important to understand that the replication nature of these functions requires special handling during deletion. The three approaches outlined above provide flexible options depending on your specific needs: retaining the function for later cleanup, manual deletion first, or waiting for automatic replica cleanup.
Co-Author: Kirtan Gajjar