This template configures a basic dynamic scaling plan for an AWS Auto Scaling group, optimizing for cost by adjusting capacity based on CPU utilization.

Terraform Template

data "aws_availability_zones" "available" {
}

resource "aws_autoscaling_group" "example" {
  availability_zones = [data.aws_availability_zones.available.names[0]]
  launch_configuration = "aws_launch_configuration.example.name"
  max_size = 3
  min_size = 0
  name_prefix = "example"
  name = ""
}

resource "aws_autoscalingplans_scaling_plan" "example" {

  application_source {

    tag_filter {
      key = "application"
      values = ["example"]
    }
  }
  name = "example-dynamic-cost-optimization"

  scaling_instruction {
    max_capacity = 3
    min_capacity = 0
    resource_id = format("autoScalingGroup/%s", aws_autoscaling_group.example.name)
    scalable_dimension = "autoscaling:autoScalingGroup:DesiredCapacity"
    service_namespace = "autoscaling"

    target_tracking_configuration {

      predefined_scaling_metric_specification {
        predefined_scaling_metric_type = "ASGAverageCPUUtilization"
      }
      target_value = 70
    }
  }
}