fix(cloudformation): skip re-invoking custom resource handlers when properties are unchanged (#2785)

Per AWS docs (UserGuide/template-custom-resources-sns.md): "During a stack
update, if no changes are made to a custom resource, CloudFormation will not
send any requests to it." floci's provisionCustomResource() already stashed
each custom resource's prior resolved properties (CR_PROPERTIES_ATTR) and read
them back on Update, but never actually compared old vs new before invoking
the handler, so every stack update replayed every custom resource regardless
of whether its own properties changed — repeating non-idempotent side effects
on an unrelated update.
This commit is contained in:
Sam Fakhreddine authored and GitHub committed 2026-09-04 00:53:59 -05:00
1 parent aa455fbba0
commit 71c7836da1
2 files changed
+51

No files matched your search

@@ -6033,6 +6033,16 @@ public class CloudFormationResourceProvisioner {
// The prior values were stashed at the last create/update; read them before we overwrite below.
ObjectNode oldResourceProperties = isUpdate ? readStashedProperties(r) : null;
// CloudFormation invokes a custom resource's Update handler only when its resolved
// properties changed (UserGuide/template-custom-resources-sns.md: "During a stack update,
// if no changes are made to a custom resource, CloudFormation will not send any requests
// to it."). Replaying every custom resource during an unrelated stack update can repeat
// non-idempotent side effects. The prior resolved properties are already stashed on the
// resource, so an exact match is a safe no-op that preserves physical ID and attributes.
if (oldResourceProperties != null && oldResourceProperties.equals(resourceProperties)) {
return;
}
JsonNode response = invokeCustomResourceHandler(serviceToken, requestType, r.getLogicalId(),
r.getResourceType(), priorPhysicalId, resourceProperties, oldResourceProperties,
region, accountId, stackName);
@@ -163,6 +163,47 @@ class CustomResourceProvisionerTest {
assertTrue(r.getStatusReason().contains("boom"));
}
@Test
void updateWithUnchangedResolvedPropertiesSkipsHandlerInvocation() {
stubHandler("phys-123", Map.of("Greeting", "hello"));
// Initial Create — stashes the resolved ResourceProperties on the resource.
StackResource created = provisioner.provision("MyCr", "Custom::Test", props(),
engine(), "us-east-1", "000000000000", "my-stack");
assertEquals("phys-123", created.getPhysicalId());
// "Update" with byte-identical resolved properties: real CloudFormation would not send
// any request to the custom resource at all (UserGuide/template-custom-resources-sns.md:
// "During a stack update, if no changes are made to a custom resource, CloudFormation
// will not send any requests to it.").
StackResource updated = provisioner.provision("MyCr", "Custom::Test", props(),
engine(), "us-east-1", "000000000000", "my-stack",
created.getPhysicalId(), created.getAttributes());
verify(lambdaService, times(1)).invoke(any(), eq(SERVICE_TOKEN), any(),
eq(InvocationType.RequestResponse));
assertEquals("CREATE_COMPLETE", updated.getStatus());
assertEquals("phys-123", updated.getPhysicalId());
assertEquals("hello", updated.getAttributes().get("Greeting"));
}
@Test
void updateWithChangedResolvedPropertiesStillInvokesHandler() {
stubHandler("phys-123", Map.of("Greeting", "hello"));
StackResource created = provisioner.provision("MyCr", "Custom::Test", props(),
engine(), "us-east-1", "000000000000", "my-stack");
ObjectNode changedProps = props();
changedProps.put("Message", "bye");
provisioner.provision("MyCr", "Custom::Test", changedProps,
engine(), "us-east-1", "000000000000", "my-stack",
created.getPhysicalId(), created.getAttributes());
verify(lambdaService, times(2)).invoke(any(), eq(SERVICE_TOKEN), any(),
eq(InvocationType.RequestResponse));
}
@Test
void deleteReinvokesHandlerWithDeleteRequestType() {
stubHandler("phys-123", Map.of("Greeting", "hello"));