From e1334cb51282d0b6961e88212cdbae56fc25906b Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Mon, 21 Sep 2026 08:58:28 -0500 Subject: [PATCH] fix(cloudformation): fail an EC2 instance whose LaunchTemplate cannot be resolved (#4058) Ec2InstanceCfnProvisioner swallowed a failed resolveLaunchTemplateData at debug and then launched the instance with default config (t3.micro, no template AMI/KeyName). A LaunchTemplate a stack references but that does not resolve is a real error, as on AWS, so let the AwsException propagate and fail the resource. The unused LOG field is removed with the swallow. The lookup only runs when the resolved LaunchTemplate is an object naming a template: an Fn::If that selects AWS::NoValue resolves to an empty node (CloudFormationTemplateEngine maps AWS::NoValue to ""), which on AWS means the property is absent, so it must launch without a template rather than raise InvalidLaunchTemplateId.NotFound. Refs #3810 --- .../Ec2InstanceCfnProvisioner.java | 41 +++++++++---------- .../Ec2InstanceCfnProvisionerTest.java | 31 ++++++++++++++ 2 files changed, 50 insertions(+), 22 deletions(-) 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 448a89e6b6..e00b7c4af8 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 @@ -12,7 +12,6 @@ import io.github.hectorvent.floci.services.ec2.model.Reservation; import io.github.hectorvent.floci.services.ec2.model.Tag; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; -import org.jboss.logging.Logger; import java.util.ArrayList; import java.util.HashMap; @@ -28,8 +27,6 @@ import java.util.Set; @ApplicationScoped public class Ec2InstanceCfnProvisioner implements CfnResourceProvisioner { - private static final Logger LOG = Logger.getLogger(Ec2InstanceCfnProvisioner.class); - private static final String INSTANCE = "AWS::EC2::Instance"; private final Ec2Service ec2Service; @@ -55,25 +52,25 @@ public class Ec2InstanceCfnProvisioner implements CfnResourceProvisioner { // An instance may reference a LaunchTemplate for its config; fields the // properties don't set resolve from the template's data, as on AWS. - if (props != null && props.has("LaunchTemplate")) { - JsonNode ltRef = engine.resolveNode(props.get("LaunchTemplate")); - try { - LaunchTemplateData ltData = ec2Service.resolveLaunchTemplateData(region, - ltRef.path("LaunchTemplateId").asText(null), - ltRef.path("LaunchTemplateName").asText(null), - ltRef.path("Version").asText(null)); - if (imageId == null || imageId.isBlank()) { - imageId = ltData.getImageId(); - } - if (instanceType == null || instanceType.isBlank()) { - instanceType = ltData.getInstanceType(); - } - if (keyName == null || keyName.isBlank()) { - keyName = ltData.getKeyName(); - } - } catch (Exception e) { - LOG.debugv("Could not resolve launch template for instance {0}: {1}", - r.getLogicalId(), e.getMessage()); + JsonNode ltRef = props != null ? engine.resolveNode(props.get("LaunchTemplate")) : null; + String ltId = ltRef != null ? ltRef.path("LaunchTemplateId").asText(null) : null; + String ltName = ltRef != null ? ltRef.path("LaunchTemplateName").asText(null) : null; + // Only look one up when the property actually names a template. An Fn::If that selects + // AWS::NoValue resolves to an empty node here, which on AWS means the property is absent + // (launch without a template), so it must not turn into an InvalidLaunchTemplateId.NotFound. + if (ltRef != null && ltRef.isObject() && (ltId != null || ltName != null)) { + // A LaunchTemplate the stack references but that does not resolve is a real error, as on + // AWS: let it fail the resource rather than silently launching with default config. + LaunchTemplateData ltData = ec2Service.resolveLaunchTemplateData(region, ltId, ltName, + ltRef.path("Version").asText(null)); + if (imageId == null || imageId.isBlank()) { + imageId = ltData.getImageId(); + } + if (instanceType == null || instanceType.isBlank()) { + instanceType = ltData.getInstanceType(); + } + if (keyName == null || keyName.isBlank()) { + keyName = ltData.getKeyName(); } } if (instanceType == null || instanceType.isBlank()) { 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 66a04cca71..652ebaf0c9 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 @@ -91,6 +91,37 @@ class Ec2InstanceCfnProvisionerTest { eq("template-key"), anyList(), any(), any(), anyList(), any(), any(), any()); } + @Test + void aLaunchTemplateThatDoesNotResolveFailsTheInstance() throws Exception { + when(ec2.resolveLaunchTemplateData("us-east-1", "lt-missing", null, null)) + .thenThrow(new AwsException("InvalidLaunchTemplateId.NotFound", + "The specified launch template does not exist.", 400)); + + AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(resource("Server"), props(""" + {"LaunchTemplate": {"LaunchTemplateId": "lt-missing"}} + """), ctx())); + + assertEquals("InvalidLaunchTemplateId.NotFound", e.getErrorCode()); + } + + @Test + void aLaunchTemplateResolvingToNoValueLaunchesWithoutIt() throws Exception { + Instance instance = new Instance(); + instance.setInstanceId("i-nolt"); + stubLaunch(instance); + + // An Fn::If selecting AWS::NoValue resolves to an empty node, which on AWS means the property + // is absent: the instance launches without a template rather than failing to resolve one. + StackResource r = resource("Server"); + provisioner.provision(r, props(""" + {"LaunchTemplate": ""} + """), ctx()); + + assertEquals("i-nolt", r.getPhysicalId()); + verify(ec2).runInstances(eq("us-east-1"), any(), eq("t3.micro"), eq(1), eq(1), any(), + anyList(), any(), any(), anyList(), any(), any(), any()); + } + @Test void provisionsAnInstanceDeclaredWithNoProperties() { Instance instance = new Instance();