cdkd deploy: names prefixed before v0.94.0
If a stack was deployed with a cdkd older than v0.94.0, or with
--prefix-user-supplied-names, a later deploy without that flag can replace
some of its resources. This page explains why, how to see it coming, and gives
the three ways to handle it.
Changed in v0.94.0: cdkd uses a name you declared in CDK code as written. Earlier versions put the stack name in front of it for these types: IAM Role, User, Group, InstanceProfile and ManagedPolicy, and ELBv2 LoadBalancer and TargetGroup.
Why the deploy replaces resources
Take this role in a stack named MyStack:
new iam.Role(this, 'DeployRole', {
roleName: 'my-role',
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
});
An older cdkd created it as MyStack-my-role. On v0.94.0 or later the same
template means my-role. A role cannot be renamed in place, so the next deploy
that updates the role replaces it: it creates my-role and deletes
MyStack-my-role, as CloudFormation replaces a renamed resource. An IAM
managed policy is renamed the same way.
The other types keep their prefixed name. Their updates never change the name, so they are not replaced. A deploy that leaves a prefixed role or policy unchanged does not rename it either.
The same holds the other way: a deploy with --prefix-user-supplied-names on
a stack whose role is recorded as my-role replaces it with
MyStack-my-role when it updates it, and cdkd diff with the flag shows it.
Seeing it before you deploy
cdkd diff previews the same naming. Each updated resource that the deploy
renames shows as a replacement:
[~] DeployRole (AWS::IAM::Role)
[requires replacement: its name drops the stack-name prefix an earlier --prefix-user-supplied-names deploy gave it; created under the new name first, the old one deleted once the deploy succeeds, or kept under UpdateReplacePolicy: Retain]
The new role is created first. The old one is deleted only after every other
change of the deploy succeeded, so a deploy that fails gets the old role back
as it was. With UpdateReplacePolicy: Retain on the role (in CDK,
cfnOptions.updateReplacePolicy on its CfnRole), the old role is kept and
no longer tracked by cdkd.
A role whose only change is a value read from a NoEcho parameter is not
listed: cdkd compares that value with AWS during the deploy. If it moved, the
deploy prints the warning below and --require-approval asks about the
replacement then, before anything is sent; declined, nothing is sent for it.
It is counted by cdkd diff --fail-on=destructive, listed in the --json
output's destructiveChanges (and as prefixRename on the change), and
asked about by cdkd deploy --require-approval destructive. Pass
--prefix-user-supplied-names to cdkd diff, or set it as below, to preview
the deploy that keeps the prefix.
Resources in the same stack that read a replaced resource follow it in the
same deploy, even when the plan lists them as unchanged: a managed policy
attached through its roles is attached to the renamed role, and one whose
reference is create-only is replaced too.
Three ways to handle it
The options are listed from the most conservative to the least.
| Option | Effect |
|---|---|
Pass --prefix-user-supplied-names for that stack |
Nothing in AWS changes. |
| Accept the replacement | One replacement per renamed resource, after the prompt below. |
Remove the explicit roleName, managedPolicyName and so on from your CDK code |
Also one replacement, and CDK generates a stable name from then on. |
To keep the old names without passing the flag on every deploy, set it in
cdk.json. cdkd diff reads the same setting, as well as
CDKD_PREFIX_USER_SUPPLIED_NAMES=true:
{
"context": {
"cdkd": { "prefixUserSuppliedNames": true }
}
}
The confirmation prompt
After it plans the deploy, and before it calls AWS to change anything,
cdkd deploy lists the resources the plan replaces for the rename and asks:
WARNING: --no-prefix-user-supplied-names will REPLACE 2 resource(s) whose
AWS physical name is still prefixed with the stack name:
- MyRole (AWS::IAM::Role): from MyStack-my-role to my-role
- MyPolicy (AWS::IAM::ManagedPolicy): from arn:aws:iam::123456789012:policy/MyStack-my-policy to arn:aws:iam::123456789012:policy/my-policy
These resources will be REPLACED because the new naming convention drops
the stack-name prefix.
Continue? (y/N):
The prompt defaults to no. If you decline, the deploy exits and nothing is
modified. Pass -y / --yes to confirm the prompt in CI. Without it, cdkd
refuses a run that has no terminal.
The stack lock is held while the prompt is open, and declining releases it.
A rename that only shows up while the deploy runs (a replacement AWS proves
mid-deploy) is printed with the same warning but not asked about: the deploy
is already under way. --require-approval asks its own question about that
replacement.
Which resources are listed
A resource is listed only when the deploy replaces it and its physical name is exactly the prefixed form of the name you declared.
- A name that you built from the stack name yourself, such as
roleName: `${this.stackName}-role`, was already used as written. It is not listed. - A prefixed role or policy that this deploy does not change is not listed: it keeps its name until a deploy updates it.
- The check does nothing on a first deploy, on a stack that never used the
prefix, or when
--prefix-user-supplied-namesis set.
Related
- cdkd deploy: tuning: the flag and where to set it
- cdkd diff: the preview, its
--fail-onand--json - cdkd deploy: safety & compatibility flags: the other guards around replacements