From 79dd1c7d80026e745967c776e77ebbb44580d41c Mon Sep 17 00:00:00 2001 From: Jason Wieringa Date: Wed, 29 Jun 2016 02:36:34 -0400 Subject: [PATCH] Protect instance from autoscale in on aws_autoscaling_group (#6490) * Add support for NewInstancesProtectedFromScaleIn on aws_autoscaling_group * Add documentation for aws_autoscaling_group protect_from_scale_in --- .../providers/aws/resource_aws_autoscaling_group.go | 10 ++++++++++ .../aws/resource_aws_autoscaling_group_test.go | 5 +++++ .../providers/aws/r/autoscaling_group.html.markdown | 3 +++ 3 files changed, 18 insertions(+) diff --git a/builtin/providers/aws/resource_aws_autoscaling_group.go b/builtin/providers/aws/resource_aws_autoscaling_group.go index 06036b248..9d57d5395 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_group.go +++ b/builtin/providers/aws/resource_aws_autoscaling_group.go @@ -163,6 +163,12 @@ func resourceAwsAutoscalingGroup() *schema.Resource { Default: "1Minute", }, + "protect_from_scale_in": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + "tag": autoscalingTagsSchema(), }, } @@ -185,6 +191,7 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{}) autoScalingGroupOpts.LaunchConfigurationName = aws.String(d.Get("launch_configuration").(string)) autoScalingGroupOpts.MinSize = aws.Int64(int64(d.Get("min_size").(int))) autoScalingGroupOpts.MaxSize = aws.Int64(int64(d.Get("max_size").(int))) + autoScalingGroupOpts.NewInstancesProtectedFromScaleIn = aws.Bool(d.Get("protect_from_scale_in").(bool)) // Availability Zones are optional if VPC Zone Identifer(s) are specified if v, ok := d.GetOk("availability_zones"); ok && v.(*schema.Set).Len() > 0 { @@ -278,6 +285,7 @@ func resourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) e d.Set("name", g.AutoScalingGroupName) d.Set("tag", autoscalingTagDescriptionsToSlice(g.Tags)) d.Set("vpc_zone_identifier", strings.Split(*g.VPCZoneIdentifier, ",")) + d.Set("protect_from_scale_in", g.NewInstancesProtectedFromScaleIn) // If no termination polices are explicitly configured and the upstream state // is only using the "Default" policy, clear the state to make it consistent @@ -307,6 +315,8 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{}) AutoScalingGroupName: aws.String(d.Id()), } + opts.NewInstancesProtectedFromScaleIn = aws.Bool(d.Get("protect_from_scale_in").(bool)) + if d.HasChange("default_cooldown") { opts.DefaultCooldown = aws.Int64(int64(d.Get("default_cooldown").(int))) } diff --git a/builtin/providers/aws/resource_aws_autoscaling_group_test.go b/builtin/providers/aws/resource_aws_autoscaling_group_test.go index f78e324b5..9cc714a07 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_group_test.go +++ b/builtin/providers/aws/resource_aws_autoscaling_group_test.go @@ -54,6 +54,8 @@ func TestAccAWSAutoScalingGroup_basic(t *testing.T) { "aws_autoscaling_group.bar", "termination_policies.0", "OldestInstance"), resource.TestCheckResourceAttr( "aws_autoscaling_group.bar", "termination_policies.1", "ClosestToNextInstanceHour"), + resource.TestCheckResourceAttr( + "aws_autoscaling_group.bar", "protect_from_scale_in", "false"), ), }, @@ -66,6 +68,8 @@ func TestAccAWSAutoScalingGroup_basic(t *testing.T) { "aws_autoscaling_group.bar", "desired_capacity", "5"), resource.TestCheckResourceAttr( "aws_autoscaling_group.bar", "termination_policies.0", "ClosestToNextInstanceHour"), + resource.TestCheckResourceAttr( + "aws_autoscaling_group.bar", "protect_from_scale_in", "true"), testLaunchConfigurationName("aws_autoscaling_group.bar", &lc), testAccCheckAutoscalingTags(&group.Tags, "Bar", map[string]interface{}{ "value": "bar-foo", @@ -602,6 +606,7 @@ resource "aws_autoscaling_group" "bar" { desired_capacity = 5 force_delete = true termination_policies = ["ClosestToNextInstanceHour"] + protect_from_scale_in = true launch_configuration = "${aws_launch_configuration.new.name}" diff --git a/website/source/docs/providers/aws/r/autoscaling_group.html.markdown b/website/source/docs/providers/aws/r/autoscaling_group.html.markdown index 915198ed3..e8599f44c 100644 --- a/website/source/docs/providers/aws/r/autoscaling_group.html.markdown +++ b/website/source/docs/providers/aws/r/autoscaling_group.html.markdown @@ -86,6 +86,9 @@ The following arguments are supported: on both create and update operations. (Takes precedence over `min_elb_capacity` behavior.) (See also [Waiting for Capacity](#waiting-for-capacity) below.) +* `protect_from_scale_in` (Optional) Allows setting instance protection. The + autoscaling group will not select instances with this setting for terminination + during scale in events. Tags support the following: