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

Add auto rollback feature to auto scaling groups preferences #29513

Merged
merged 4 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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_autoscaling_group: Add `auto_rollback` to the `instance_refresh.preferences` configuration block
```
8 changes: 8 additions & 0 deletions internal/service/autoscaling/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ func ResourceGroup() *schema.Resource {
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"auto_rollback": {
Type: schema.TypeBool,
Optional: true,
},
"checkpoint_delay": {
Type: nullable.TypeNullableInt,
Optional: true,
Expand Down Expand Up @@ -3063,6 +3067,10 @@ func expandRefreshPreferences(tfMap map[string]interface{}) *autoscaling.Refresh

apiObject := &autoscaling.RefreshPreferences{}

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

if v, ok := tfMap["checkpoint_delay"].(string); ok {
if v, null, _ := nullable.Int(v).Value(); !null {
apiObject.CheckpointDelay = aws.Int64(v)
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 @@ -1016,6 +1016,7 @@ func TestAccAutoScalingGroup_InstanceRefresh_basic(t *testing.T) {
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.auto_rollback", "false"),
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", ""),
Expand All @@ -1031,6 +1032,7 @@ func TestAccAutoScalingGroup_InstanceRefresh_basic(t *testing.T) {
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.auto_rollback", "false"),
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", ""),
Expand All @@ -1040,12 +1042,29 @@ func TestAccAutoScalingGroup_InstanceRefresh_basic(t *testing.T) {
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.auto_rollback", "true"),
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.strategy", "Rolling"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.triggers.#", "0"),
),
},
{
Config: testAccGroupConfig_instanceRefreshFull(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.auto_rollback", "false"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.checkpoint_delay", "25"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.checkpoint_percentages.#", "5"),
resource.TestCheckResourceAttr(resourceName, "instance_refresh.0.preferences.0.checkpoint_percentages.0", "1"),
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 {
auto_rollback = true
min_healthy_percentage = 0
}
}

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