From 79ae2d7fb2121ff96b7be60e2aabf568fc68795a Mon Sep 17 00:00:00 2001 From: Lynch Date: Wed, 28 Oct 2020 14:49:25 -0400 Subject: [PATCH 1/9] added LaunchTemplateSpec for MixedPolicy override --- aws/resource_aws_autoscaling_group.go | 34 +++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_autoscaling_group.go b/aws/resource_aws_autoscaling_group.go index f929fe0fe4f1..47da4e070910 100644 --- a/aws/resource_aws_autoscaling_group.go +++ b/aws/resource_aws_autoscaling_group.go @@ -189,6 +189,31 @@ func resourceAwsAutoscalingGroup() *schema.Resource { Type: schema.TypeString, Optional: true, }, + "launch_template_specification": { + Type: schema.TypeList, + Optional: true, + MinItems: 1, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "launch_template_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "launch_template_name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "version": { + Type: schema.TypeString, + Optional: true, + Default: "$Default", + }, + }, + }, + }, "weighted_capacity": { Type: schema.TypeString, Optional: true, @@ -1577,6 +1602,10 @@ func expandAutoScalingLaunchTemplateOverride(m map[string]interface{}) *autoscal launchTemplateOverrides.InstanceType = aws.String(v.(string)) } + if v, ok := m["launch_template_specification"]; ok && v.(string) != "" { + launchTemplateOverrides.LaunchTemplateSpecification = expandAutoScalingLaunchTemplateSpecification(v.([]interface{})) + } + if v, ok := m["weighted_capacity"]; ok && v.(string) != "" { launchTemplateOverrides.WeightedCapacity = aws.String(v.(string)) } @@ -1668,8 +1697,9 @@ func flattenAutoScalingLaunchTemplateOverrides(launchTemplateOverrides []*autosc continue } m := map[string]interface{}{ - "instance_type": aws.StringValue(launchTemplateOverride.InstanceType), - "weighted_capacity": aws.StringValue(launchTemplateOverride.WeightedCapacity), + "instance_type": aws.StringValue(launchTemplateOverride.InstanceType), + "launch_template_specification": flattenAutoScalingLaunchTemplateSpecification(launchTemplateOverride.LaunchTemplateSpecification), + "weighted_capacity": aws.StringValue(launchTemplateOverride.WeightedCapacity), } l[i] = m } From de8963b8aa2599baf9a14dc9daba347bcdeddfc1 Mon Sep 17 00:00:00 2001 From: Lynch Date: Wed, 28 Oct 2020 15:09:27 -0400 Subject: [PATCH 2/9] modifiedi the min to 0 for optional resources --- aws/resource_aws_autoscaling_group.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_autoscaling_group.go b/aws/resource_aws_autoscaling_group.go index 47da4e070910..26d52a3b8e7f 100644 --- a/aws/resource_aws_autoscaling_group.go +++ b/aws/resource_aws_autoscaling_group.go @@ -192,7 +192,7 @@ func resourceAwsAutoscalingGroup() *schema.Resource { "launch_template_specification": { Type: schema.TypeList, Optional: true, - MinItems: 1, + MinItems: 0, MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ From d59093c8c6d5bc7e15c675188d716280eda45e02 Mon Sep 17 00:00:00 2001 From: Lynch Date: Thu, 29 Oct 2020 12:23:11 -0400 Subject: [PATCH 3/9] updated tests and documentation --- aws/resource_aws_autoscaling_group.go | 4 +- aws/resource_aws_autoscaling_group_test.go | 93 +++++++++++++++++++ .../docs/r/autoscaling_group.html.markdown | 45 +++++++++ 3 files changed, 140 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_autoscaling_group.go b/aws/resource_aws_autoscaling_group.go index 26d52a3b8e7f..c900f811d85d 100644 --- a/aws/resource_aws_autoscaling_group.go +++ b/aws/resource_aws_autoscaling_group.go @@ -1602,8 +1602,8 @@ func expandAutoScalingLaunchTemplateOverride(m map[string]interface{}) *autoscal launchTemplateOverrides.InstanceType = aws.String(v.(string)) } - if v, ok := m["launch_template_specification"]; ok && v.(string) != "" { - launchTemplateOverrides.LaunchTemplateSpecification = expandAutoScalingLaunchTemplateSpecification(v.([]interface{})) + if v, ok := m["launch_template_specification"]; ok && v.([]interface{}) != nil { + launchTemplateOverrides.LaunchTemplateSpecification = expandAutoScalingLaunchTemplateSpecification(m["launch_template_specification"].([]interface{})) } if v, ok := m["weighted_capacity"]; ok && v.(string) != "" { diff --git a/aws/resource_aws_autoscaling_group_test.go b/aws/resource_aws_autoscaling_group_test.go index 7a569a999895..df0b278bfbb9 100644 --- a/aws/resource_aws_autoscaling_group_test.go +++ b/aws/resource_aws_autoscaling_group_test.go @@ -2001,6 +2001,46 @@ func TestAccAWSAutoScalingGroup_MixedInstancesPolicy_LaunchTemplate_Override_Ins }) } +func TestAccAWSAutoScalingGroup_MixedInstancesPolicy_LaunchTemplate_Override_InstanceType_With_LaunchTemplateSpecification(t *testing.T) { + var group autoscaling.Group + resourceName := "aws_autoscaling_group.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + rName2 := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAutoScalingGroupConfig_MixedInstancesPolicy_LaunchTemplate_Override_InstanceType_With_LaunchTemplateSpecification(rName, rName2), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAutoScalingGroupExists(resourceName, &group), + resource.TestCheckResourceAttr(resourceName, "mixed_instances_policy.#", "1"), + resource.TestCheckResourceAttr(resourceName, "mixed_instances_policy.0.launch_template.#", "1"), + resource.TestCheckResourceAttr(resourceName, "mixed_instances_policy.0.launch_template.0.override.#", "2"), + resource.TestCheckResourceAttr(resourceName, "mixed_instances_policy.0.launch_template.0.override.0.instance_type", "t2.micro"), + resource.TestCheckResourceAttr(resourceName, "mixed_instances_policy.0.launch_template.0.override.1.instance_type", "t4g.micro"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{ + "force_delete", + "initial_lifecycle_hook", + "name_prefix", + "tag", + "tags", + "wait_for_capacity_timeout", + "wait_for_elb_capacity", + }, + }, + }, + }) +} + func TestAccAWSAutoScalingGroup_MixedInstancesPolicy_LaunchTemplate_Override_WeightedCapacity(t *testing.T) { var group autoscaling.Group resourceName := "aws_autoscaling_group.test" @@ -3773,6 +3813,26 @@ resource "aws_launch_template" "test" { `, rName) } +func testAccAWSAutoScalingGroupConfig_MixedInstancesPolicy_Arm_Base(rName string) string { + return fmt.Sprintf(` +data "aws_ami" "testarm" { + most_recent = true + owners = ["amazon"] + + filter { + name = "name" + values = ["amzn2-ami-hvm-*-arm64-gp2"] + } +} + +resource "aws_launch_template" "testarm" { + image_id = data.aws_ami.testarm.id + instance_type = "t4g.micro" + name = %q +} +`, rName) +} + func testAccAWSAutoScalingGroupConfig_MixedInstancesPolicy(rName string) string { return testAccAWSAutoScalingGroupConfig_MixedInstancesPolicy_Base(rName) + fmt.Sprintf(` @@ -4080,6 +4140,39 @@ resource "aws_autoscaling_group" "test" { `, rName, instanceType) } +func testAccAWSAutoScalingGroupConfig_MixedInstancesPolicy_LaunchTemplate_Override_InstanceType_With_LaunchTemplateSpecification(rName, rName2 string) string { + return testAccAWSAutoScalingGroupConfig_MixedInstancesPolicy_Base(rName) + + testAccAWSAutoScalingGroupConfig_MixedInstancesPolicy_Arm_Base(rName2) + + fmt.Sprintf(` + +resource "aws_autoscaling_group" "test" { + availability_zones = [data.aws_availability_zones.available.names[0]] + desired_capacity = 0 + max_size = 0 + min_size = 0 + name = %q + + mixed_instances_policy { + launch_template { + launch_template_specification { + launch_template_id = aws_launch_template.test.id + } + + override { + instance_type = "t2.micro" + } + override { + instance_type = "t4g.micro" + launch_template_specification { + launch_template_id = aws_launch_template.testarm.id + } + } + } + } +} +`, rName) +} + func testAccAWSAutoScalingGroupConfig_MixedInstancesPolicy_LaunchTemplate_Override_WeightedCapacity(rName string) string { return testAccAWSAutoScalingGroupConfig_MixedInstancesPolicy_Base(rName) + fmt.Sprintf(` diff --git a/website/docs/r/autoscaling_group.html.markdown b/website/docs/r/autoscaling_group.html.markdown index b962c7de37eb..e9eba45dd589 100644 --- a/website/docs/r/autoscaling_group.html.markdown +++ b/website/docs/r/autoscaling_group.html.markdown @@ -132,6 +132,50 @@ resource "aws_autoscaling_group" "example" { } ``` +### Mixed Instances Policy with Instance level LaunchTemplateSpecification Overrides +Some instance types might require a launch template with a different AMI (Graviton). +```hcl +resource "aws_launch_template" "example" { + name_prefix = "example" + image_id = data.aws_ami.example.id + instance_type = "c5.large" +} + +resource "aws_launch_template" "example2" { + name_prefix = "example2" + image_id = data.aws_ami.example2.id +} + +resource "aws_autoscaling_group" "example" { + availability_zones = ["us-east-1a"] + desired_capacity = 1 + max_size = 1 + min_size = 1 + + mixed_instances_policy { + launch_template { + launch_template_specification { + launch_template_id = aws_launch_template.example.id + } + + override { + instance_type = "c4.large" + weighted_capacity = "3" + } + + override { + instance_type = "c6g.large" + launch_template_specification { + launch_template_id = aws_launch_template.example2.id + } + weighted_capacity = "2" + } + } + } +} +``` + + ## Interpolated tags ```hcl @@ -287,6 +331,7 @@ This configuration block supports the following: This configuration block supports the following: * `instance_type` - (Optional) Override the instance type in the Launch Template. +* `launch_template_specification` - (Optional) Override the instance launch template specification in the Launch Template. * `weighted_capacity` - (Optional) The number of capacity units, which gives the instance type a proportional weight to other instance types. ### tag and tags From bba439abeacc60c80cfd8f7f8cf3a54e0d766f1a Mon Sep 17 00:00:00 2001 From: Tyler Lynch Date: Thu, 19 Nov 2020 14:14:32 -0500 Subject: [PATCH 4/9] Formatting markdown as per linter --- website/docs/r/autoscaling_group.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/website/docs/r/autoscaling_group.html.markdown b/website/docs/r/autoscaling_group.html.markdown index 21607882865b..e60b30f92e0e 100644 --- a/website/docs/r/autoscaling_group.html.markdown +++ b/website/docs/r/autoscaling_group.html.markdown @@ -134,6 +134,7 @@ resource "aws_autoscaling_group" "example" { ### Mixed Instances Policy with Instance level LaunchTemplateSpecification Overrides Some instance types might require a launch template with a different AMI (Graviton). + ```hcl resource "aws_launch_template" "example" { name_prefix = "example" @@ -142,8 +143,8 @@ resource "aws_launch_template" "example" { } resource "aws_launch_template" "example2" { - name_prefix = "example2" - image_id = data.aws_ami.example2.id + name_prefix = "example2" + image_id = data.aws_ami.example2.id } resource "aws_autoscaling_group" "example" { @@ -164,7 +165,7 @@ resource "aws_autoscaling_group" "example" { } override { - instance_type = "c6g.large" + instance_type = "c6g.large" launch_template_specification { launch_template_id = aws_launch_template.example2.id } From 4b71d6ee184f0ce464838a9ee47ead19815bf5c3 Mon Sep 17 00:00:00 2001 From: Tyler Lynch Date: Wed, 25 Nov 2020 13:03:43 -0500 Subject: [PATCH 5/9] Updated documentaiton to better reflect use cases. --- website/docs/r/autoscaling_group.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/autoscaling_group.html.markdown b/website/docs/r/autoscaling_group.html.markdown index e60b30f92e0e..b7ce0bdc19c0 100644 --- a/website/docs/r/autoscaling_group.html.markdown +++ b/website/docs/r/autoscaling_group.html.markdown @@ -133,7 +133,7 @@ resource "aws_autoscaling_group" "example" { ``` ### Mixed Instances Policy with Instance level LaunchTemplateSpecification Overrides -Some instance types might require a launch template with a different AMI (Graviton). +When using a diverse instance set, some instance types might require a launch template with configuration values unique to that instance type such as a different AMI (Graviton), different EBS configuration. ```hcl resource "aws_launch_template" "example" { From d5adfb67de9b4be499e4c9adf6724ccd2a56ca71 Mon Sep 17 00:00:00 2001 From: Tyler Lynch Date: Wed, 25 Nov 2020 15:43:19 -0500 Subject: [PATCH 6/9] Fixed terrfmt issues in test file for ASG --- aws/resource_aws_autoscaling_group_test.go | 28 +++++++++++----------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/aws/resource_aws_autoscaling_group_test.go b/aws/resource_aws_autoscaling_group_test.go index da5080555588..2944352694b3 100644 --- a/aws/resource_aws_autoscaling_group_test.go +++ b/aws/resource_aws_autoscaling_group_test.go @@ -3815,20 +3815,20 @@ resource "aws_launch_template" "test" { func testAccAWSAutoScalingGroupConfig_MixedInstancesPolicy_Arm_Base(rName string) string { return fmt.Sprintf(` data "aws_ami" "testarm" { - most_recent = true - owners = ["amazon"] + most_recent = true + owners = ["amazon"] - filter { - name = "name" - values = ["amzn2-ami-hvm-*-arm64-gp2"] - } + filter { + name = "name" + values = ["amzn2-ami-hvm-*-arm64-gp2"] + } } resource "aws_launch_template" "testarm" { - image_id = data.aws_ami.testarm.id - instance_type = "t4g.micro" - name = %q -} + image_id = data.aws_ami.testarm.id + instance_type = "t4g.micro" + name = %q +} `, rName) } @@ -4161,10 +4161,10 @@ resource "aws_autoscaling_group" "test" { instance_type = "t2.micro" } override { - instance_type = "t4g.micro" - launch_template_specification { - launch_template_id = aws_launch_template.testarm.id - } + instance_type = "t4g.micro" + launch_template_specification { + launch_template_id = aws_launch_template.testarm.id + } } } } From cf056c9e7289a0f9975392756ee97e27ee6574bc Mon Sep 17 00:00:00 2001 From: Tyler Lynch Date: Sun, 21 Feb 2021 11:51:26 -0500 Subject: [PATCH 7/9] Updated acceptance tests to verify the expected arm64 override LT is set. Verify that no override LT is set for x86. --- aws/resource_aws_autoscaling_group_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/aws/resource_aws_autoscaling_group_test.go b/aws/resource_aws_autoscaling_group_test.go index 3bd6f8b94034..121dcd95a2c3 100644 --- a/aws/resource_aws_autoscaling_group_test.go +++ b/aws/resource_aws_autoscaling_group_test.go @@ -2185,7 +2185,9 @@ func TestAccAWSAutoScalingGroup_MixedInstancesPolicy_LaunchTemplate_Override_Ins resource.TestCheckResourceAttr(resourceName, "mixed_instances_policy.0.launch_template.#", "1"), resource.TestCheckResourceAttr(resourceName, "mixed_instances_policy.0.launch_template.0.override.#", "2"), resource.TestCheckResourceAttr(resourceName, "mixed_instances_policy.0.launch_template.0.override.0.instance_type", "t2.micro"), + resource.TestCheckNoResourceAttr(resourceName, "mixed_instances_policy.0.launch_template.0.override.0.launch_template_specification.#"), resource.TestCheckResourceAttr(resourceName, "mixed_instances_policy.0.launch_template.0.override.1.instance_type", "t4g.micro"), + resource.TestCheckResourceAttrPair(resourceName, "mixed_instances_policy.0.launch_template.0.override.1.launch_template_specification.0.launch_template_id", "aws_launch_template.testarm", "id"), ), }, { From b594c03ae04288cd76718274f58cc7abbae456cf Mon Sep 17 00:00:00 2001 From: Tyler Lynch Date: Sun, 21 Feb 2021 11:56:29 -0500 Subject: [PATCH 8/9] Updated documentation to be inline with AWS documentation regarding launch template overrides in a mixed instance policy. --- website/docs/r/autoscaling_group.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/autoscaling_group.html.markdown b/website/docs/r/autoscaling_group.html.markdown index 06ed4e1354f2..d9bd775984bc 100644 --- a/website/docs/r/autoscaling_group.html.markdown +++ b/website/docs/r/autoscaling_group.html.markdown @@ -176,7 +176,7 @@ resource "aws_autoscaling_group" "example" { ### Mixed Instances Policy with Instance level LaunchTemplateSpecification Overrides -When using a diverse instance set, some instance types might require a launch template with configuration values unique to that instance type such as a different AMI (Graviton), different EBS configuration. +When using a diverse instance set, some instance types might require a launch template with configuration values unique to that instance type such as a different AMI (Graviton2), architecture specific user data script, different EBS configuration, or different networking configuration. ```hcl resource "aws_launch_template" "example" { From b0917f1b1fc7e90b9b6e696d0932a47c61fbbaa3 Mon Sep 17 00:00:00 2001 From: Tyler Lynch Date: Thu, 25 Feb 2021 16:29:06 -0500 Subject: [PATCH 9/9] Updated changelog for PR 16325 --- .changelog/16325.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/16325.txt diff --git a/.changelog/16325.txt b/.changelog/16325.txt new file mode 100644 index 000000000000..0f375a56b0b4 --- /dev/null +++ b/.changelog/16325.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_autoscaling_group: Added support Auto Scaling groups with multiple launch templates using a mixed instances policy +``` \ No newline at end of file