Skip to content

Commit 88df4a1

Browse files
authored
Capacity provider feature (#22)
Signed-off-by: Arthur Diniz <[email protected]>
1 parent 4064da7 commit 88df4a1

File tree

6 files changed

+71
-62
lines changed

6 files changed

+71
-62
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
name: Minimum version check
5252
runs-on: ubuntu-latest
5353
container:
54-
image: hashicorp/terraform:0.12.0
54+
image: hashicorp/terraform:0.13.0
5555
steps:
5656
- uses: actions/checkout@master
5757
- name: Validate Code

_variables.tf

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# == REQUIRED VARS
22

33
variable "name" {
4-
description = "Name of this ECS cluster"
4+
description = "Name of this ECS cluster."
55
}
66

77
variable "instance_type_1" {
8-
description = "Instance type for ECS workers (first priority)"
8+
description = "Instance type for ECS workers (first priority)."
99
}
1010

1111
variable "instance_type_2" {
12-
description = "Instance type for ECS workers (second priority)"
12+
description = "Instance type for ECS workers (second priority)."
1313
}
1414

1515
variable "instance_type_3" {
16-
description = "Instance type for ECS workers (third priority)"
16+
description = "Instance type for ECS workers (third priority)."
1717
}
1818

1919
variable "architecture" {
@@ -22,7 +22,7 @@ variable "architecture" {
2222
}
2323

2424
variable "on_demand_percentage" {
25-
description = "Percentage of on-demand intances vs spot"
25+
description = "Percentage of on-demand intances vs spot."
2626
default = 100
2727
}
2828

@@ -32,22 +32,22 @@ variable "on_demand_base_capacity" {
3232
}
3333

3434
variable "vpc_id" {
35-
description = "VPC ID to deploy the ECS cluster"
35+
description = "VPC ID to deploy the ECS cluster."
3636
}
3737

3838
variable "private_subnet_ids" {
3939
type = list(string)
40-
description = "List of private subnet IDs for ECS instances and Internal ALB when enabled"
40+
description = "List of private subnet IDs for ECS instances and Internal ALB when enabled."
4141
}
4242

4343
variable "public_subnet_ids" {
4444
type = list(string)
45-
description = "List of public subnet IDs for ECS ALB"
45+
description = "List of public subnet IDs for ECS ALB."
4646
}
4747

4848
variable "secure_subnet_ids" {
4949
type = list(string)
50-
description = "List of secure subnet IDs for EFS"
50+
description = "List of secure subnet IDs for EFS."
5151
}
5252

5353
variable "certificate_arn" {}
@@ -57,27 +57,27 @@ variable "certificate_arn" {}
5757
variable "security_group_ids" {
5858
type = list(string)
5959
default = []
60-
description = "Extra security groups for instances"
60+
description = "Extra security groups for instances."
6161
}
6262

6363
variable "userdata" {
6464
default = ""
65-
description = "Extra commands to pass to userdata"
65+
description = "Extra commands to pass to userdata."
6666
}
6767

6868
variable "alb" {
6969
default = true
70-
description = "Whether to deploy an ALB or not with the cluster"
70+
description = "Whether to deploy an ALB or not with the cluster."
7171
}
7272

7373
variable "alb_only" {
7474
default = false
75-
description = "Whether to deploy only an alb and no cloudFront or not with the cluster"
75+
description = "Whether to deploy only an alb and no cloudFront or not with the cluster."
7676
}
7777

7878
variable "alb_internal" {
7979
default = false
80-
description = "Deploys a second internal ALB for private APIs"
80+
description = "Deploys a second internal ALB for private APIs."
8181
}
8282

8383
variable "certificate_internal_arn" {
@@ -93,129 +93,139 @@ variable "alb_ssl_policy" {
9393

9494
variable "asg_min" {
9595
default = 1
96-
description = "Min number of instances for autoscaling group"
96+
description = "Min number of instances for autoscaling group."
9797
}
9898

9999
variable "asg_max" {
100100
default = 4
101-
description = "Max number of instances for autoscaling group"
101+
description = "Max number of instances for autoscaling group."
102102
}
103103

104-
variable "asg_memory_target" {
105-
default = 60
106-
description = "Target average memory percentage to track for autoscaling"
104+
variable "asg_protect_from_scale_in" {
105+
default = false
106+
description = "(Optional) Allows setting instance protection. The autoscaling group will not select instances with this setting for termination during scale in events."
107+
}
108+
109+
variable "asg_target_capacity" {
110+
default = 70
111+
description = "Target average capacity percentage for the ECS capacity provider to track for autoscaling."
107112
}
108113

109114
variable "alarm_sns_topics" {
110115
default = []
111-
description = "Alarm topics to create and alert on ECS instance metrics"
116+
description = "Alarm topics to create and alert on ECS instance metrics."
112117
}
113118

114119
variable "alarm_asg_high_cpu_threshold" {
115-
description = "Max threshold average CPU percentage allowed in a 2 minutes interval (use 0 to disable this alarm)"
120+
description = "Max threshold average CPU percentage allowed in a 2 minutes interval (use 0 to disable this alarm)."
116121
default = 80
117122
}
118123

119124
variable "alarm_ecs_high_memory_threshold" {
120-
description = "Max threshold average Memory percentage allowed in a 2 minutes interval (use 0 to disable this alarm)"
125+
description = "Max threshold average Memory percentage allowed in a 2 minutes interval (use 0 to disable this alarm)."
121126
default = 80
122127
}
123128

124129
variable "alarm_ecs_high_cpu_threshold" {
125-
description = "Max threshold average CPU percentage allowed in a 2 minutes interval (use 0 to disable this alarm)"
130+
description = "Max threshold average CPU percentage allowed in a 2 minutes interval (use 0 to disable this alarm)."
126131
default = 80
127132
}
128133

129134
variable "alarm_alb_latency_anomaly_threshold" {
130-
description = "ALB Latency anomaly detection width (use 0 to disable this alarm)"
135+
description = "ALB Latency anomaly detection width (use 0 to disable this alarm)."
131136
default = 2
132137
}
133138

134139
variable "alarm_alb_500_errors_threshold" {
135-
description = "Max threshold of HTTP 500 errors allowed in a 5 minutes interval (use 0 to disable this alarm)"
140+
description = "Max threshold of HTTP 500 errors allowed in a 5 minutes interval (use 0 to disable this alarm)."
136141
default = 10
137142
}
138143

139144
variable "alarm_alb_400_errors_threshold" {
140-
description = "Max threshold of HTTP 4000 errors allowed in a 5 minutes interval (use 0 to disable this alarm)"
145+
description = "Max threshold of HTTP 4000 errors allowed in a 5 minutes interval (use 0 to disable this alarm)."
141146
default = 10
142147
}
143148

144149
variable "alarm_efs_credits_low_threshold" {
145-
description = "Alerts when EFS credits fell below this number in bytes - default 1000000000000 is 1TB of a maximum of 2.31T of credits (use 0 to disable this alarm)"
150+
description = "Alerts when EFS credits fell below this number in bytes - default 1000000000000 is 1TB of a maximum of 2.31T of credits (use 0 to disable this alarm)."
146151
default = 1000000000000
147152
}
148153

149154
variable "target_group_arns" {
150155
default = []
151156
type = list(string)
152-
description = "List of target groups for ASG to register"
157+
description = "List of target groups for ASG to register."
153158
}
154159

155160
variable "autoscaling_health_check_grace_period" {
156161
default = 300
157-
description = "The length of time that Auto Scaling waits before checking an instance's health status. The grace period begins when an instance comes into service"
162+
description = "The length of time that Auto Scaling waits before checking an instance's health status. The grace period begins when an instance comes into service."
158163
}
159164

160165
variable "autoscaling_default_cooldown" {
161166
default = 300
162-
description = "The amount of time, in seconds, after a scaling activity completes before another scaling activity can start"
167+
description = "The amount of time, in seconds, after a scaling activity completes before another scaling activity can start."
163168
}
164169

165170
variable "instance_volume_size" {
166-
description = "Volume size for docker volume (in GB)"
167-
default = 30
171+
description = "Volume size for docker volume (in GB)."
172+
default = 22
173+
}
174+
175+
variable "instance_volume_size_root" {
176+
description = "Volume size for root volume (in GB)."
177+
default = 16
168178
}
169179

170180
variable "lb_access_logs_bucket" {
171181
type = string
172182
default = ""
173-
description = "Bucket to store logs from lb access"
183+
description = "Bucket to store logs from lb access."
174184
}
175185

176186
variable "lb_access_logs_prefix" {
177187
type = string
178188
default = ""
179-
description = "Bucket prefix to store lb access logs"
189+
description = "Bucket prefix to store lb access logs."
180190
}
181191

182192
variable "enable_schedule" {
183193
default = false
184-
description = "Enables schedule to shut down and start up instances outside business hours"
194+
description = "Enables schedule to shut down and start up instances outside business hours."
185195
}
186196
variable "schedule_cron_start" {
187197
type = string
188198
default = ""
189-
description = "Cron expression to define when to trigger a start of the auto-scaling group. E.g. '0 20 * * *' to start at 8pm GMT time"
199+
description = "Cron expression to define when to trigger a start of the auto-scaling group. E.g. '0 20 * * *' to start at 8pm GMT time."
190200
}
191201

192202
variable "schedule_cron_stop" {
193203
type = string
194204
default = ""
195-
description = "Cron expression to define when to trigger a stop of the auto-scaling group. E.g. '0 10 * * *' to stop at 10am GMT time"
205+
description = "Cron expression to define when to trigger a stop of the auto-scaling group. E.g. '0 10 * * *' to stop at 10am GMT time."
196206
}
197207

198208
variable "backup" {
199209
type = string
200210
default = "true"
201-
description = "Assing a backup tag to efs resource - Backup will be performed by AWS Backup"
211+
description = "Assing a backup tag to efs resource - Backup will be performed by AWS Backup."
202212
}
203213

204214
variable "throughput_mode" {
205215
type = string
206216
default = "bursting"
207-
description = "Throughput mode for the file system. Defaults to bursting. Valid values: bursting, provisioned"
217+
description = "Throughput mode for the file system. Defaults to bursting. Valid values: bursting, provisioned."
208218
}
209219

210220
variable "provisioned_throughput_in_mibps" {
211221
default = 0
212-
description = "The throughput, measured in MiB/s, that you want to provision for the file system"
222+
description = "The throughput, measured in MiB/s, that you want to provision for the file system."
213223
}
214224

215225
variable "alarm_prefix" {
216226
type = string
217227
description = "String prefix for cloudwatch alarms. (Optional)"
218-
default = ""
228+
default = "alarm"
219229
}
220230

221231
variable "kms_key_arn" {

asg.tf

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ resource "aws_autoscaling_group" "ecs" {
3333
min_size = var.asg_min
3434
max_size = var.asg_max
3535

36+
protect_from_scale_in = var.asg_protect_from_scale_in
37+
3638
tags = [
3739
map("key", "Name", "value", "ecs-node-${var.name}", "propagate_at_launch", true)
3840
]
@@ -45,25 +47,18 @@ resource "aws_autoscaling_group" "ecs" {
4547
}
4648
}
4749

48-
resource "aws_autoscaling_policy" "ecs_memory_tracking" {
49-
name = "ecs-${var.name}-memory"
50-
policy_type = "TargetTrackingScaling"
51-
autoscaling_group_name = aws_autoscaling_group.ecs.name
52-
estimated_instance_warmup = "180"
50+
resource "aws_ecs_capacity_provider" "ecs_capacity_provider" {
51+
name = "${var.name}-capacity-provider"
5352

54-
target_tracking_configuration {
55-
customized_metric_specification {
56-
metric_dimension {
57-
name = "ClusterName"
58-
value = aws_ecs_cluster.ecs.name
59-
}
53+
auto_scaling_group_provider {
54+
auto_scaling_group_arn = aws_autoscaling_group.ecs.arn
55+
managed_termination_protection = "DISABLED"
6056

61-
metric_name = "MemoryReservation"
62-
namespace = "AWS/ECS"
63-
statistic = "Average"
64-
unit = "Percent"
57+
managed_scaling {
58+
maximum_scaling_step_size = 10
59+
minimum_scaling_step_size = 1
60+
status = "ENABLED"
61+
target_capacity = var.asg_target_capacity
6562
}
66-
67-
target_value = var.asg_memory_target
6863
}
6964
}

ec2-launch-template.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ data "template_file" "userdata" {
22
template = file("${path.module}/userdata.tpl")
33

44
vars = {
5-
tf_cluster_name = aws_ecs_cluster.ecs.name
5+
tf_cluster_name = var.name
66
tf_efs_id = aws_efs_file_system.ecs.id
77
userdata_extra = var.userdata
88
}

ecs.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
resource "aws_ecs_cluster" "ecs" {
2-
name = var.name
2+
3+
depends_on = [aws_autoscaling_group.ecs, aws_ecs_capacity_provider.ecs_capacity_provider]
4+
name = var.name
5+
6+
capacity_providers = ["${var.name}-capacity-provider"]
37

48
lifecycle {
59
ignore_changes = [

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
terraform {
2-
required_version = ">= 0.12.0"
2+
required_version = ">= 0.13.0"
33
}

0 commit comments

Comments
 (0)