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 enabled attribute for aws_autoscaling_policy #12625

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/12625.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_autoscaling_policy: Add `enabled` argument
```
121 changes: 64 additions & 57 deletions internal/service/autoscaling/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,14 @@ func ResourcePolicy() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"adjustment_type": {
Type: schema.TypeString,
Optional: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"autoscaling_group_name": {
Type: schema.TypeString,
Required: true,
Expand All @@ -52,6 +47,11 @@ func ResourcePolicy() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"estimated_instance_warmup": {
Type: schema.TypeInt,
Optional: true,
Expand All @@ -66,6 +66,11 @@ func ResourcePolicy() *schema.Resource {
Optional: true,
ValidateFunc: validation.IntAtLeast(1),
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"policy_type": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -78,6 +83,17 @@ func ResourcePolicy() *schema.Resource {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"max_capacity_breach_behavior": {
Type: schema.TypeString,
Optional: true,
Default: autoscaling.PredictiveScalingMaxCapacityBreachBehaviorHonorMaxCapacity,
ValidateFunc: validation.StringInSlice(autoscaling.PredictiveScalingMaxCapacityBreachBehavior_Values(), false),
},
"max_capacity_buffer": {
Type: nullable.TypeNullableInt,
Optional: true,
ValidateFunc: nullable.ValidateTypeStringNullableIntBetween(0, 100),
},
"metric_specification": {
Type: schema.TypeList,
Required: true,
Expand Down Expand Up @@ -126,16 +142,17 @@ func ResourcePolicy() *schema.Resource {
},
},
},
"predefined_metric_pair_specification": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
"predefined_load_metric_specification": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"predictive_scaling_configuration.0.metric_specification.0.customized_load_metric_specification"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"predefined_metric_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(autoscaling.PredefinedMetricPairType_Values(), false),
ValidateFunc: validation.StringInSlice(autoscaling.PredefinedLoadMetricType_Values(), false),
},
"resource_label": {
Type: schema.TypeString,
Expand All @@ -144,17 +161,16 @@ func ResourcePolicy() *schema.Resource {
},
},
},
"predefined_scaling_metric_specification": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"predictive_scaling_configuration.0.metric_specification.0.customized_scaling_metric_specification"},
"predefined_metric_pair_specification": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"predefined_metric_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(autoscaling.PredefinedScalingMetricType_Values(), false),
ValidateFunc: validation.StringInSlice(autoscaling.PredefinedMetricPairType_Values(), false),
},
"resource_label": {
Type: schema.TypeString,
Expand All @@ -163,17 +179,17 @@ func ResourcePolicy() *schema.Resource {
},
},
},
"predefined_load_metric_specification": {
"predefined_scaling_metric_specification": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"predictive_scaling_configuration.0.metric_specification.0.customized_load_metric_specification"},
ConflictsWith: []string{"predictive_scaling_configuration.0.metric_specification.0.customized_scaling_metric_specification"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"predefined_metric_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(autoscaling.PredefinedLoadMetricType_Values(), false),
ValidateFunc: validation.StringInSlice(autoscaling.PredefinedScalingMetricType_Values(), false),
},
"resource_label": {
Type: schema.TypeString,
Expand All @@ -189,17 +205,6 @@ func ResourcePolicy() *schema.Resource {
},
},
},
"max_capacity_breach_behavior": {
Type: schema.TypeString,
Optional: true,
Default: autoscaling.PredictiveScalingMaxCapacityBreachBehaviorHonorMaxCapacity,
ValidateFunc: validation.StringInSlice(autoscaling.PredictiveScalingMaxCapacityBreachBehavior_Values(), false),
},
"max_capacity_buffer": {
Type: nullable.TypeNullableInt,
Optional: true,
ValidateFunc: nullable.ValidateTypeStringNullableIntBetween(0, 100),
},
"mode": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -247,24 +252,6 @@ func ResourcePolicy() *schema.Resource {
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"predefined_metric_specification": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"target_tracking_configuration.0.customized_metric_specification"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"predefined_metric_type": {
Type: schema.TypeString,
Required: true,
},
"resource_label": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
"customized_metric_specification": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -307,15 +294,33 @@ func ResourcePolicy() *schema.Resource {
},
},
},
"target_value": {
Type: schema.TypeFloat,
Required: true,
},
"disable_scale_in": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"predefined_metric_specification": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"target_tracking_configuration.0.customized_metric_specification"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"predefined_metric_type": {
Type: schema.TypeString,
Required: true,
},
"resource_label": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
"target_value": {
Type: schema.TypeFloat,
Required: true,
},
},
},
},
Expand Down Expand Up @@ -448,14 +453,15 @@ func resourcePolicyRead(d *schema.ResourceData, meta interface{}) error {
d.Set("arn", p.PolicyARN)
d.Set("autoscaling_group_name", p.AutoScalingGroupName)
d.Set("cooldown", p.Cooldown)
d.Set("enabled", p.Enabled)
d.Set("estimated_instance_warmup", p.EstimatedInstanceWarmup)
d.Set("metric_aggregation_type", p.MetricAggregationType)
d.Set("name", p.PolicyName)
d.Set("policy_type", p.PolicyType)
if p.MinAdjustmentMagnitude != nil {
d.Set("min_adjustment_magnitude", p.MinAdjustmentMagnitude)
}

d.Set("name", p.PolicyName)
d.Set("scaling_adjustment", p.ScalingAdjustment)
if err := d.Set("predictive_scaling_configuration", flattenPredictiveScalingConfig(p.PredictiveScalingConfiguration)); err != nil {
return fmt.Errorf("error setting predictive_scaling_configuration: %s", err)
Expand Down Expand Up @@ -576,6 +582,7 @@ func FindScalingPolicy(conn *autoscaling.AutoScaling, asgName, policyName string
func getPutScalingPolicyInput(d *schema.ResourceData) (*autoscaling.PutScalingPolicyInput, error) {
var params = &autoscaling.PutScalingPolicyInput{
AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
Enabled: aws.Bool(d.Get("enabled").(bool)),
PolicyName: aws.String(d.Get("name").(string)),
}

Expand Down
33 changes: 23 additions & 10 deletions internal/service/autoscaling/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,30 @@ func TestAccAutoScalingPolicy_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckScalingPolicyExists(resourceSimpleName, &v),
resource.TestCheckResourceAttr(resourceSimpleName, "adjustment_type", "ChangeInCapacity"),
resource.TestCheckResourceAttr(resourceSimpleName, "policy_type", "SimpleScaling"),
resource.TestCheckResourceAttr(resourceSimpleName, "autoscaling_group_name", rName),
resource.TestCheckResourceAttr(resourceSimpleName, "cooldown", "300"),
resource.TestCheckResourceAttr(resourceSimpleName, "enabled", "true"),
resource.TestCheckResourceAttr(resourceSimpleName, "name", rName+"-simple"),
resource.TestCheckResourceAttr(resourceSimpleName, "policy_type", "SimpleScaling"),
resource.TestCheckResourceAttr(resourceSimpleName, "scaling_adjustment", "2"),
resource.TestCheckResourceAttr(resourceSimpleName, "autoscaling_group_name", rName),

testAccCheckScalingPolicyExists(resourceStepName, &v),
resource.TestCheckResourceAttr(resourceStepName, "adjustment_type", "ChangeInCapacity"),
resource.TestCheckResourceAttr(resourceStepName, "policy_type", "StepScaling"),
resource.TestCheckResourceAttr(resourceStepName, "name", rName+"-step"),
resource.TestCheckResourceAttr(resourceStepName, "metric_aggregation_type", "Minimum"),
resource.TestCheckResourceAttr(resourceStepName, "estimated_instance_warmup", "200"),
resource.TestCheckResourceAttr(resourceStepName, "autoscaling_group_name", rName),
resource.TestCheckResourceAttr(resourceStepName, "enabled", "false"),
resource.TestCheckResourceAttr(resourceStepName, "estimated_instance_warmup", "200"),
resource.TestCheckResourceAttr(resourceStepName, "metric_aggregation_type", "Minimum"),
resource.TestCheckResourceAttr(resourceStepName, "name", rName+"-step"),
resource.TestCheckResourceAttr(resourceStepName, "policy_type", "StepScaling"),
resource.TestCheckTypeSetElemNestedAttrs(resourceStepName, "step_adjustment.*", map[string]string{
"scaling_adjustment": "1",
}),

testAccCheckScalingPolicyExists(resourceTargetTrackingName, &v),
resource.TestCheckResourceAttr(resourceTargetTrackingName, "policy_type", "TargetTrackingScaling"),
resource.TestCheckResourceAttr(resourceTargetTrackingName, "name", rName+"-tracking"),
resource.TestCheckResourceAttr(resourceTargetTrackingName, "autoscaling_group_name", rName),
resource.TestCheckResourceAttr(resourceTargetTrackingName, "enabled", "true"),
resource.TestCheckResourceAttr(resourceTargetTrackingName, "name", rName+"-tracking"),
resource.TestCheckResourceAttr(resourceTargetTrackingName, "policy_type", "TargetTrackingScaling"),
resource.TestCheckResourceAttr(resourceTargetTrackingName, "target_tracking_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceTargetTrackingName, "target_tracking_configuration.0.customized_metric_specification.#", "0"),
resource.TestCheckResourceAttr(resourceTargetTrackingName, "target_tracking_configuration.0.predefined_metric_specification.#", "1"),
Expand Down Expand Up @@ -81,15 +85,20 @@ func TestAccAutoScalingPolicy_basic(t *testing.T) {
Config: testAccPolicyConfig_basicUpdate(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckScalingPolicyExists(resourceSimpleName, &v),
resource.TestCheckResourceAttr(resourceSimpleName, "policy_type", "SimpleScaling"),
resource.TestCheckResourceAttr(resourceSimpleName, "cooldown", "30"),
resource.TestCheckResourceAttr(resourceSimpleName, "enabled", "true"),
resource.TestCheckResourceAttr(resourceSimpleName, "policy_type", "SimpleScaling"),

testAccCheckScalingPolicyExists(resourceStepName, &v),
resource.TestCheckResourceAttr(resourceStepName, "policy_type", "StepScaling"),
resource.TestCheckResourceAttr(resourceStepName, "enabled", "true"),
resource.TestCheckResourceAttr(resourceStepName, "estimated_instance_warmup", "20"),
resource.TestCheckResourceAttr(resourceStepName, "policy_type", "StepScaling"),
resource.TestCheckTypeSetElemNestedAttrs(resourceStepName, "step_adjustment.*", map[string]string{
"scaling_adjustment": "10",
}),

testAccCheckScalingPolicyExists(resourceTargetTrackingName, &v),
resource.TestCheckResourceAttr(resourceTargetTrackingName, "enabled", "false"),
resource.TestCheckResourceAttr(resourceTargetTrackingName, "policy_type", "TargetTrackingScaling"),
resource.TestCheckResourceAttr(resourceTargetTrackingName, "target_tracking_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceTargetTrackingName, "target_tracking_configuration.0.customized_metric_specification.#", "1"),
Expand Down Expand Up @@ -520,6 +529,7 @@ resource "aws_autoscaling_policy" "test_step" {
policy_type = "StepScaling"
estimated_instance_warmup = 200
metric_aggregation_type = "Minimum"
enabled = false

step_adjustment {
scaling_adjustment = 1
Expand All @@ -533,6 +543,7 @@ resource "aws_autoscaling_policy" "test_tracking" {
name = "%[1]s-tracking"
policy_type = "TargetTrackingScaling"
autoscaling_group_name = aws_autoscaling_group.test.name
enabled = true

target_tracking_configuration {
predefined_metric_specification {
Expand Down Expand Up @@ -697,6 +708,7 @@ resource "aws_autoscaling_policy" "test_step" {
policy_type = "StepScaling"
estimated_instance_warmup = 20
metric_aggregation_type = "Minimum"
enabled = true

step_adjustment {
scaling_adjustment = 10
Expand All @@ -710,6 +722,7 @@ resource "aws_autoscaling_policy" "test_tracking" {
name = "%[1]s-tracking"
policy_type = "TargetTrackingScaling"
autoscaling_group_name = aws_autoscaling_group.test.name
enabled = false

target_tracking_configuration {
customized_metric_specification {
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/autoscaling_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ resource "aws_autoscaling_policy" "example" {
* `policy_type` - (Optional) The policy type, either "SimpleScaling", "StepScaling", "TargetTrackingScaling", or "PredictiveScaling". If this value isn't provided, AWS will default to "SimpleScaling."
* `predictive_scaling_configuration` - (Optional) The predictive scaling policy configuration to use with Amazon EC2 Auto Scaling.
* `estimated_instance_warmup` - (Optional) The estimated time, in seconds, until a newly launched instance will contribute CloudWatch metrics. Without a value, AWS will default to the group's specified cooldown period.
* `enabled` - (Optional) Indicates whether the scaling policy is enabled or disabled. Default: `true`.

The following argument is only available to "SimpleScaling" and "StepScaling" type policies:

Expand Down