Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cloudformation): publish EC2 Instance State via Fn::GetAtt (#4203)

Hector Ventura024c2cbe2702
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java
index cd39654894..92111a52c2 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisioner.java
@@ -147,7 +147,10 @@ public class Ec2InstanceCfnProvisioner implements CfnResourceProvisioner {
r.getAttributes().put(CfnRollback.ROLLBACK_OWNED_ATTR, "true");
ec2Service.awaitContainerLaunch(instance);
r.getAttributes().remove(CfnRollback.ROLLBACK_OWNED_ATTR);
- publishInstanceAttributes(r, instance);
+ // Re-read once the launch settles, as CloudFormation's own stabilization does, so the
+ // published State is the settled instance's rather than the pending one RunInstances returned.
+ Instance launched = findInstance(region, instance.getInstanceId());
+ publishInstanceAttributes(r, launched != null ? launched : instance);
storeDeclaredCreateOnly(r, privateIpAddress, availabilityZone);
// A createOnly change that landed a new instance id replaced the prior one: record it so
// the stack cleans the displaced instance up after the update commits.
@@ -306,6 +309,16 @@ public class Ec2InstanceCfnProvisioner implements CfnResourceProvisioner {
} else {
r.getAttributes().remove("VpcId");
}
+ // The schema's State is a nested object, so Fn::GetAtt exposes its members in dotted form
+ // (State.Code, State.Name), the same shape as an RDS Endpoint; there is no bare State.
+ InstanceState state = instance.getState();
+ if (state != null && state.getName() != null) {
+ r.getAttributes().put("State.Code", String.valueOf(state.getCode()));
+ r.getAttributes().put("State.Name", state.getName());
+ } else {
+ r.getAttributes().remove("State.Code");
+ r.getAttributes().remove("State.Name");
+ }
}
/** Reconciles the instance tags to the template when the instance is kept in place. */
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationEc2InstanceIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationEc2InstanceIntegrationTest.java
index 2bc8471548..7fd1ff276b 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationEc2InstanceIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationEc2InstanceIntegrationTest.java
@@ -8,6 +8,7 @@ import java.util.regex.Pattern;
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;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -39,7 +40,9 @@ class CloudFormationEc2InstanceIntegrationTest {
}
},
"Outputs": {
- "InstanceId": {"Value": {"Ref": "Server"}}
+ "InstanceId": {"Value": {"Ref": "Server"}},
+ "StateName": {"Value": {"Fn::GetAtt": ["Server", "State.Name"]}},
+ "StateCode": {"Value": {"Fn::GetAtt": "Server.State.Code"}}
}
}
""";
@@ -73,7 +76,7 @@ class CloudFormationEc2InstanceIntegrationTest {
String instanceId = m.group(1);
// The instance really exists in EC2.
- given()
+ String describeInstances = given()
.contentType("application/x-www-form-urlencoded")
.header("Authorization", EC2_AUTH)
.formParam("Action", "DescribeInstances")
@@ -82,7 +85,24 @@ class CloudFormationEc2InstanceIntegrationTest {
.post("/")
.then()
.statusCode(200)
- .body(containsString(instanceId));
+ .body(containsString(instanceId))
+ .extract().asString();
+
+ // The schema's State is a nested object, so Fn::GetAtt exposes State.Code and State.Name:
+ // the state the launch settled to, as DescribeInstances reports it, in both GetAtt forms.
+ Matcher state = Pattern.compile("<instanceState>\\s*<code>(\\d+)</code>\\s*<name>([a-z-]+)</name>")
+ .matcher(describeInstances);
+ assertTrue(state.find(), "expected an instance state in DescribeInstances");
+ assertEquals("running", state.group(2));
+ assertEquals(state.group(2), output(describeStacks, "StateName"));
+ assertEquals(state.group(1), output(describeStacks, "StateCode"));
+ }
+
+ private static String output(String describeStacks, String key) {
+ Matcher m = Pattern.compile("<OutputKey>" + key + "</OutputKey>\\s*<OutputValue>([^<]*)</OutputValue>")
+ .matcher(describeStacks);
+ assertTrue(m.find(), "expected output " + key + " in " + describeStacks);
+ return m.group(1);
}
@Test
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisionerTest.java
index d99cd249a6..b08ebca737 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisionerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/Ec2InstanceCfnProvisionerTest.java
@@ -63,6 +63,7 @@ class Ec2InstanceCfnProvisionerTest {
instance.setPublicDnsName("ec2-54-0-0-5.compute-1.amazonaws.com");
instance.setPlacement(new Placement("us-east-1a"));
instance.setVpcId("vpc-1");
+ instance.setState(InstanceState.running());
stubLaunch(instance);
StackResource r = resource("Server");
@@ -70,13 +71,48 @@ class Ec2InstanceCfnProvisionerTest {
assertEquals("i-1", r.getPhysicalId());
assertEquals(Set.of("InstanceId", "PrivateIp", "PublicIp", "PrivateDnsName", "PublicDnsName",
- "AvailabilityZone", "VpcId"), r.getAttributes().keySet());
+ "AvailabilityZone", "VpcId", "State.Code", "State.Name"), r.getAttributes().keySet());
assertEquals("10.0.0.5", r.getAttributes().get("PrivateIp"));
assertEquals("us-east-1a", r.getAttributes().get("AvailabilityZone"));
assertEquals("vpc-1", r.getAttributes().get("VpcId"));
+ assertEquals("16", r.getAttributes().get("State.Code"));
+ assertEquals("running", r.getAttributes().get("State.Name"));
verify(ec2).awaitContainerLaunch(instance);
}
+ @Test
+ void launchPublishesTheStateTheLaunchSettledTo() throws Exception {
+ Instance pending = new Instance();
+ pending.setInstanceId("i-1");
+ pending.setState(InstanceState.pending());
+ stubLaunch(pending);
+ Instance running = new Instance();
+ running.setInstanceId("i-1");
+ running.setState(InstanceState.running());
+ when(ec2.describeInstances("us-east-1", List.of("i-1"), null))
+ .thenReturn(List.of(reservationOf(running)));
+ StackResource r = resource("Server");
+
+ provisioner.provision(r, props("{\"ImageId\": \"ami-1\"}"), ctx());
+
+ assertEquals("16", r.getAttributes().get("State.Code"));
+ assertEquals("running", r.getAttributes().get("State.Name"));
+ }
+
+ @Test
+ void anInstanceWithoutAStateExposesNoStateAttributes() throws Exception {
+ Instance instance = new Instance();
+ instance.setInstanceId("i-stateless");
+ stubLaunch(instance);
+ StackResource r = resource("Server");
+ r.getAttributes().put("State.Code", "80");
+ r.getAttributes().put("State.Name", "stopped");
+
+ provisioner.provision(r, props("{\"ImageId\": \"ami-1\"}"), ctx());
+
+ assertEquals(Set.of("InstanceId"), r.getAttributes().keySet());
+ }
+
@Test
void launchTemplateFillsWhatThePropertiesLeaveUnset() throws Exception {
LaunchTemplateData data = new LaunchTemplateData();
diff --git a/src/test/resources/cloudformation/getatt-attribute-gaps.tsv b/src/test/resources/cloudformation/getatt-attribute-gaps.tsv
index 89305d3988..3f8aa72dc3 100644
--- a/src/test/resources/cloudformation/getatt-attribute-gaps.tsv
+++ b/src/test/resources/cloudformation/getatt-attribute-gaps.tsv
@@ -2,7 +2,7 @@ AWS::AppSync::GraphQLApi GraphQLEndpointArn the private-endpoint ARN of the Grap
AWS::CodeBuild::Project Id schema gives no description; AWS's Id may differ from the project name Floci uses
AWS::CodePipeline::CustomActionType Id schema gives no description; the custom action has no separate id in Floci
AWS::CodePipeline::Webhook Id schema gives no description; Floci keys webhooks by name
-AWS::EC2::Instance State not published yet: Floci tracks the instance state, setting it is a separate change to what Fn::GetAtt returns
+AWS::EC2::Instance State the dotted State.Code and State.Name are set instead, the form AWS's GetAtt exposes for the nested State object
AWS::EC2::NatGateway AutoProvisionZones availability-mode NAT gateways are not emulated
AWS::EC2::NatGateway AutoScalingIps availability-mode NAT gateways are not emulated
AWS::EC2::NatGateway EniId the NAT gateway's network interface is not created