From 71c7836da1bb01f89ce78914e8f9b0c65cc505f8 Mon Sep 17 00:00:00 2001 From: Sam Fakhreddine Date: Thu, 3 Sep 2026 23:53:59 -0600 Subject: [PATCH] fix(cloudformation): skip re-invoking custom resource handlers when properties are unchanged (#2785) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../CloudFormationResourceProvisioner.java | 10 +++++ .../CustomResourceProvisionerTest.java | 41 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java index 3be679396f..dcae1904da 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationResourceProvisioner.java @@ -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); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CustomResourceProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CustomResourceProvisionerTest.java index 4a9230e445..6943663e7e 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CustomResourceProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CustomResourceProvisionerTest.java @@ -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"));