|
| 1 | +resource "aws_autoscaling_group" "ecs" { |
| 2 | + name = "ecs-${var.name}" |
| 3 | + |
| 4 | + mixed_instances_policy { |
| 5 | + launch_template { |
| 6 | + launch_template_specification { |
| 7 | + launch_template_id = "${aws_launch_template.ecs.id}" |
| 8 | + version = "$$Latest" |
| 9 | + } |
| 10 | + |
| 11 | + override { |
| 12 | + instance_type = "${var.instance_type_1}" |
| 13 | + } |
| 14 | + |
| 15 | + override { |
| 16 | + instance_type = "${var.instance_type_2}" |
| 17 | + } |
| 18 | + |
| 19 | + override { |
| 20 | + instance_type = "${var.instance_type_3}" |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + instances_distribution { |
| 25 | + spot_instance_pools = 3 |
| 26 | + on_demand_base_capacity = 0 |
| 27 | + on_demand_percentage_above_base_capacity = "${var.on_demand_percentage}" |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + vpc_zone_identifier = ["${var.private_subnet_ids}"] |
| 32 | + |
| 33 | + min_size = "${var.asg_min}" |
| 34 | + max_size = "${var.asg_max}" |
| 35 | + |
| 36 | + tags = [ |
| 37 | + "${map("key", "Name", "value", "ecs-node-${var.name}", "propagate_at_launch", true)}", |
| 38 | + ] |
| 39 | + |
| 40 | + lifecycle { |
| 41 | + create_before_destroy = true |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +resource "aws_autoscaling_policy" "ecs_memory_tracking" { |
| 46 | + name = "ecs-${var.name}-memory" |
| 47 | + adjustment_type = "ChangeInCapacity" |
| 48 | + policy_type = "TargetTrackingScaling" |
| 49 | + autoscaling_group_name = "${aws_autoscaling_group.ecs.name}" |
| 50 | + estimated_instance_warmup = "180" |
| 51 | + |
| 52 | + target_tracking_configuration { |
| 53 | + customized_metric_specification { |
| 54 | + metric_dimension { |
| 55 | + name = "ClusterName" |
| 56 | + value = "${aws_autoscaling_group.ecs.name}" |
| 57 | + } |
| 58 | + |
| 59 | + metric_name = "MemoryReservation" |
| 60 | + namespace = "AWS/ECS" |
| 61 | + statistic = "Average" |
| 62 | + } |
| 63 | + |
| 64 | + target_value = "${var.asg_memory_target}" |
| 65 | + } |
| 66 | +} |
0 commit comments