Skip to content

Commit 79dd1c7

Browse files
jwieringastack72
authored andcommitted
Protect instance from autoscale in on aws_autoscaling_group (hashicorp#6490)
* Add support for NewInstancesProtectedFromScaleIn on aws_autoscaling_group * Add documentation for aws_autoscaling_group protect_from_scale_in
1 parent 20240c0 commit 79dd1c7

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

builtin/providers/aws/resource_aws_autoscaling_group.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
163163
Default: "1Minute",
164164
},
165165

166+
"protect_from_scale_in": &schema.Schema{
167+
Type: schema.TypeBool,
168+
Optional: true,
169+
Default: false,
170+
},
171+
166172
"tag": autoscalingTagsSchema(),
167173
},
168174
}
@@ -185,6 +191,7 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{})
185191
autoScalingGroupOpts.LaunchConfigurationName = aws.String(d.Get("launch_configuration").(string))
186192
autoScalingGroupOpts.MinSize = aws.Int64(int64(d.Get("min_size").(int)))
187193
autoScalingGroupOpts.MaxSize = aws.Int64(int64(d.Get("max_size").(int)))
194+
autoScalingGroupOpts.NewInstancesProtectedFromScaleIn = aws.Bool(d.Get("protect_from_scale_in").(bool))
188195

189196
// Availability Zones are optional if VPC Zone Identifer(s) are specified
190197
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
278285
d.Set("name", g.AutoScalingGroupName)
279286
d.Set("tag", autoscalingTagDescriptionsToSlice(g.Tags))
280287
d.Set("vpc_zone_identifier", strings.Split(*g.VPCZoneIdentifier, ","))
288+
d.Set("protect_from_scale_in", g.NewInstancesProtectedFromScaleIn)
281289

282290
// If no termination polices are explicitly configured and the upstream state
283291
// is only using the "Default" policy, clear the state to make it consistent
@@ -307,6 +315,8 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{})
307315
AutoScalingGroupName: aws.String(d.Id()),
308316
}
309317

318+
opts.NewInstancesProtectedFromScaleIn = aws.Bool(d.Get("protect_from_scale_in").(bool))
319+
310320
if d.HasChange("default_cooldown") {
311321
opts.DefaultCooldown = aws.Int64(int64(d.Get("default_cooldown").(int)))
312322
}

builtin/providers/aws/resource_aws_autoscaling_group_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ func TestAccAWSAutoScalingGroup_basic(t *testing.T) {
5454
"aws_autoscaling_group.bar", "termination_policies.0", "OldestInstance"),
5555
resource.TestCheckResourceAttr(
5656
"aws_autoscaling_group.bar", "termination_policies.1", "ClosestToNextInstanceHour"),
57+
resource.TestCheckResourceAttr(
58+
"aws_autoscaling_group.bar", "protect_from_scale_in", "false"),
5759
),
5860
},
5961

@@ -66,6 +68,8 @@ func TestAccAWSAutoScalingGroup_basic(t *testing.T) {
6668
"aws_autoscaling_group.bar", "desired_capacity", "5"),
6769
resource.TestCheckResourceAttr(
6870
"aws_autoscaling_group.bar", "termination_policies.0", "ClosestToNextInstanceHour"),
71+
resource.TestCheckResourceAttr(
72+
"aws_autoscaling_group.bar", "protect_from_scale_in", "true"),
6973
testLaunchConfigurationName("aws_autoscaling_group.bar", &lc),
7074
testAccCheckAutoscalingTags(&group.Tags, "Bar", map[string]interface{}{
7175
"value": "bar-foo",
@@ -602,6 +606,7 @@ resource "aws_autoscaling_group" "bar" {
602606
desired_capacity = 5
603607
force_delete = true
604608
termination_policies = ["ClosestToNextInstanceHour"]
609+
protect_from_scale_in = true
605610
606611
launch_configuration = "${aws_launch_configuration.new.name}"
607612

website/source/docs/providers/aws/r/autoscaling_group.html.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ The following arguments are supported:
8686
on both create and update operations. (Takes precedence over
8787
`min_elb_capacity` behavior.)
8888
(See also [Waiting for Capacity](#waiting-for-capacity) below.)
89+
* `protect_from_scale_in` (Optional) Allows setting instance protection. The
90+
autoscaling group will not select instances with this setting for terminination
91+
during scale in events.
8992

9093
Tags support the following:
9194

0 commit comments

Comments
 (0)