fix(cloudformation): replace AWS::S3::Bucket when BucketName changes (#5762)

A change to BucketName is a replacement: CloudFormation creates the new
bucket first and then deletes the old one, unless UpdateReplacePolicy is
Retain. S3CfnProvisioner created the replacement but never removed the
displaced bucket, so a successful rename left both buckets, and a rename
that a later resource failed ended in UPDATE_ROLLBACK_FAILED, with
DescribeStackResources still naming the new bucket.

Record the displaced bucket with ReplacementCleanup and delegate the
replacement lifecycle to it, so the committed update deletes the old
bucket, honouring UpdateReplacePolicy: Retain, and a rollback points the
resource back at the original bucket and deletes the replacement. Route
the bucket delete through CfnDeletes.safeDelete so a bucket already gone
is tolerated while a real failure such as BucketNotEmpty still propagates
and reaches the stack as DELETE_FAILED.

Fixes #5678

Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
Matheus Oliveira authored and GitHub committed 2026-10-18 10:52:00 -04:00
1 parent 5dcddc9dfa
commit 8298779ead
4 files changed
+515 -13

No files matched your search

@@ -69,6 +69,19 @@ public final class ReplacementCleanup {
*/
static void record(StackResource r, ProvisionContext ctx, Map<String, String> attributesBeforeProvision,
boolean replaced) {
record(r, ctx, attributesBeforeProvision, replaced, true);
}
/**
* Records a replacement the provisioner decided on, spelling out whether this update created the
* entity under the new physical id. {@code replacementCreated} is false when that entity already
* existed and the provision only took it over, the way us-east-1's idempotent {@code CreateBucket}
* answers for a bucket the account already owns. The prior entity is still displaced, so a
* committed update deletes it, but {@link #rollback} points the resource back without deleting an
* entity this update never created.
*/
static void record(StackResource r, ProvisionContext ctx, Map<String, String> attributesBeforeProvision,
boolean replaced, boolean replacementCreated) {
ObjectNode cleanup = readOrEmpty(r);
cleanup.remove("priorPhysicalId");
cleanup.remove("undeletedReplacement");
@@ -88,6 +101,11 @@ public final class ReplacementCleanup {
addDisplaced(cleanup, prior, r.getResourceType(), ctx.region(), true);
}
}
if (replaced && !replacementCreated) {
cleanup.put("replacementPreexisting", true);
} else {
cleanup.remove("replacementPreexisting");
}
write(r, cleanup);
}
@@ -104,6 +122,10 @@ public final class ReplacementCleanup {
* propagates so the stack reports it. A retry (CloudFormation {@code ContinueUpdateRollback})
* re-attempts that failed delete. Returns false when this update replaced nothing, which
* leaves the engine's handling of in-place updates unchanged.
*
* <p>A replacement this update did not create is left standing: the resource points back to the
* prior entity, and the entity under the new id, whether another resource created it or it was
* already owned, is never touched by a rollback this update did not make.
*/
static boolean rollback(StackResource r, Deleter deleter) {
ObjectNode cleanup = read(r);
@@ -137,10 +159,15 @@ public final class ReplacementCleanup {
}
cleanup.path("priorAttributes").fields()
.forEachRemaining(e -> r.getAttributes().put(e.getKey(), e.getValue().asText()));
boolean replacementPreexisting = cleanup.path("replacementPreexisting").asBoolean(false);
cleanup.remove("priorPhysicalId");
cleanup.remove("priorAttributes");
cleanup.remove("replacementPreexisting");
removeDisplaced(cleanup, prior);
write(r, cleanup);
if (replacementPreexisting) {
return true;
}
if (prior.equals(replacement) && !CfnResourceDispatcher.isStub(prior, r.getAttributes())) {
return true;
}
@@ -20,6 +20,7 @@ import org.jboss.logging.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
@@ -80,6 +81,7 @@ 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);
// provision is also the update path. CreateBucket on a bucket this account already owns is
@@ -88,6 +90,7 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {
// and only reconcile the bucket's configuration. A replacing update derives a different
// name and still creates, hence reusesPriorEntity rather than isUpdate.
Unwind unwind = new Unwind();
boolean replacementCreated = true;
r.getAttributes().remove(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR);
try {
// Built before anything is created, so a configuration that fails validation creates nothing.
@@ -103,6 +106,7 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {
boolean owned = s3Service.ownsBucket(bucketName);
s3Service.createBucket(bucketName, ctx.region());
unwind.created = !owned;
replacementCreated = !owned;
}
// Ahead of the other properties, so a destination S3 cannot validate fails the resource
// before any of them has changed the bucket. An unchanged configuration is not put again:
@@ -138,6 +142,12 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {
}
r.getAttributes().put("WebsiteURL", AwsEndpoints.s3WebsiteUrl(bucketName, ctx.region()));
r.getAttributes().put("BucketName", bucketName);
// A change to BucketName is create-only, so the engine replaces the resource: this update
// created the bucket above, and the one the prior physical id named is left for the cleanup
// once the update commits, or put back when a later resource fails the update. A name the
// account already owned was not created here, so a rollback must not delete it.
ReplacementCleanup.record(r, ctx, attributesBefore,
ctx.isUpdate() && !ctx.priorPhysicalId().equals(r.getPhysicalId()), replacementCreated);
}
private void applyBucketCorsConfiguration(String bucketName, JsonNode props, ProvisionContext ctx) {
@@ -307,28 +317,27 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {
@Override
public boolean rollbackUpdate(StackResource resource) {
if (!BUCKET.equals(resource.getResourceType())) {
return false;
}
String snapshot = resource.getAttributes().get(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR);
if (snapshot == null) {
return false;
if (snapshot != null) {
s3Service.restoreBucketConfiguration(resource.getPhysicalId(), snapshot);
resource.getAttributes().remove(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR);
return true;
}
s3Service.restoreBucketConfiguration(resource.getPhysicalId(), snapshot);
resource.getAttributes().remove(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR);
return true;
return ReplacementCleanup.rollback(resource, this::delete);
}
@Override
public UpdateCleanupResult completeUpdate(StackResource resource) {
return resource.getAttributes().containsKey(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR)
? new UpdateCleanupResult(true, true, null, 0, null)
: UpdateCleanupResult.notApplicable();
if (resource.getAttributes().containsKey(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR)) {
return new UpdateCleanupResult(true, true, null, 0, null);
}
return ReplacementCleanup.complete(resource, this::delete);
}
@Override
public void clearUpdate(StackResource resource) {
resource.getAttributes().remove(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR);
ReplacementCleanup.clear(resource);
}
private static boolean sameConfiguration(NotificationConfiguration desired, NotificationConfiguration current) {
@@ -619,8 +628,10 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {
public void delete(String resourceType, String physicalId, String region) {
if (BUCKET.equals(resourceType)) {
// Deleting a non-empty bucket raises BucketNotEmpty, which propagates so the stack
// reports DELETE_FAILED as AWS does.
s3Service.deleteBucket(physicalId);
// reports DELETE_FAILED as AWS does; a bucket already gone is treated as deleted, so a
// cleanup the update committed reaches an already-removed bucket without failing.
CfnDeletes.safeDelete("S3 bucket", physicalId,
() -> s3Service.deleteBucket(physicalId), "NoSuchBucket");
return;
}
if (BUCKET_POLICY.equals(resourceType)) {
@@ -628,6 +639,16 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {
}
}
@Override
public boolean hasReplacementUpdate(StackResource resource) {
return ReplacementCleanup.hasReplacement(resource);
}
@Override
public String updateCleanupPhysicalId(StackResource resource) {
return ReplacementCleanup.cleanupPhysicalId(resource);
}
/**
* Clears the policy the stack put on the bucket. AWS removes it on stack delete unless the
* resource carries {@code DeletionPolicy: Retain}, and leaving it behind is how a dropped