fix(cloudformation): reconcile or replace EC2 instances on stack update (#3884)

AWS::EC2::Instance provisioning launched a new instance on every UpdateStack, leaking the prior one and breaking Ref. Reuse the existing instance when no createOnly property changed, republishing its attributes and reconciling its tags in place. Replace it only when ImageId, SubnetId or KeyName differs, recording the displaced instance through ReplacementCleanup so the stack removes it after the update commits.

The reuse lookup tolerates a prior instance terminated out of band: describeInstances raises InvalidInstanceID.NotFound for an unknown id, and a terminated instance lingers in the store, so both fall back to launching a fresh instance instead of failing the update or pointing Ref at a dead instance. Republishing an instance's attributes now clears the ones it does not set, so a replacement never inherits the prior instance's PublicIp or DNS names.

Deferred: mutable properties are not applied to a reused instance in place (SecurityGroupIds, UserData, IamInstanceProfile), an in-place InstanceType change is not applied, and createOnly changes to properties the provisioner does not read (PrivateIpAddress, AvailabilityZone) do not force replacement.

Refs #3810
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-18 23:36:46 -04:00
1 parent ec4943380b
commit 21beccceb1
2 files changed
+307 -7

No files matched your search

@@ -2,32 +2,39 @@ package io.github.hectorvent.floci.services.cloudformation.provisioners;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.ec2.model.Instance;
import io.github.hectorvent.floci.services.ec2.model.InstanceState;
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplateData;
import io.github.hectorvent.floci.services.ec2.model.Placement;
import io.github.hectorvent.floci.services.ec2.model.Reservation;
import io.github.hectorvent.floci.services.ec2.model.Tag;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import io.github.hectorvent.floci.core.common.AwsException;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doThrow;
/**
* {@code AWS::EC2::Instance} in isolation: the attributes Ref and Fn::GetAtt read once the launch
@@ -124,6 +131,152 @@ class Ec2InstanceCfnProvisionerTest {
assertEquals("DependencyViolation", failure.getErrorCode());
}
@Test
void updateWithoutACreateOnlyChangeReusesTheInstance() throws Exception {
Instance i1 = new Instance();
i1.setInstanceId("i-1");
i1.setImageId("ami-1");
stubLaunch(i1);
StackResource r = resource("Server");
provisioner.provision(r, props("{\"ImageId\": \"ami-1\"}"), ctx(null));
assertEquals("i-1", r.getPhysicalId());
when(ec2.describeInstances("us-east-1", List.of("i-1"), null))
.thenReturn(List.of(reservationOf(i1)));
provisioner.provision(r, props("{\"ImageId\": \"ami-1\", \"InstanceType\": \"t3.small\"}"),
ctx("i-1"));
assertEquals("i-1", r.getPhysicalId());
verify(ec2, times(1)).runInstances(anyString(), any(), anyString(), anyInt(), anyInt(), any(),
anyList(), any(), any(), anyList(), any(), any(), any());
}
@Test
void changingImageIdReplacesTheInstanceAndRecordsTheCleanup() throws Exception {
Instance i1 = new Instance();
i1.setInstanceId("i-1");
i1.setImageId("ami-1");
Instance i2 = new Instance();
i2.setInstanceId("i-2");
i2.setImageId("ami-2");
when(ec2.runInstances(anyString(), any(), anyString(), anyInt(), anyInt(), any(), anyList(),
any(), any(), anyList(), any(), any(), any()))
.thenReturn(reservationOf(i1), reservationOf(i2));
StackResource r = resource("Server");
provisioner.provision(r, props("{\"ImageId\": \"ami-1\"}"), ctx(null));
assertEquals("i-1", r.getPhysicalId());
when(ec2.describeInstances("us-east-1", List.of("i-1"), null))
.thenReturn(List.of(reservationOf(i1)));
provisioner.provision(r, props("{\"ImageId\": \"ami-2\"}"), ctx("i-1"));
assertEquals("i-2", r.getPhysicalId());
assertTrue(provisioner.hasReplacementUpdate(r));
assertEquals("i-1", provisioner.updateCleanupPhysicalId(r));
}
@Test
void reusedInstanceReconcilesTagsInPlace() throws Exception {
Instance i1 = new Instance();
i1.setInstanceId("i-1");
i1.setImageId("ami-1");
stubLaunch(i1);
StackResource r = resource("Server");
provisioner.provision(r, props("""
{"ImageId": "ami-1", "Tags": [{"Key": "env", "Value": "prod"}]}
"""), ctx(null));
when(ec2.describeInstances("us-east-1", List.of("i-1"), null))
.thenReturn(List.of(reservationOf(i1)));
when(ec2.describeTags(eq("us-east-1"), any())).thenReturn(List.of(
tagEntry("i-1", "env", "prod"), tagEntry("i-1", "old", "stale")));
provisioner.provision(r, props("""
{"ImageId": "ami-1", "Tags": [{"Key": "env", "Value": "prod"}]}
"""), ctx("i-1"));
@SuppressWarnings("unchecked")
ArgumentCaptor<List<Tag>> removed = ArgumentCaptor.forClass(List.class);
verify(ec2).deleteTags(eq("us-east-1"), eq(List.of("i-1")), removed.capture());
assertEquals(1, removed.getValue().size());
assertEquals("old", removed.getValue().get(0).getKey());
}
@Test
void updateCreatesAFreshInstanceWhenThePriorOneWasTerminatedOutOfBand() throws Exception {
Instance created = new Instance();
created.setInstanceId("i-9");
created.setImageId("ami-1");
stubLaunch(created);
when(ec2.describeInstances("us-east-1", List.of("i-1"), null))
.thenThrow(new AwsException("InvalidInstanceID.NotFound", "gone", 400));
StackResource r = resource("Server");
assertDoesNotThrow(() -> provisioner.provision(r, props("{\"ImageId\": \"ami-1\"}"), ctx("i-1")));
assertEquals("i-9", r.getPhysicalId());
verify(ec2).runInstances(anyString(), any(), anyString(), anyInt(), anyInt(), any(),
anyList(), any(), any(), anyList(), any(), any(), any());
}
@Test
void updateDoesNotReuseATerminatedPriorInstance() throws Exception {
Instance terminated = new Instance();
terminated.setInstanceId("i-1");
terminated.setImageId("ami-1");
terminated.setState(InstanceState.terminated());
Instance created = new Instance();
created.setInstanceId("i-9");
created.setImageId("ami-1");
stubLaunch(created);
when(ec2.describeInstances("us-east-1", List.of("i-1"), null))
.thenReturn(List.of(reservationOf(terminated)));
StackResource r = resource("Server");
provisioner.provision(r, props("{\"ImageId\": \"ami-1\"}"), ctx("i-1"));
assertEquals("i-9", r.getPhysicalId());
verify(ec2).runInstances(anyString(), any(), anyString(), anyInt(), anyInt(), any(),
anyList(), any(), any(), anyList(), any(), any(), any());
}
@Test
void replacementDoesNotInheritStaleAttributesFromThePriorInstance() throws Exception {
Instance withPublicIp = new Instance();
withPublicIp.setInstanceId("i-1");
withPublicIp.setImageId("ami-1");
withPublicIp.setPublicIpAddress("54.0.0.5");
Instance withoutPublicIp = new Instance();
withoutPublicIp.setInstanceId("i-2");
withoutPublicIp.setImageId("ami-2");
when(ec2.runInstances(anyString(), any(), anyString(), anyInt(), anyInt(), any(), anyList(),
any(), any(), anyList(), any(), any(), any()))
.thenReturn(reservationOf(withPublicIp), reservationOf(withoutPublicIp));
StackResource r = resource("Server");
provisioner.provision(r, props("{\"ImageId\": \"ami-1\"}"), ctx(null));
assertEquals("54.0.0.5", r.getAttributes().get("PublicIp"));
when(ec2.describeInstances("us-east-1", List.of("i-1"), null))
.thenReturn(List.of(reservationOf(withPublicIp)));
provisioner.provision(r, props("{\"ImageId\": \"ami-2\"}"), ctx("i-1"));
assertEquals("i-2", r.getPhysicalId());
assertFalse(r.getAttributes().containsKey("PublicIp"));
}
private static Reservation reservationOf(Instance instance) {
Reservation reservation = new Reservation();
reservation.getInstances().add(instance);
return reservation;
}
private static Map<String, String> tagEntry(String resourceId, String key, String value) {
Map<String, String> entry = new HashMap<>();
entry.put("resourceId", resourceId);
entry.put("key", key);
entry.put("value", value);
return entry;
}
private void stubLaunch(Instance instance) {
Reservation reservation = new Reservation();
reservation.getInstances().add(instance);
@@ -132,13 +285,17 @@ class Ec2InstanceCfnProvisionerTest {
}
private ProvisionContext ctx() {
return ctx(null);
}
private ProvisionContext ctx(String priorPhysicalId) {
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
when(engine.resolve(any())).thenAnswer(inv -> {
JsonNode node = inv.getArgument(0);
return node == null ? null : node.asText();
});
when(engine.resolveNode(any())).thenAnswer(inv -> inv.getArgument(0));
return new ProvisionContext(engine, "us-east-1", "000000000000", "my-stack");
return new ProvisionContext(engine, "us-east-1", "000000000000", "my-stack", priorPhysicalId);
}
private JsonNode props(String json) throws Exception {