Best practice guidance to avoid "CloudFormation cannot update a stack when a custom-named resource requires replacing"
Over the years we have taken the approach of naming everything we deploy — it's clean, orderly and unambiguous.
Since embracing infastructure-as-code practices, our CloudFormation recipes have been written to name everything with the project's prefix and stage. For example, a VPC will be deployed as
projectname-vpc-dev, and its subnets will be
Unfortunately, it seems some AWS resources won't update via CF if they are named — CloudFormation returns an error like this:
CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename <name> and update the stack again.
How should we best overcome this? Should we simply avoid naming things? Can we use tags instead to avoid this? What's best practice?
For reference, here's a snippet of CloudFormation that appears to be causing the issue above (with serverless.yml variables):
Type: AWS::EC2::SecurityGroup Properties: GroupName: projectname-dev GroupDescription: Security group for projectname-dev ...
I also had the same problem previously with
Generally speaking, how do I know which CloudFormation settings block stack updates like this? It feels like a bit of whack-a-mole at present. For the above example the docs at https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-security-group.html say nothing of this behaviour, but it does say "update requires replacement" against the fields
GroupDescription. Is that what I need to look out for, or is that something different again?
Thanks in advance... Scott
From the notes, I understand that you are getting an error “CloudFormation cannot update a stack when a custom-named resource requires replacing”, while trying to updating an existing stack. Please correct me if I misunderstood.
Please note that 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. I understand that in your case, you have been naming everything with the project's prefix and stage. Nonetheless, you can still use that method with slight changes.
This is how you can resolve the issue:
- In a code editor, open the AWS CloudFormation template for the stack that you want to update.
- Replace the names, or values, of any resource properties that have custom names with different names. You can refer to  on naming your resources.
- Save the changes to your AWS CloudFormation template, and then use the template to update your stack, you can check  on updating stacks directly.
In your case on step 2, you can change from e.g. projectname-dev to projectname-dev1. Another method is to omit the DBInstanceIdentifier property from your template. However, in this case AWS CloudFormation will generate a unique physical ID to use for the DB instance.
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.
I hope you find the above information helpful. Have a great day ahead!
Best practice guidance to avoid "CloudFormation cannot update a stack when a custom-named resource requires replacing"asked 15 days ago
Cloudformation-based update of RDS DBSnapshotIdentifier fails with "failed validation constraint for keyword [pattern]"asked 5 months ago
Cloudformation exports exists after stack is deletedasked 6 months ago
Is there any limit to the number or age of events logged in CloudFormation event history?Accepted Answerasked 8 months ago
Create a notification once CloudFormation StackSet is finishedasked 5 months ago
Have a service running in an ECS cluster dynamically create a new service in the same clusterasked 20 days ago
Recent update to Code Deploy now breaking build (we made no changes)asked a year ago
Cloudformation Stack Stuck in UPDATE_IN_PROGRESSasked 2 years ago
Every stack update tries to optimize gp3 volumeasked a year ago
SAM deploy error - Template format error: Unrecognized resource types: [AWS::Athena::NamedQuery]asked 3 months ago