Skip to content

How should I Handle Lambda@Edge Function Deletion Failure in CloudFormation Stacks

3 minute read
Content level: Intermediate
2

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:

  1. 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
  2. Choose Your Deletion Approach: You have three options to successfully delete the stack:

Option 1: Retain and Delete Later

  1. Navigate to CloudFormation stack which contains Lambda@Edge functions and failed to delete
  2. Reinitiate the Deletion process to retry deletion of Lambda function
  3. When prompted, select the checkbox to retain the Lambda@Edge function resource
  4. Continue with the stack deletion - this will remove all other resources while keeping the Lambda@Edge function
  5. 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

  1. Navigate to the AWS Lambda console
  2. Locate the Lambda function specified in the error message
  3. Select the "Versions" tab to view function versions and replicas
  4. Delete all replicas
  5. 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

1 Comment

This guide provides a clear and detailed approach to troubleshooting Lambda@Edge function deletion failures in CloudFormation stacks. The step-by-step resolution methods, including detaching the function from CloudFront and manually deleting replicas, are particularly useful. It would be great to see additional insights on automating cleanup processes or common scenarios where such failures occur.

replied 8 months ago