How do I resolve the AWS CloudFormation error "Cannot update a stack when a custom-named resource requires replacing"?

Last updated: 2019-10-31

When I try to update an AWS CloudFormation stack, I get an error message similar to the following: "CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename 'MYResourceXXX' and update the stack again." How can I resolve this error?

Short Description

This error typically occurs when a stack update tries to replace resources that have properties with custom names. AWS CloudFormation doesn't replace a resource that has a custom name unless that custom name is changed to a different name. To prevent a stack failure and avoid the error message, change any resources with custom names to use different names before you update a stack.

The resolution for this error assumes the following:

  • You're updating an existing stack and not creating a new stack.
  • You're changing the names of existing custom-named resources and not creating new custom-named resources.

Resolution

1.    In a code editor, open the AWS CloudFormation template for the stack that you want to update.

2.    Replace the names, or values, of any resource properties that have custom names with different names.

Note: In the following example, the DBInstanceIdentifier property of the MyRDS resource is set to the custom name PRODdb. The AWS CloudFormation stack can't be successfully updated until the name PRODdb is changed to a different name, such as PRODdb1. Or, you can omit the DBInstanceIdentifier property from your template. That way, AWS CloudFormation generates a unique physical ID to use for the DB instance.

"MyRDS": {
    "Type": "AWS::RDS::DBInstance",
    "Properties": {
        "DBInstanceClass": "db.m3.medium",
        "Engine": "MySQL",
        "DBInstanceIdentifier": "PRODdb",
        "AllocatedStorage": "10",
        "AutoMinorVersionUpgrade": "true",
        "BackupRetentionPeriod": "0",
    …
    …
    }
}

Important: When you rename a custom-named resource, AWS CloudFormation replaces that resource. For more information, see Name Type. Replacement of certain resources can cause data loss without proper backup. For more information, see Replacement.

3.    Save the changes to your AWS CloudFormation template, and then use the template to update your stack.