fix(cloudformation): resolve a stack id only to the stack carrying it (#4973)

* fix(cloudformation): resolve a stack id only to the stack carrying it

resolveStack turned an ARN into its stack name and returned whatever live
stack held that name. After a stack was deleted and its name reused,
DescribeStacks with the old stack id returned the new live stack (new id,
CREATE_COMPLETE) instead of the retained deleted one, and DeleteStack with
the old id deleted the new stack.

An ARN now resolves to a live stack only when the stack ids match, so
DescribeStacks/DescribeStackEvents fall through to the retained deleted
record (DELETE_COMPLETE), DeleteStack on the old id is the existing no-op,
and other operations answer "Stack with id ... does not exist".

* fix(cloudformation): send the real stack id in custom resource events

The custom resource event carried a made-up StackId (stack/<name>/<random
uuid>), so a handler calling DescribeStacks(event.StackId) now gets
"does not exist" since stack ids resolve only to the stack carrying them.
Pass the engine's stack id on Create/Update and stash it for Delete, which
only sees the StackResource.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-10-02 23:25:05 -04:00
1 parent 036f55b196
commit 8e98c087fd
5 files changed
+67 -13

No files matched your search

@@ -7,6 +7,8 @@ import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
/**
* What a client polling a deleted stack by name gets back, per operation. The two wordings are
@@ -37,6 +39,44 @@ class CloudFormationDeletedStackByNameIntegrationTest {
.body(containsString("<Message>Stack with id " + stackName + " does not exist</Message>"));
}
/**
* A stack id names one stack. After the name is reused, the old id still resolves to the
* deleted stack (DELETE_COMPLETE) and the name resolves to the live one; a DeleteStack
* aimed at the old id must not reach the live stack that took the name.
*/
@Test
void oldStackIdResolvesToTheDeletedStackAfterTheNameIsReused() {
String stackName = "reused-name-" + Long.toString(System.nanoTime(), 36);
String template = "{\"Resources\":{\"P\":{\"Type\":\"AWS::SSM::Parameter\","
+ "\"Properties\":{\"Type\":\"String\",\"Value\":\"v\"}}}}";
String oldId = cfn("CreateStack", stackName, template).then().statusCode(200)
.extract().xmlPath().getString("CreateStackResponse.CreateStackResult.StackId");
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
cfn("DeleteStack", stackName, null).then().statusCode(200);
awaitGone(stackName);
String newId = cfn("CreateStack", stackName, template).then().statusCode(200)
.extract().xmlPath().getString("CreateStackResponse.CreateStackResult.StackId");
assertNotEquals(oldId, newId);
assertEquals("CREATE_COMPLETE", CfnStackWaits.awaitTerminal(stackName).status());
Response byOldId = cfn("DescribeStacks", oldId, null);
byOldId.then().statusCode(200);
assertEquals(oldId, byOldId.xmlPath().getString(
"DescribeStacksResponse.DescribeStacksResult.Stacks.member.StackId"));
assertEquals("DELETE_COMPLETE", byOldId.xmlPath().getString(
"DescribeStacksResponse.DescribeStacksResult.Stacks.member.StackStatus"));
cfn("DeleteStack", oldId, null).then().statusCode(200);
Response byName = cfn("DescribeStacks", stackName, null);
byName.then().statusCode(200);
assertEquals(newId, byName.xmlPath().getString(
"DescribeStacksResponse.DescribeStacksResult.Stacks.member.StackId"));
assertEquals("CREATE_COMPLETE", byName.xmlPath().getString(
"DescribeStacksResponse.DescribeStacksResult.Stacks.member.StackStatus"));
cfn("DeleteStack", stackName, null).then().statusCode(200);
}
@Test
void describeStackEventsOnAStackThatNeverExistedUsesTheSameWording() {
cfn("DescribeStackEvents", "never-created", null).then().statusCode(400)
@@ -35,6 +35,9 @@ class CustomResourceProvisionerTest {
private static final String SERVICE_TOKEN =
"arn:aws:lambda:us-east-1:000000000000:function:MyHandler";
private static final String STACK_ID =
"arn:aws:cloudformation:us-east-1:000000000000:stack/my-stack/1b2c3d4e-0000-0000-0000-000000000001";
private final ObjectMapper mapper = new ObjectMapper();
private LambdaService lambdaService;
private CustomResourceResponseStore store;
@@ -57,7 +60,7 @@ class CustomResourceProvisionerTest {
private CloudFormationTemplateEngine engine() {
return new CloudFormationTemplateEngine("000000000000", "us-east-1", "my-stack",
"stack/id", Map.of(), Map.of(), Map.of(), Map.of(), Map.of(), mapper,
STACK_ID, Map.of(), Map.of(), Map.of(), Map.of(), Map.of(), mapper,
(Function<String, String>) name -> null);
}
@@ -105,6 +108,8 @@ class CustomResourceProvisionerTest {
eq(InvocationType.RequestResponse));
JsonNode event = readEvent(payload);
assertEquals("Create", event.get("RequestType").asText());
// The real stack id, so a handler can DescribeStacks(event.StackId).
assertEquals(STACK_ID, event.get("StackId").asText());
assertTrue(event.get("ResponseURL").asText().startsWith("http://floci:4566/cfn-response/"));
assertEquals("hi", event.get("ResourceProperties").get("Message").asText());
// CloudFormation carries ServiceToken both at the top level and inside ResourceProperties.
@@ -137,6 +142,7 @@ class CustomResourceProvisionerTest {
eq(InvocationType.RequestResponse));
JsonNode event = readEvent(payload);
assertEquals("Update", event.get("RequestType").asText());
assertEquals(STACK_ID, event.get("StackId").asText());
assertEquals("phys-123", event.get("PhysicalResourceId").asText());
assertEquals("hi", event.get("ResourceProperties").get("Message").asText());
// CloudFormation includes the previous properties on Update so handlers can diff.
@@ -218,6 +224,7 @@ class CustomResourceProvisionerTest {
eq(InvocationType.RequestResponse));
JsonNode deleteEvent = readEvent(() -> payloads.getAllValues().get(1));
assertEquals("Delete", deleteEvent.get("RequestType").asText());
assertEquals(STACK_ID, deleteEvent.get("StackId").asText());
assertEquals("phys-123", deleteEvent.get("PhysicalResourceId").asText());
assertEquals("hi", deleteEvent.get("ResourceProperties").get("Message").asText());
}