Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_autoscaling_group: disable scale-in protection before draining instances #23187

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions internal/service/autoscaling/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,18 @@ func resourceGroupDrain(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error setting capacity to zero to drain: %s", err)
}

// Next, ensure that instances are not prevented from scaling in.
//
// The ASG's own scale-in protection setting doesn't make a difference here,
// as it only affects new instances, which won't be launched now that the
// desired capacity is set to 0. There is also the possibility that this ASG
// no longer applies scale-in protection to new instances, but there's still
// old ones that have it.
log.Printf("[DEBUG] Disabling scale-in protection for all instances in the group")
if err := disableASGScaleInProtections(d, conn); err != nil {
return fmt.Errorf("Error disabling scale-in protection for all instances: %s", err)
}

// Next, wait for the Auto Scaling Group to drain
log.Printf("[DEBUG] Waiting for group to have zero instances")
var g *autoscaling.Group
Expand Down Expand Up @@ -2365,3 +2377,40 @@ func flattenLaunchTemplateSpecification(lt *autoscaling.LaunchTemplateSpecificat

return result
}

// disableASGScaleInProtections disables scale-in protection for all instances
// in the given Auto-Scaling Group.
func disableASGScaleInProtections(d *schema.ResourceData, conn *autoscaling.AutoScaling) error {
g, err := getGroup(d.Id(), conn)
if err != nil {
return fmt.Errorf("Error getting group %s: %s", d.Id(), err)
}

var instanceIds []string
for _, instance := range g.Instances {
if aws.BoolValue(instance.ProtectedFromScaleIn) {
instanceIds = append(instanceIds, aws.StringValue(instance.InstanceId))
}
}

const chunkSize = 50 // API limit

for i := 0; i < len(instanceIds); i += chunkSize {
j := i + chunkSize
if j > len(instanceIds) {
j = len(instanceIds)
}

input := autoscaling.SetInstanceProtectionInput{
AutoScalingGroupName: aws.String(d.Id()),
InstanceIds: aws.StringSlice(instanceIds[i:j]),
ProtectedFromScaleIn: aws.Bool(false),
}

if _, err := conn.SetInstanceProtection(&input); err != nil {
return fmt.Errorf("Error disabling scale-in protections: %s", err)
}
}

return nil
}
77 changes: 77 additions & 0 deletions internal/service/autoscaling/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2306,6 +2306,33 @@ func TestAccAutoScalingGroup_launchTempPartitionNum(t *testing.T) {
})
}

func TestAccAutoScalingGroup_Destroy_whenProtectedFromScaleIn(t *testing.T) {
var group autoscaling.Group
rName := fmt.Sprintf("terraform-test-%s", sdkacctest.RandString(10))
resourceName := "aws_autoscaling_group.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, autoscaling.EndpointsID),
Providers: acctest.Providers,
CheckDestroy: testAccCheckGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccGroupConfig_DestroyWhenProtectedFromScaleIn_beforeDestroy(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckGroupExists(resourceName, &group),
testAccCheckGroupHealthyCapacity(&group, 2),
resource.TestCheckResourceAttr(resourceName, "protect_from_scale_in", "true"),
),
},
{
Config: testAccGroupConfig_DestroyWhenProtectedFromScaleIn_afterDestroy(),
// Reaching this step is good enough, as it indicates the ASG was destroyed successfully.
},
},
})
}

func testAccGroupNameGeneratedConfig() string {
return acctest.ConfigCompose(
acctest.ConfigAvailableAZsNoOptInDefaultExclude(),
Expand Down Expand Up @@ -4776,6 +4803,56 @@ resource "aws_autoscaling_group" "test" {
`
}

func testAccGroupConfig_DestroyWhenProtectedFromScaleIn_beforeDestroy(name string) string {
return acctest.ConfigAvailableAZsNoOptInDefaultExclude() +
fmt.Sprintf(`
data "aws_ami" "test" {
most_recent = true
owners = ["amazon"]

filter {
name = "name"
values = ["amzn-ami-hvm-*-x86_64-gp2"]
}
}

resource "aws_launch_configuration" "test" {
image_id = data.aws_ami.test.id
instance_type = "t3.micro"
}

resource "aws_autoscaling_group" "test" {
availability_zones = [data.aws_availability_zones.available.names[0]]
name = %[1]q
max_size = 2
min_size = 2
desired_capacity = 2
protect_from_scale_in = true
launch_configuration = aws_launch_configuration.test.name
}
`, name)
}

func testAccGroupConfig_DestroyWhenProtectedFromScaleIn_afterDestroy() string {
return acctest.ConfigAvailableAZsNoOptInDefaultExclude() +
`
data "aws_ami" "test" {
most_recent = true
owners = ["amazon"]

filter {
name = "name"
values = ["amzn-ami-hvm-*-x86_64-gp2"]
}
}

resource "aws_launch_configuration" "test" {
image_id = data.aws_ami.test.id
instance_type = "t3.micro"
}
`
}

func testAccCheckAutoScalingInstanceRefreshCount(group *autoscaling.Group, expected int) resource.TestCheckFunc {
return func(state *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).AutoScalingConn
Expand Down