Short answer: (requires replacement) next to a property in cdk diff means that property can't be changed on the existing resource. CloudFormation will create a new resource with a new physical ID (a new name, ARN or ID), point everything in the stack at it, and then delete the old one unless a policy says to keep it. For anything that holds data, such as a bucket, table or database, the new one starts empty. Check before you deploy, and if you didn't mean to cause it, change the code so it doesn't.
What it looks like
This is real cdk diff output. One property that requires replacement is enough to replace the whole resource, even when the others could change in place:
[~] AWS::RDS::DBInstance Db Db replace
├─ [~] AllocatedStorage
│ ├─ [-] 20
│ └─ [+] 30
├─ [~] DBInstanceClass
│ ├─ [-] db.t4g.micro
│ └─ [+] db.t4g.small
└─ [~] DBName (requires replacement)
├─ [-] shop
└─ [+] orders
[~] AWS::EC2::Instance Worker Worker may be replaced
└─ [~] InstanceType (may cause replacement)
├─ [-] t3.micro
└─ [+] t3.small
The word at the end of the resource line sums it up: replace for the database, and may be replaced for the instance.
Update, interruption or replacement
CloudFormation updates a changed resource in one of three ways. Which one depends on the property you changed:
| Update behaviour | What happens |
|---|---|
| No interruption | Updated in place. Same physical ID, and it keeps working during the update |
| Some interruption | Updated in place, but it may be briefly unavailable (an EC2 instance restarting, for example) |
| Replacement | A new resource with a new physical ID. The old one is usually deleted afterwards |
(may cause replacement) means the property's behaviour is conditional. For example, AWS's reference for AWS::EC2::Instance says that changing an EBS-backed instance's type restarts or replaces it depending on whether the old and new types are compatible, while an instance with an instance-store root volume is always replaced.
Which changes force it
Don't guess, and don't generalise from one resource to another: the answer is per property. The CloudFormation reference page for each resource type lists every property with an Update requires line: No interruption, Some interruptions, or Replacement.
cdk diff gets its markings from CDK's own copy of CloudFormation's resource specification, and that can be stricter than the reference pages. For example, the AWS::EC2::Instance page lists InstanceType as "Some interruptions", with the note about replacement in its description, while cdk diff marks it (may cause replacement). When the two disagree, a change set (below) gives CloudFormation's own answer.
Some changes that replace a resource:
- Changing a fixed name, such as
bucketNameon an S3 bucket,tableNameon a DynamoDB table orinstanceIdentifieron an RDS instance. Each of those reference pages warns that if you set the name yourself, you can't make updates that require replacement unless you also give it a new name. - Changing a property that's part of what the resource is, such as
DBNameabove, which names the first database created inside the instance. AWS lists it as "Update requires: Replacement". - Renaming or moving the construct in your CDK code. This doesn't show as
(requires replacement)at all. The logical ID changes, so the diff shows one resource removed ([-]) and a new one added ([+]). To CloudFormation that's a different resource, which is a replacement in all but name.
The CDK diff explainer flags all three, including the removed-and-added pairs that look like renames.
When it loses data
Replacement is usually harmless for things that hold no data, such as an IAM role or a Lambda function. But anything outside the stack that stored the old name or ARN will still point at the old one, and a fixed roleName or functionName blocks the replacement just as a fixed bucket name does. For things that hold data, what happens to the old resource is decided by its UpdateReplacePolicy:
| UpdateReplacePolicy | The old resource |
|---|---|
| Delete (the default) | Deleted, with everything in it |
| Retain | Kept in your account, outside the stack, still costing money. Your app uses the new, empty one |
| Snapshot | A snapshot is taken, then it's deleted. Only for types that support snapshots, such as RDS |
In CDK, removalPolicy sets both DeletionPolicy and UpdateReplacePolicy. Buckets and DynamoDB tables default to RETAIN, and RDS DatabaseInstance to SNAPSHOT, so with CDK's defaults a replaced bucket isn't deleted and a replaced database is snapshotted first. But the stack now points at a new, empty bucket, and the old one is no longer managed. Nothing is lost, but your app's data isn't where your app looks.
A resource with a fixed name (one you set yourself, such as bucketName or instanceIdentifier) has another problem: CloudFormation can't create the new one while the old one still has that name. So it can't replace it unless the name changes too, and the deploy fails.
How to check before you deploy
- Run
cdk diffwith a change set. The default--method=autocreates one when it can, which gives CloudFormation's own answer. A template-only diff (--method=template, or the fallback when a change set can't be created) marks every requires-replacement property as a replacement, even when the change is cosmetic. - Read the change set itself for a definitive answer. Each resource change has a
Replacementfield ofTrue,FalseorConditional.Conditionalmeans CloudFormation can't tell until it runs: either the property's own behaviour is conditional (likeInstanceType), or the new value comes from another resource that may itself be replaced. When a resource has several changes, the one with the most impact decides. - Make CI stop.
cdk diff --fail-on=destructiveexits with code 1 and lists everything that would or may be replaced, deleted or orphaned.cdk deploy --require-approval=destructiveasks for approval before deploying any stack with such a change, and deploys stacks without one unattended. In CI, with no terminal to ask in, a stack that needs approval fails instead.
Paste the diff into the CDK diff explainer to see the replacements first, with what each one loses.
Ways to avoid it
- Make the change another way. If a property requires replacement, ask whether you need to change it at all. If you do, create the new resource alongside the old one, move the data or traffic across, and then remove the old one.
- Keep logical IDs stable. Don't rename or move constructs that hold data. If you must, keep the old ID with
overrideLogicalId()on the construct'snode.defaultChild.cdk refactorcan move resources to new logical IDs without replacing them, but it's in preview and needs--unstable=refactor. - Set a removal policy on anything you can't lose.
removalPolicy: RemovalPolicy.RETAINkeeps the old resource on replacement and on removal.RemovalPolicy.SNAPSHOTtakes a snapshot first, for resources that support it. - Avoid fixed names (
bucketName,tableName) unless something outside the stack needs them. A generated name can be replaced; a fixed one blocks the deploy. - Back up first. Before a deploy you know will replace a database, take a snapshot yourself, and plan how your app will cope while the new one is created.
