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"));