-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.tf
159 lines (123 loc) · 4.56 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
resource "aws_launch_template" "LT" {
count = var.instances_desired > 0 || var.instances_autoscale_max > 0 ? 1 : 0
name = var.spot ? "${var.cluster_name}-spot" : var.cluster_name
dynamic "instance_market_options" {
for_each = var.spot ? [1] : []
content {
market_type = "spot"
}
}
credit_specification {
cpu_credits = var.cpu_unlimited ? "unlimited" : "standard"
}
image_id = var.ami != "" ? data.aws_ami.ami[0].id : jsondecode(data.aws_ssm_parameter.recommended_ami[0].insecure_value).image_id
instance_type = var.instance_type
vpc_security_group_ids = data.aws_security_group.group[*].id
dynamic "metadata_options" {
for_each = length(var.metadata_options) > 0 ? [1] : []
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template#metadata-options
content {
http_endpoint = lookup(var.metadata_options, "http_endpoint", "enabled")
http_tokens = lookup(var.metadata_options, "http_tokens", "optional")
http_put_response_hop_limit = lookup(var.metadata_options, "http_put_response_hop_limit", 1)
http_protocol_ipv6 = lookup(var.metadata_options, "http_protocol_ipv6", "disabled")
instance_metadata_tags = lookup(var.metadata_options, "instance_metadata_tags", "disabled")
}
}
iam_instance_profile {
arn = aws_iam_instance_profile.ec2-instance-role.arn
}
block_device_mappings {
device_name = "/dev/xvda"
ebs {
volume_size = var.disk_size
volume_type = var.disk_type
delete_on_termination = true
encrypted = var.disk_encrypted
}
}
key_name = var.ec2_key_name
user_data = data.cloudinit_config.config[0].rendered
depends_on = [aws_iam_instance_profile.ec2-instance-role]
}
resource "aws_autoscaling_group" "ASG" {
count = var.instances_desired > 0 || var.instances_autoscale_max > 0 ? 1 : 0
name = var.cluster_name
max_size = var.instances_desired > 0 ? var.instances_desired : var.instances_autoscale_max
min_size = var.instances_desired > 0 ? var.instances_desired : 0
desired_capacity = var.instances_desired
force_delete = true
protect_from_scale_in = var.instances_autoscale_max > 0 && var.instances_desired == 0 // if using autoscaling
launch_template {
id = aws_launch_template.LT[0].id
version = aws_launch_template.LT[0].latest_version
}
vpc_zone_identifier = coalescelist(var.subnet_ids, tolist(data.aws_subnets.subnets.ids))
termination_policies = ["OldestInstance"]
dynamic "instance_refresh" {
for_each = var.instance_refresh ? [1] : []
content {
strategy = "Rolling"
triggers = ["launch_template"]
}
}
tag {
key = "Name"
value = "${var.cluster_name}-ecs"
propagate_at_launch = true
}
tag {
key = "ecs-cluster"
value = var.cluster_name
propagate_at_launch = true
}
// ecs autoscaling will add it otherwise and we'll get drift
dynamic "tag" {
for_each = var.instances_autoscale_max > 0 && var.instances_desired == 0 ? [{}] : []
content {
key = "AmazonECSManaged"
value = ""
propagate_at_launch = true
}
}
dynamic "tag" {
for_each = var.tags
content {
key = tag.key
value = tag.value
propagate_at_launch = true
}
}
depends_on = [aws_iam_instance_profile.ec2-instance-role]
lifecycle {
// desired_capacity is managed by ECS
ignore_changes = [desired_capacity]
}
}
resource "aws_ecs_capacity_provider" "autoscale" {
count = var.instances_autoscale_max > 0 && var.instances_desired == 0 ? 1 : 0
name = "${var.cluster_name}-autoscale"
auto_scaling_group_provider {
auto_scaling_group_arn = aws_autoscaling_group.ASG[0].arn
managed_termination_protection = "ENABLED"
managed_scaling {
status = "ENABLED"
}
}
}
resource "aws_ecs_cluster" "main" {
name = var.cluster_name
}
resource "aws_ecs_cluster_capacity_providers" "providers" {
cluster_name = aws_ecs_cluster.main.name
capacity_providers = compact([
var.instances_autoscale_max > 0 && var.instances_desired == 0 ? aws_ecs_capacity_provider.autoscale[0].name : "",
"FARGATE",
"FARGATE_SPOT",
])
default_capacity_provider_strategy {
base = 1
weight = 100
capacity_provider = var.instances_autoscale_max > 0 && var.instances_desired == 0 ? aws_ecs_capacity_provider.autoscale[0].name : "FARGATE"
}
}