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
This commit is contained in:
Jason Wieringa 2016-06-29 02:36:34 -04:00 committed by Paul Stack
parent 20240c0e52
commit 79dd1c7d80
3 changed files with 18 additions and 0 deletions

View File

@ -163,6 +163,12 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
Default: "1Minute", Default: "1Minute",
}, },
"protect_from_scale_in": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"tag": autoscalingTagsSchema(), "tag": autoscalingTagsSchema(),
}, },
} }
@ -185,6 +191,7 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{})
autoScalingGroupOpts.LaunchConfigurationName = aws.String(d.Get("launch_configuration").(string)) autoScalingGroupOpts.LaunchConfigurationName = aws.String(d.Get("launch_configuration").(string))
autoScalingGroupOpts.MinSize = aws.Int64(int64(d.Get("min_size").(int))) autoScalingGroupOpts.MinSize = aws.Int64(int64(d.Get("min_size").(int)))
autoScalingGroupOpts.MaxSize = aws.Int64(int64(d.Get("max_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 // Availability Zones are optional if VPC Zone Identifer(s) are specified
if v, ok := d.GetOk("availability_zones"); ok && v.(*schema.Set).Len() > 0 { 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("name", g.AutoScalingGroupName)
d.Set("tag", autoscalingTagDescriptionsToSlice(g.Tags)) d.Set("tag", autoscalingTagDescriptionsToSlice(g.Tags))
d.Set("vpc_zone_identifier", strings.Split(*g.VPCZoneIdentifier, ",")) 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 // If no termination polices are explicitly configured and the upstream state
// is only using the "Default" policy, clear the state to make it consistent // 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()), AutoScalingGroupName: aws.String(d.Id()),
} }
opts.NewInstancesProtectedFromScaleIn = aws.Bool(d.Get("protect_from_scale_in").(bool))
if d.HasChange("default_cooldown") { if d.HasChange("default_cooldown") {
opts.DefaultCooldown = aws.Int64(int64(d.Get("default_cooldown").(int))) opts.DefaultCooldown = aws.Int64(int64(d.Get("default_cooldown").(int)))
} }

View File

@ -54,6 +54,8 @@ func TestAccAWSAutoScalingGroup_basic(t *testing.T) {
"aws_autoscaling_group.bar", "termination_policies.0", "OldestInstance"), "aws_autoscaling_group.bar", "termination_policies.0", "OldestInstance"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "termination_policies.1", "ClosestToNextInstanceHour"), "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"), "aws_autoscaling_group.bar", "desired_capacity", "5"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"aws_autoscaling_group.bar", "termination_policies.0", "ClosestToNextInstanceHour"), "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), testLaunchConfigurationName("aws_autoscaling_group.bar", &lc),
testAccCheckAutoscalingTags(&group.Tags, "Bar", map[string]interface{}{ testAccCheckAutoscalingTags(&group.Tags, "Bar", map[string]interface{}{
"value": "bar-foo", "value": "bar-foo",
@ -602,6 +606,7 @@ resource "aws_autoscaling_group" "bar" {
desired_capacity = 5 desired_capacity = 5
force_delete = true force_delete = true
termination_policies = ["ClosestToNextInstanceHour"] termination_policies = ["ClosestToNextInstanceHour"]
protect_from_scale_in = true
launch_configuration = "${aws_launch_configuration.new.name}" launch_configuration = "${aws_launch_configuration.new.name}"

View File

@ -86,6 +86,9 @@ The following arguments are supported:
on both create and update operations. (Takes precedence over on both create and update operations. (Takes precedence over
`min_elb_capacity` behavior.) `min_elb_capacity` behavior.)
(See also [Waiting for Capacity](#waiting-for-capacity) below.) (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: Tags support the following: