Resource: aws_autoscaling_policy

Provides an AutoScaling Scaling Policy resource.

Hands-on: Try the Manage AWS Auto Scaling Groups tutorial on HashiCorp Learn.

Example Usage

resource "aws_autoscaling_policy" "bat" {
  name                   = "foobar3-terraform-test"
  scaling_adjustment     = 4
  adjustment_type        = "ChangeInCapacity"
  cooldown               = 300
  autoscaling_group_name = aws_autoscaling_group.bar.name
}

resource "aws_autoscaling_group" "bar" {
  availability_zones        = ["us-east-1a"]
  name                      = "foobar3-terraform-test"
  max_size                  = 5
  min_size                  = 2
  health_check_grace_period = 300
  health_check_type         = "ELB"
  force_delete              = true
  launch_configuration      = aws_launch_configuration.foo.name
}

Create target tracking scaling policy using metric math

resource "aws_autoscaling_policy" "example" {
  autoscaling_group_name = "my-test-asg"
  name                   = "foo"
  policy_type            = "TargetTrackingScaling"
  target_tracking_configuration {
    target_value = 100
    customized_metric_specification {
      metrics {
        label = "Get the queue size (the number of messages waiting to be processed)"
        id    = "m1"
        metric_stat {
          metric {
            namespace   = "AWS/SQS"
            metric_name = "ApproximateNumberOfMessagesVisible"
            dimensions {
              name  = "QueueName"
              value = "my-queue"
            }
          }
          stat = "Sum"
        }
        return_data = false
      }
      metrics {
        label = "Get the group size (the number of InService instances)"
        id    = "m2"
        metric_stat {
          metric {
            namespace   = "AWS/AutoScaling"
            metric_name = "GroupInServiceInstances"
            dimensions {
              name  = "AutoScalingGroupName"
              value = "my-asg"
            }
          }
          stat = "Average"
        }
        return_data = false
      }
      metrics {
        label       = "Calculate the backlog per instance"
        id          = "e1"
        expression  = "m1 / m2"
        return_data = true
      }
    }
  }
}

Create predictive scaling policy using customized metrics

resource "aws_autoscaling_policy" "example" {
  autoscaling_group_name = "my-test-asg"
  name                   = "foo"
  policy_type            = "PredictiveScaling"
  predictive_scaling_configuration {
    metric_specification {
      target_value = 10
      customized_load_metric_specification {
        metric_data_queries {
          id         = "load_sum"
          expression = "SUM(SEARCH('{AWS/EC2,AutoScalingGroupName} MetricName=\"CPUUtilization\" my-test-asg', 'Sum', 3600))"
        }
      }
      customized_capacity_metric_specification {
        metric_data_queries {
          id         = "capacity_sum"
          expression = "SUM(SEARCH('{AWS/AutoScaling,AutoScalingGroupName} MetricName=\"GroupInServiceIntances\" my-test-asg', 'Average', 300))"
        }
      }
      customized_scaling_metric_specification {
        metric_data_queries {
          id          = "capacity_sum"
          expression  = "SUM(SEARCH('{AWS/AutoScaling,AutoScalingGroupName} MetricName=\"GroupInServiceIntances\" my-test-asg', 'Average', 300))"
          return_data = false
        }
        metric_data_queries {
          id          = "load_sum"
          expression  = "SUM(SEARCH('{AWS/EC2,AutoScalingGroupName} MetricName=\"CPUUtilization\" my-test-asg', 'Sum', 300))"
          return_data = false
        }
        metric_data_queries {
          id         = "weighted_average"
          expression = "load_sum / (capacity_sum * PERIOD(capacity_sum) / 60)"
        }
      }
    }
  }
}

Create predictive scaling policy using customized scaling and predefined load metric

resource "aws_autoscaling_policy" "example" {
  autoscaling_group_name = "my-test-asg"
  name                   = "foo"
  policy_type            = "PredictiveScaling"
  predictive_scaling_configuration {
    metric_specification {
      target_value = 10
      predefined_load_metric_specification {
        predefined_metric_type = "ASGTotalCPUUtilization"
        resource_label         = "app/my-alb/778d41231b141a0f/targetgroup/my-alb-target-group/943f017f100becff"
      }
      customized_scaling_metric_specification {
        metric_data_queries {
          id = "scaling"
          metric_stat {
            metric {
              metric_name = "CPUUtilization"
              namespace   = "AWS/EC2"
              dimensions {
                name  = "AutoScalingGroupName"
                value = "my-test-asg"
              }
            }
            stat = "Average"
          }
        }
      }
    }
  }
}

Argument Reference

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

The following arguments are only available to "SimpleScaling" type policies:

The following arguments are only available to "StepScaling" type policies:

resource "aws_autoscaling_policy" "example" {
  # ... other configuration ...

  step_adjustment {
    scaling_adjustment          = -1
    metric_interval_lower_bound = 1.0
    metric_interval_upper_bound = 2.0
  }

  step_adjustment {
    scaling_adjustment          = 1
    metric_interval_lower_bound = 2.0
    metric_interval_upper_bound = 3.0
  }
}

The following fields are available in step adjustments:

Notice the bounds are relative to the alarm threshold, meaning that the starting point is not 0%, but the alarm threshold. Check the official docs for a detailed example.

The following arguments are only available to "TargetTrackingScaling" type policies:

resource "aws_autoscaling_policy" "example" {
  # ... other configuration ...

  target_tracking_configuration {
    predefined_metric_specification {
      predefined_metric_type = "ASGAverageCPUUtilization"
    }

    target_value = 40.0
  }
}

The following fields are available in target tracking configuration:

predefined_metric_specification

This argument supports the following arguments:

customized_metric_specification

This argument supports the following arguments:

metric_dimension

This argument supports the following arguments:

metrics

This argument supports the following arguments:

metric_stat

This argument supports the following arguments:

metric

This argument supports the following arguments:

dimensions

This argument supports the following arguments:

predictive_scaling_configuration

This argument supports the following arguments:

metric_specification

This argument supports the following arguments:

predefined_load_metric_specification

This argument supports the following arguments:

predefined_metric_pair_specification

This argument supports the following arguments:

predefined_scaling_metric_specification

This argument supports the following arguments:

customized_scaling_metric_specification

This argument supports the following arguments:

customized_load_metric_specification

This argument supports the following arguments:

customized_capacity_metric_specification

This argument supports the following arguments:

metric_data_queries

This argument supports the following arguments:

metric_stat

This argument supports the following arguments:

metric

This argument supports the following arguments:

dimensions

This argument supports the following arguments:

Attribute Reference

This resource exports the following attributes in addition to the arguments above:

Import

In Terraform v1.5.0 and later, use an import block to import AutoScaling scaling policy using the role autoscaling_group_name and name separated by /. For example:

import {
  to = aws_autoscaling_policy.test-policy
  id = "asg-name/policy-name"
}

Using terraform import, import AutoScaling scaling policy using the role autoscaling_group_name and name separated by /. For example:

% terraform import aws_autoscaling_policy.test-policy asg-name/policy-name