fix(cloudformation): replace an S3 bucket when the engine says its create-only properties changed (#8453)

* fix(cloudformation): replace an S3 bucket when the engine says its create-only properties changed
* test(cloudformation): pin S3 bucket replacement when the name is removed
* fix(cloudformation): list a bucket a failed replacing update could not remove
This commit is contained in:
Oleksandr Nikolaichuk authored and GitHub committed 2026-11-15 09:22:03 -04:00
1 parent 7bf99c0a48
commit 2688ed5180
2 files changed
+32 -9

No files matched your search

@@ -90,8 +90,18 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {
private void provisionBucket(StackResource r, JsonNode props, ProvisionContext ctx) {
Map<String, String> attributesBefore = Map.copyOf(r.getAttributes());
String bucketName = ctx.stablePhysicalName(ctx.resolveOptional(props, "BucketName"),
r.getLogicalId(), BUCKET_NAME_MAX_LENGTH, true);
String explicitName = ctx.resolveOptional(props, "BucketName");
// The engine's change decides, as it does for the change set's Replacement: BucketName,
// BucketNamePrefix and BucketNamespace are create-only in the registry. A bucket named in the
// template cannot be replaced under that name; an unnamed one gets a fresh generated name.
boolean replaces = ctx.isUpdate() && ctx.priorPhysicalId() != null
&& ctx.changes(BUCKET, props).requiresReplacement();
if (replaces && explicitName != null && explicitName.equals(ctx.priorPhysicalId())) {
throw CfnExceptions.customNamedReplacement(explicitName);
}
String bucketName = replaces && (explicitName == null || explicitName.isBlank())
? ctx.generatePhysicalName(r.getLogicalId(), BUCKET_NAME_MAX_LENGTH, true)
: ctx.stablePhysicalName(explicitName, r.getLogicalId(), BUCKET_NAME_MAX_LENGTH, true);
// provision is also the update path. CreateBucket on a bucket this account already owns is
// idempotent only in us-east-1; every other region answers BucketAlreadyOwnedByYou, as on
// AWS. With the name now stable across updates, the second UpdateStack must skip the create
@@ -328,6 +338,10 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {
if (ctx.isUpdate()) {
r.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR,
"Could not restore bucket " + bucketName + ": " + e.getMessage());
if (unwind.created) {
// The bucket this update created stays listed, so a later commit or the stack delete removes it.
ReplacementCleanup.recordOrphan(r, bucketName, BUCKET, ctx.region());
}
}
}
}
@@ -104,12 +104,15 @@ class CfnReplacementIntegrationTest {
"True", true, "repl-sng-b");
private static final String SQS_TYPE = "AWS::SQS::Queue";
private static final String QUEUE_NAME_IF =
"{\"Fn::If\": [\"Named\", \"repl-q-if\", {\"Ref\": \"AWS::NoValue\"}]}";
private static final String S3_TYPE = "AWS::S3::Bucket";
/** A queue named by {@code Fn::If} on {@code Named}, which is {@code Fn::Equals} of {@code equalsArgs}. */
private static String namedByCondition(String equalsArgs) {
return subjectStack("", "", SQS_TYPE, "\"QueueName\": " + QUEUE_NAME_IF).replace("\"Resources\"",
/**
* A {@code type} whose {@code nameProperty} is {@code name} by {@code Fn::If} on {@code Named}, which is
* {@code Fn::Equals} of {@code equalsArgs}, and {@code AWS::NoValue} otherwise.
*/
private static String namedByCondition(String type, String nameProperty, String name, String equalsArgs) {
return subjectStack("", "", type, "\"" + nameProperty + "\": {\"Fn::If\": [\"Named\", \"" + name
+ "\", {\"Ref\": \"AWS::NoValue\"}]}").replace("\"Resources\"",
"\"Conditions\": {\"Named\": {\"Fn::Equals\": " + equalsArgs + "}}, \"Resources\"");
}
@@ -147,8 +150,14 @@ class CfnReplacementIntegrationTest {
createOnlyRow("SQS queue explicit QueueName removed", "", "", SQS_TYPE,
"\"QueueName\": \"repl-q-removed\"", "\"VisibilityTimeout\": 30", "True", false, null),
new Row("SQS queue QueueName dropped by Fn::If choosing AWS::NoValue",
namedByCondition("[\"x\", \"x\"]"),
namedByCondition("[\"x\", \"y\"]"),
namedByCondition(SQS_TYPE, "QueueName", "repl-q-if", "[\"x\", \"x\"]"),
namedByCondition(SQS_TYPE, "QueueName", "repl-q-if", "[\"x\", \"y\"]"),
"Subject", "True", true, null),
createOnlyRow("S3 bucket explicit BucketName removed", "", "", S3_TYPE,
"\"BucketName\": \"repl-s3-removed\"", "", "True", false, null),
new Row("S3 bucket BucketName dropped by Fn::If choosing AWS::NoValue",
namedByCondition(S3_TYPE, "BucketName", "repl-s3-if", "[\"x\", \"x\"]"),
namedByCondition(S3_TYPE, "BucketName", "repl-s3-if", "[\"x\", \"y\"]"),
"Subject", "True", true, null),
new Row("deployed parameter renamed, same value",
locationStack(stringParameter("NameA", HOSTED), "", REF_A, "d"),