Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(cloudformation): resolve ASG launch templates by id and MixedInstancesPolicy (#2005) (#2019)

thejrose19840971cf6f649b
diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.md
index e880bb9f88..e06368b920 100644
--- a/docs/services/cloudformation.md
+++ b/docs/services/cloudformation.md
@@ -122,6 +122,21 @@ because their backing services are not implemented. A `SecretId` change is appli
than reproducing CloudFormation's replacement event sequence; failed changes restore affected secret
data and attachment ownership.
+## Auto Scaling Launch Template Resolution
+
+`AWS::AutoScaling::AutoScalingGroup` resolves its launch template through any of the shapes AWS
+accepts, not only by name:
+
+- `LaunchTemplate` with `LaunchTemplateId` **or** `LaunchTemplateName` (plus an optional `Version`).
+ The id and name are distinct lookup keys, so an `lt-` id is matched as an id rather than being
+ treated as a name.
+- A `Ref` to an in-stack `AWS::EC2::LaunchTemplate`, whose `Ref` returns the `lt-` id and whose
+ `Fn::GetAtt LatestVersionNumber` supplies the version.
+- `MixedInstancesPolicy` → `LaunchTemplate` → `LaunchTemplateSpecification`, including
+ `Overrides[].InstanceType` and `InstancesDistribution` (`OnDemandBaseCapacity`,
+ `OnDemandPercentageAboveBaseCapacity`, `SpotAllocationStrategy`). A non-integer where AWS expects
+ a number fails the stack rather than being dropped.
+
## Lambda Stack Updates
`AWS::Lambda::Function` resources are reconciled during `UpdateStack` in the same shape as CloudFormation/CDK deployments:
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 0598bfea51..bb0337ecbe 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
@@ -34,6 +34,7 @@ import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService
import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService;
import io.github.hectorvent.floci.services.cloudwatch.metrics.model.Dimension;
import io.github.hectorvent.floci.services.autoscaling.AutoScalingService;
+import io.github.hectorvent.floci.services.autoscaling.model.MixedInstancesPolicy;
import io.github.hectorvent.floci.services.cloudwatch.metrics.model.MetricAlarm;
import io.github.hectorvent.floci.services.ec2.Ec2Service;
import io.github.hectorvent.floci.services.kinesis.KinesisService;
@@ -1086,20 +1087,22 @@ public class CloudFormationResourceProvisioner {
name = generatePhysicalName(stackName, r.getLogicalId(), 255, false);
}
String launchConfigName = resolveOptional(props, "LaunchConfigurationName", engine);
+ String launchTemplateId = null;
String launchTemplateName = null;
String launchTemplateVersion = null;
if (props != null && props.has("LaunchTemplate")) {
JsonNode lt = props.get("LaunchTemplate");
+ // Id and name are distinct lookup keys in Auto Scaling: passing an lt- id in the name slot
+ // never matches a stored template.
+ launchTemplateId = engine.resolve(lt.path("LaunchTemplateId"));
launchTemplateName = engine.resolve(lt.path("LaunchTemplateName"));
- if (launchTemplateName == null || launchTemplateName.isBlank()) {
- launchTemplateName = engine.resolve(lt.path("LaunchTemplateId"));
- }
launchTemplateVersion = engine.resolve(lt.path("Version"));
}
var asg = autoScalingService.createAutoScalingGroup(region, name,
- blankToNull(launchConfigName), null, blankToNull(launchTemplateName), blankToNull(launchTemplateVersion),
- null,
+ blankToNull(launchConfigName),
+ blankToNull(launchTemplateId), blankToNull(launchTemplateName), blankToNull(launchTemplateVersion),
+ resolveMixedInstancesPolicy(props, engine),
parseIntProp(props, "MinSize", engine, 0),
parseIntProp(props, "MaxSize", engine, 0),
parseIntProp(props, "DesiredCapacity", engine, 0),
@@ -1118,6 +1121,72 @@ public class CloudFormationResourceProvisioner {
r.getAttributes().put("Arn", asg.getAutoScalingGroupArn());
}
+ /**
+ * Builds the {@code MixedInstancesPolicy} of an Auto Scaling group from template properties, in the
+ * same shape the Query API parser produces. Returns {@code null} when the property is absent, so
+ * that the group falls back to its {@code LaunchTemplate} or {@code LaunchConfigurationName}.
+ */
+ private MixedInstancesPolicy resolveMixedInstancesPolicy(JsonNode props,
+ CloudFormationTemplateEngine engine) {
+ if (props == null || !props.has("MixedInstancesPolicy") || props.get("MixedInstancesPolicy").isNull()) {
+ return null;
+ }
+ JsonNode policyNode = props.get("MixedInstancesPolicy");
+ MixedInstancesPolicy policy = new MixedInstancesPolicy();
+
+ JsonNode launchTemplateNode = policyNode.path("LaunchTemplate");
+ if (launchTemplateNode.isObject()) {
+ MixedInstancesPolicy.LaunchTemplate launchTemplate = new MixedInstancesPolicy.LaunchTemplate();
+ JsonNode specNode = launchTemplateNode.path("LaunchTemplateSpecification");
+ if (specNode.isObject()) {
+ var specification = new MixedInstancesPolicy.LaunchTemplateSpecification();
+ specification.setLaunchTemplateId(blankToNull(engine.resolve(specNode.path("LaunchTemplateId"))));
+ specification.setLaunchTemplateName(blankToNull(engine.resolve(specNode.path("LaunchTemplateName"))));
+ specification.setVersion(blankToNull(engine.resolve(specNode.path("Version"))));
+ launchTemplate.setLaunchTemplateSpecification(specification);
+ }
+ for (JsonNode overrideNode : launchTemplateNode.path("Overrides")) {
+ String instanceType = engine.resolve(overrideNode.path("InstanceType"));
+ if (instanceType != null && !instanceType.isBlank()) {
+ var override = new MixedInstancesPolicy.LaunchTemplateOverride();
+ override.setInstanceType(instanceType);
+ launchTemplate.getOverrides().add(override);
+ }
+ }
+ policy.setLaunchTemplate(launchTemplate);
+ }
+
+ JsonNode distributionNode = policyNode.path("InstancesDistribution");
+ if (distributionNode.isObject()) {
+ var distribution = new MixedInstancesPolicy.InstancesDistribution();
+ distribution.setOnDemandBaseCapacity(parseOptionalInt("OnDemandBaseCapacity",
+ engine.resolve(distributionNode.path("OnDemandBaseCapacity"))));
+ distribution.setOnDemandPercentageAboveBaseCapacity(
+ parseOptionalInt("OnDemandPercentageAboveBaseCapacity",
+ engine.resolve(distributionNode.path("OnDemandPercentageAboveBaseCapacity"))));
+ distribution.setSpotAllocationStrategy(
+ blankToNull(engine.resolve(distributionNode.path("SpotAllocationStrategy"))));
+ policy.setInstancesDistribution(distribution);
+ }
+ return policy;
+ }
+
+ /**
+ * Reads an optional integer property. A value that is present but not a number is a template
+ * error, and AWS rejects it rather than treating it as absent.
+ */
+ private Integer parseOptionalInt(String field, String value) {
+ if (value == null || value.isBlank()) {
+ return null;
+ }
+ try {
+ return Integer.valueOf(value.trim());
+ } catch (NumberFormatException e) {
+ throw new AwsException("ValidationError",
+ "Value of property " + field + " must be an integer.", 400);
+ }
+ }
+
private Map<String, String> resolveAsgTags(JsonNode props, CloudFormationTemplateEngine engine) {
Map<String, String> tags = new LinkedHashMap<>();
if (props != null && props.has("Tags") && props.get("Tags").isArray()) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationAsgLaunchTemplateIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationAsgLaunchTemplateIntegrationTest.java
new file mode 100644
index 0000000000..685dc0eb01
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/CloudFormationAsgLaunchTemplateIntegrationTest.java
@@ -0,0 +1,312 @@
+package io.github.hectorvent.floci.services.cloudformation;
+
+import io.quarkus.test.junit.QuarkusTest;
+import org.junit.jupiter.api.Test;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.not;
+
+/**
+ * Covers how AWS::AutoScaling::AutoScalingGroup resolves a launch template when it is provisioned by
+ * CloudFormation: by name, by id, through a Ref to an in-stack AWS::EC2::LaunchTemplate, and through
+ * a MixedInstancesPolicy.
+ *
+ * <p>Regression coverage for <a href="https://github.com/floci-io/floci/issues/2005">#2005</a>,
+ * where every shape but the by-name one failed with "The specified launch template does not exist".
+ * Provisioning of the AWS::EC2::LaunchTemplate resource itself is covered by
+ * {@code CloudFormationLaunchTemplateIntegrationTest}; this suite exercises the Auto Scaling side.
+ */
+@QuarkusTest
+class CloudFormationAsgLaunchTemplateIntegrationTest {
+
+ private static final String CFN_AUTH =
+ "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/cloudformation/aws4_request";
+ private static final String EC2_AUTH =
+ "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
+ // Capacity is kept at zero throughout: launch template resolution is validated when the group is
+ // created, and launching instances would add ENIs that other tests in this suite count.
+ private static final String ASG_AUTH =
+ "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/autoscaling/aws4_request";
+
+ /** Creates a launch template through the EC2 API and returns its {@code lt-} id. */
+ private String createLaunchTemplate(String name) {
+ String xml = given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", EC2_AUTH)
+ .formParam("Action", "CreateLaunchTemplate")
+ .formParam("LaunchTemplateName", name)
+ .formParam("LaunchTemplateData.ImageId", "ami-12345678")
+ .formParam("LaunchTemplateData.InstanceType", "t3.micro")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract().body().asString();
+ int start = xml.indexOf("<launchTemplateId>") + "<launchTemplateId>".length();
+ return xml.substring(start, xml.indexOf("</launchTemplateId>"));
+ }
+
+ private void createStack(String stackName, String template) {
+ given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", CFN_AUTH)
+ .formParam("Action", "CreateStack")
+ .formParam("StackName", stackName)
+ .formParam("TemplateBody", template)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
+
+ private String describeStacks(String stackName) {
+ return given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", CFN_AUTH)
+ .formParam("Action", "DescribeStacks")
+ .formParam("StackName", stackName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract().body().asString();
+ }
+
+ private String describeAutoScalingGroup(String asgName) {
+ return given()
+ .contentType("application/x-www-form-urlencoded")
+ .header("Authorization", ASG_AUTH)
+ .formParam("Action", "DescribeAutoScalingGroups")
+ .formParam("AutoScalingGroupNames.member.1", asgName)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .extract().body().asString();
+ }
+
+ private void assertStackCreated(String stackName) {
+ String body = describeStacks(stackName);
+ assertThat(body, containsString("<StackStatus>CREATE_COMPLETE</StackStatus>"));
+ assertThat(body, not(containsString("The specified launch template does not exist.")));
+ }
+
+ @Test
+ void asgResolvesLaunchTemplateByName() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String ltName = "cfn-lt-name-" + suffix;
+ String stackName = "cfn-lt-name-stack-" + suffix;
+ createLaunchTemplate(ltName);
+
+ createStack(stackName, """
+ {
+ "Resources": {
+ "Asg": {
+ "Type": "AWS::AutoScaling::AutoScalingGroup",
+ "Properties": {
+ "AutoScalingGroupName": "cfn-asg-name-%s",
+ "LaunchTemplate": {"LaunchTemplateName": "%s", "Version": "1"},
+ "MinSize": 0,
+ "MaxSize": 0,
+ "DesiredCapacity": 0,
+ "AvailabilityZones": ["us-east-1a"]
+ }
+ }
+ }
+ }
+ """.formatted(suffix, ltName));
+
+ assertStackCreated(stackName);
+ assertThat(describeAutoScalingGroup("cfn-asg-name-" + suffix),
+ containsString("<LaunchTemplateName>" + ltName + "</LaunchTemplateName>"));
+ }
+
+ @Test
+ void asgResolvesLaunchTemplateById() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "cfn-lt-id-stack-" + suffix;
+ String ltId = createLaunchTemplate("cfn-lt-id-" + suffix);
+
+ createStack(stackName, """
+ {
+ "Resources": {
+ "Asg": {
+ "Type": "AWS::AutoScaling::AutoScalingGroup",
+ "Properties": {
+ "AutoScalingGroupName": "cfn-asg-id-%s",
+ "LaunchTemplate": {"LaunchTemplateId": "%s", "Version": "1"},
+ "MinSize": 0,
+ "MaxSize": 0,
+ "DesiredCapacity": 0,
+ "AvailabilityZones": ["us-east-1a"]
+ }
+ }
+ }
+ }
+ """.formatted(suffix, ltId));
+
+ assertStackCreated(stackName);
+ assertThat(describeAutoScalingGroup("cfn-asg-id-" + suffix),
+ containsString("<LaunchTemplateId>" + ltId + "</LaunchTemplateId>"));
+ }
+
+ @Test
+ void asgResolvesInStackLaunchTemplateByRef() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String stackName = "cfn-lt-instack-stack-" + suffix;
+
+ createStack(stackName, """
+ {
+ "Resources": {
+ "Lt": {
+ "Type": "AWS::EC2::LaunchTemplate",
+ "Properties": {
+ "LaunchTemplateName": "cfn-lt-instack-%s",
+ "LaunchTemplateData": {"ImageId": "ami-12345678", "InstanceType": "t3.micro"}
+ }
+ },
+ "Asg": {
+ "Type": "AWS::AutoScaling::AutoScalingGroup",
+ "Properties": {
+ "AutoScalingGroupName": "cfn-asg-instack-%s",
+ "LaunchTemplate": {
+ "LaunchTemplateId": {"Ref": "Lt"},
+ "Version": {"Fn::GetAtt": ["Lt", "LatestVersionNumber"]}
+ },
+ "MinSize": 0,
+ "MaxSize": 0,
+ "DesiredCapacity": 0,
+ "AvailabilityZones": ["us-east-1a"]
+ }
+ }
+ }
+ }
+ """.formatted(suffix, suffix));
+
+ assertStackCreated(stackName);
+ // Ref must have yielded a real lt- id, not the synthetic physical id of a stubbed resource.
+ assertThat(describeAutoScalingGroup("cfn-asg-instack-" + suffix),
+ containsString("<LaunchTemplateId>lt-"));
+ }
+
+ @Test
+ void asgResolvesMixedInstancesPolicyLaunchTemplate() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String ltName = "cfn-lt-mip-" + suffix;
+ String stackName = "cfn-lt-mip-stack-" + suffix;
+ createLaunchTemplate(ltName);
+
+ createStack(stackName, """
+ {
+ "Resources": {
+ "Asg": {
+ "Type": "AWS::AutoScaling::AutoScalingGroup",
+ "Properties": {
+ "AutoScalingGroupName": "cfn-asg-mip-%s",
+ "MixedInstancesPolicy": {
+ "LaunchTemplate": {
+ "LaunchTemplateSpecification": {"LaunchTemplateName": "%s", "Version": "1"},
+ "Overrides": [{"InstanceType": "t3.micro"}]
+ }
+ },
+ "MinSize": 0,
+ "MaxSize": 0,
+ "DesiredCapacity": 0,
+ "AvailabilityZones": ["us-east-1a"]
+ }
+ }
+ }
+ }
+ """.formatted(suffix, ltName));
+
+ assertStackCreated(stackName);
+ String group = describeAutoScalingGroup("cfn-asg-mip-" + suffix);
+ assertThat(group, containsString("<MixedInstancesPolicy>"));
+ assertThat(group, containsString("<LaunchTemplateName>" + ltName + "</LaunchTemplateName>"));
+ assertThat(group, containsString("<InstanceType>t3.micro</InstanceType>"));
+ }
+
+ /** InstancesDistribution is part of MixedInstancesPolicy and has to survive the round trip. */
+ @Test
+ void mixedInstancesPolicyInstancesDistributionIsMapped() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String ltName = "cfn-lt-dist-" + suffix;
+ String asgName = "cfn-asg-dist-" + suffix;
+ String stackName = "cfn-lt-dist-stack-" + suffix;
+ createLaunchTemplate(ltName);
+
+ createStack(stackName, """
+ {
+ "Resources": {
+ "Asg": {
+ "Type": "AWS::AutoScaling::AutoScalingGroup",
+ "Properties": {
+ "AutoScalingGroupName": "%s",
+ "MixedInstancesPolicy": {
+ "LaunchTemplate": {
+ "LaunchTemplateSpecification": {"LaunchTemplateName": "%s", "Version": "1"},
+ "Overrides": [{"InstanceType": "t3.micro"}, {"InstanceType": "t3.small"}]
+ },
+ "InstancesDistribution": {
+ "OnDemandBaseCapacity": 2,
+ "OnDemandPercentageAboveBaseCapacity": 50,
+ "SpotAllocationStrategy": "capacity-optimized"
+ }
+ },
+ "MinSize": 0,
+ "MaxSize": 0,
+ "DesiredCapacity": 0,
+ "AvailabilityZones": ["us-east-1a"]
+ }
+ }
+ }
+ }
+ """.formatted(asgName, ltName));
+
+ assertStackCreated(stackName);
+ String group = describeAutoScalingGroup(asgName);
+ assertThat(group, containsString("<OnDemandBaseCapacity>2</OnDemandBaseCapacity>"));
+ assertThat(group, containsString(
+ "<OnDemandPercentageAboveBaseCapacity>50</OnDemandPercentageAboveBaseCapacity>"));
+ assertThat(group, containsString("<SpotAllocationStrategy>capacity-optimized</SpotAllocationStrategy>"));
+ assertThat(group, containsString("<InstanceType>t3.micro</InstanceType>"));
+ assertThat(group, containsString("<InstanceType>t3.small</InstanceType>"));
+ }
+
+ /** A non-integer where AWS requires a number is a template error, not an absent property. */
+ @Test
+ void malformedInstancesDistributionNumberFailsTheStack() {
+ String suffix = Long.toString(System.nanoTime(), 36);
+ String ltName = "cfn-lt-baddist-" + suffix;
+ String stackName = "cfn-lt-baddist-stack-" + suffix;
+ createLaunchTemplate(ltName);
+
+ createStack(stackName, """
+ {
+ "Resources": {
+ "Asg": {
+ "Type": "AWS::AutoScaling::AutoScalingGroup",
+ "Properties": {
+ "AutoScalingGroupName": "cfn-asg-baddist-%s",
+ "MixedInstancesPolicy": {
+ "LaunchTemplate": {
+ "LaunchTemplateSpecification": {"LaunchTemplateName": "%s", "Version": "1"}
+ },
+ "InstancesDistribution": {"OnDemandBaseCapacity": "abc"}
+ },
+ "MinSize": 0,
+ "MaxSize": 0,
+ "DesiredCapacity": 0,
+ "AvailabilityZones": ["us-east-1a"]
+ }
+ }
+ }
+ }
+ """.formatted(suffix, ltName));
+
+ assertThat(describeStacks(stackName),
+ containsString("Value of property OnDemandBaseCapacity must be an integer."));
+ }
+}