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

The registry schema lists State as a read-only property of AWS::EC2::Instance,
but it is a nested object with Code and Name, and Fn::GetAtt exposes only the
dotted members: cfn-lint's GetAtt derivation for this type yields State.Code
and State.Name and no bare State, the same shape as an RDS Endpoint. Floci
never published either, so a template reading them got the literal
"Server.State.Name" back.

Ec2InstanceCfnProvisioner now publishes State.Code (the code as a string, the
schema's type) and State.Name, removing both when the instance carries no
state, and the create path re-reads the instance once the launch settles, as
CloudFormation's own stabilization does. Without the re-read a mock-mode
instance would publish "pending", since it only reaches "running" on the next
DescribeInstances. The State gap row now records the dotted form, like the
RDS Endpoint rows.

Tests: Ec2InstanceCfnProvisionerTest pins both keys, the settled state and the
no-state case; CloudFormationEc2InstanceIntegrationTest asserts both GetAtt
forms against the state DescribeInstances reports.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-23 00:03:29 -04:00
1 parent fcc89c0e8d
commit 024c2cbe27
4 files changed
+75 -6

No files matched your search

@@ -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. */