Skip to content

Commit

Permalink
Add auto rollback feature to auto scaling groups preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
torvitas committed Feb 21, 2023
1 parent ae68366 commit ee06500
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/29513.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_autoscale_group: Add `preferences.auto_rollback` attribute
```
9 changes: 9 additions & 0 deletions internal/service/autoscaling/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ func ResourceGroup() *schema.Resource {
Optional: true,
Default: false,
},
"auto_rollback": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
},
},
Expand Down Expand Up @@ -3087,6 +3092,10 @@ func expandRefreshPreferences(tfMap map[string]interface{}) *autoscaling.Refresh
apiObject.SkipMatching = aws.Bool(v)
}

if v, ok := tfMap["auto_rollback"].(bool); ok {
apiObject.AutoRollback = aws.Bool(v)
}

return apiObject
}

Expand Down
47 changes: 47 additions & 0 deletions internal/service/autoscaling/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@ func TestAccAutoScalingGroup_InstanceRefresh_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.instance_warmup", ""),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.min_healthy_percentage", "0"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.skip_matching", "false"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.auto_rollback", "false"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.strategy", "Rolling"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.triggers.#", "0"),
),
Expand All @@ -1036,6 +1037,23 @@ func TestAccAutoScalingGroup_InstanceRefresh_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.instance_warmup", ""),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.min_healthy_percentage", "0"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.skip_matching", "true"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.auto_rollback", "false"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.strategy", "Rolling"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.triggers.#", "0"),
),
},
{
Config: testAccGroupConfig_instanceRefreshAutoRollback(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckGroupExists(ctx, resourceName, &group),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.#", "1"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.#", "1"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.checkpoint_delay", ""),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.checkpoint_percentages.#", "0"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.instance_warmup", ""),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.min_healthy_percentage", "0"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.skip_matching", "false"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.auto_rollback", "true"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.strategy", "Rolling"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.triggers.#", "0"),
),
Expand All @@ -1056,6 +1074,7 @@ func TestAccAutoScalingGroup_InstanceRefresh_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.instance_warmup", "10"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.min_healthy_percentage", "50"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.skip_matching", "false"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.auto_rollback", "false"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.strategy", "Rolling"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.triggers.#", "0"),
),
Expand Down Expand Up @@ -4228,6 +4247,34 @@ resource "aws_autoscaling_group" "test" {
`, rName))
}

func testAccGroupConfig_instanceRefreshAutoRollback(rName string) string {
return acctest.ConfigCompose(testAccGroupConfig_launchConfigurationBase(rName, "t3.nano"), fmt.Sprintf(`
resource "aws_autoscaling_group" "test" {
availability_zones = [data.aws_availability_zones.available.names[0]]
name = %[1]q
max_size = 2
min_size = 1
desired_capacity = 1
launch_configuration = aws_launch_configuration.test.name
instance_refresh {
strategy = "Rolling"
preferences {
min_healthy_percentage = 0
auto_rollback = true
}
}
tag {
key = "Name"
value = %[1]q
propagate_at_launch = true
}
}
`, rName))
}

func testAccGroupConfig_instanceRefreshFull(rName string) string {
return acctest.ConfigCompose(testAccGroupConfig_launchConfigurationBase(rName, "t3.nano"), fmt.Sprintf(`
resource "aws_autoscaling_group" "test" {
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/autoscaling_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ This configuration block supports the following:
* `instance_warmup` - (Optional) Number of seconds until a newly launched instance is configured and ready to use. Default behavior is to use the Auto Scaling Group's health check grace period.
* `min_healthy_percentage` - (Optional) Amount of capacity in the Auto Scaling group that must remain healthy during an instance refresh to allow the operation to continue, as a percentage of the desired capacity of the Auto Scaling group. Defaults to `90`.
* `skip_matching` - (Optional) Replace instances that already have your desired configuration. Defaults to `false`.
* `auto_rollback` - (Optional) Automatically rollback if instance refresh fails. Defaults to `false`.
* `triggers` - (Optional) Set of additional property names that will trigger an Instance Refresh. A refresh will always be triggered by a change in any of `launch_configuration`, `launch_template`, or `mixed_instances_policy`.
~> **NOTE:** A refresh is started when any of the following Auto Scaling Group properties change: `launch_configuration`, `launch_template`, `mixed_instances_policy`. Additional properties can be specified in the `triggers` property of `instance_refresh`.
Expand Down

0 comments on commit ee06500

Please sign in to comment.