fix(cloudformation): drop a DynamoDB table's update snapshot once the update commits (#7448)

* fix(cloudformation): drop a DynamoDB table's update snapshot once the update commits

* test(cloudformation): clean up the rollback retry test's stack and table even when it fails, using a shared ContinueUpdateRollback helper
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-11-04 05:51:43 -04:00
1 parent 1743cb5f75
commit eaefd55980
3 files changed
+94

No files matched your search

@@ -562,6 +562,17 @@ public class DynamoDbCfnProvisioner implements CfnResourceProvisioner {
return resource.getAttributes().containsKey(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR); return resource.getAttributes().containsKey(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR);
} }
/**
* The engine calls this for every resource once a stack update commits. A committed update is
* never rolled back, so its snapshot is dropped here: left on the resource, a retried rollback
* of a later update would put the table back to its state before this one.
*/
@Override
public UpdateCleanupResult completeUpdate(StackResource resource) {
resource.getAttributes().remove(CfnRollback.DYNAMODB_TABLE_UPDATE_SNAPSHOT_ATTR);
return UpdateCleanupResult.notApplicable();
}
/** /**
* Puts back the tags, stream setting, TTL setting and Kinesis streaming destinations an in-place * Puts back the tags, stream setting, TTL setting and Kinesis streaming destinations an in-place
* table update changed. All four go back or none is claimed: restoring one alone would report a * table update changed. All four go back or none is claimed: restoring one alone would report a
@@ -15,6 +15,7 @@ import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
@@ -185,6 +186,69 @@ class CloudFormationDynamoDbTimeToLiveIntegrationTest {
deleteStack(stack); deleteStack(stack);
} }
/**
* A committed in-place update is never rolled back. A later replacing update fails and cannot
* delete the table it created, so its rollback fails; retrying that rollback must keep the tags
* the committed update set rather than put back the ones from before it.
*/
@Test
void retryingAFailedReplacementRollbackKeepsTheCommittedTags() {
String suffix = Long.toString(System.nanoTime(), 36);
String stack = "cfn-ddb-ttl-retry-" + suffix;
String table = "ttl-retry-table-" + suffix;
String replacement = "ttl-retry-new-" + suffix;
Throwable testFailure = null;
try {
cloudFormation(stack, "CreateStack", TEMPLATE.formatted(table, "dev", "", ""));
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status());
String tableArn = dynamoDb("DescribeTable", "{\"TableName\": \"" + table + "\"}")
.statusCode(200)
.extract().path("Table.TableArn");
cloudFormation(stack, "UpdateStack", TEMPLATE.formatted(table, "prod", "", ""));
assertEquals("UPDATE_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status());
failNextUpdateTimeToLive();
Mockito.doThrow(new IllegalStateException("simulated DeleteTable failure"))
.doCallRealMethod()
.when(dynamoDbService)
.deleteTable(anyString(), anyString());
try {
cloudFormation(stack, "UpdateStack", TEMPLATE.formatted(replacement, "prod", TTL_ENABLED, ""));
assertEquals("UPDATE_ROLLBACK_FAILED", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status());
} finally {
Mockito.doCallRealMethod().when(dynamoDbService)
.updateTimeToLive(anyString(), anyString(), anyBoolean(), anyString());
Mockito.doCallRealMethod().when(dynamoDbService).deleteTable(anyString(), anyString());
}
CfnStackWaits.continueUpdateRollback(stack, CFN_AUTH);
assertEquals("UPDATE_ROLLBACK_FAILED", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status(),
"a replacing table update still has no rollback, so the retry fails again");
dynamoDb("ListTagsOfResource", "{\"ResourceArn\": \"" + tableArn + "\"}")
.statusCode(200)
.body("Tags.Value", contains("prod"));
CfnStackWaits.continueUpdateRollback(stack, CFN_AUTH, "Orders");
assertEquals("UPDATE_ROLLBACK_COMPLETE", CfnStackWaits.awaitTerminal(stack, CFN_AUTH).status());
} catch (RuntimeException | Error failure) {
testFailure = failure;
throw failure;
} finally {
try {
assertAll("retry test cleanup",
() -> dynamoDb("DeleteTable", "{\"TableName\": \"" + replacement + "\"}"),
() -> deleteStack(stack));
} catch (RuntimeException | Error cleanupFailure) {
if (testFailure == null) {
throw cleanupFailure;
}
testFailure.addSuppressed(cleanupFailure);
}
}
}
/** A create that fails after the table exists rolls back to no table, not to an orphan. */ /** A create that fails after the table exists rolls back to no table, not to an orphan. */
@Test @Test
void aCreateThatFailsAfterTheTableExistsLeavesNoTable() { void aCreateThatFailsAfterTheTableExistsLeavesNoTable() {
@@ -178,6 +178,25 @@ public final class CfnStackWaits {
awaitStackDeleted(stackName, authorization); awaitStackDeleted(stackName, authorization);
} }
/**
* Sends ContinueUpdateRollback and checks it was accepted; the caller waits for the outcome.
* {@code authorization} may be null, and each of {@code resourcesToSkip} is sent as a
* ResourcesToSkip member.
*/
public static void continueUpdateRollback(String stackName, String authorization, String... resourcesToSkip) {
RequestSpecification request = given()
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "ContinueUpdateRollback")
.formParam("StackName", stackName);
if (authorization != null) {
request.header("Authorization", authorization);
}
for (int i = 0; i < resourcesToSkip.length; i++) {
request.formParam("ResourcesToSkip.member." + (i + 1), resourcesToSkip[i]);
}
request.when().post("/").then().statusCode(200);
}
/** /**
* Waits until DescribeStacks answers 400, which is how a missing stack is reported. Fails on * Waits until DescribeStacks answers 400, which is how a missing stack is reported. Fails on
* DELETE_FAILED or after ten seconds. * DELETE_FAILED or after ten seconds.