-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.tf
215 lines (190 loc) · 7.37 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
## ec2 autoscaling groups with systems manager/session manager
## features
locals {
node_groups_enabled = (var.node_groups != null ? ((length(var.node_groups) > 0) ? true : false) : false)
}
## autoscaling groups (asg)
# security/policy
resource "aws_iam_role" "ng" {
for_each = { for ng in var.node_groups : ng.name => ng }
name = join("-", [local.name, "ng", each.key])
tags = merge(local.default-tags, var.tags)
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = [format("ec2.%s", module.aws.partition.dns_suffix)]
}
}]
})
}
resource "aws_iam_role_policy_attachment" "ng-ssm" {
for_each = { for ng in var.node_groups : ng.name => ng }
policy_arn = format("arn:%s:iam::aws:policy/AmazonSSMManagedInstanceCore", module.aws.partition.partition)
role = aws_iam_role.ng[each.key].id
}
resource "aws_iam_role_policy_attachment" "ng" {
for_each = { for k, v in [for p in chunklist(flatten(
[
for k, v in var.node_groups : setproduct([v.name], v.policy_arns)
if(length(lookup(v, "policy_arns", [])) > 0)
]), 2) :
{
role = p[0]
policy = p[1]
}
] : k => v }
policy_arn = each.value.policy
role = aws_iam_role.ng[each.value.role].id
}
resource "aws_iam_instance_profile" "ng" {
for_each = { for ng in var.node_groups : ng.name => ng }
name = aws_iam_role.ng[each.key].name
role = aws_iam_role.ng[each.key].id
}
## amazon-linux 2
data "aws_ami" "al2" {
for_each = { for ng in var.node_groups : ng.name => ng }
owners = ["amazon"]
most_recent = true
filter {
name = "name"
values = [format("amzn2-ami-hvm-*")]
}
filter {
name = "block-device-mapping.volume-type"
values = [lookup(each.value, "volume_type", "gp2")]
}
filter {
name = "architecture"
values = [length(regexall("ARM", lookup(each.value, "ami_type", "AL2_x86_64"))) > 0 ? "arm64" : "x86_64"]
}
}
data "cloudinit_config" "ng" {
for_each = { for ng in var.node_groups : ng.name => ng }
base64_encode = true
gzip = false
part {
content_type = "text/x-shellscript"
content = <<-EOT
#!/bin/bash
yum update -y
yum install -y amazon-cloudwatch-agent
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s
EOT
}
part {
content_type = "text/x-shellscript"
content = lookup(each.value, "user_data", "")
}
}
resource "aws_launch_template" "ng" {
for_each = { for ng in var.node_groups : ng.name => ng }
name = join("-", [local.name, each.key])
tags = merge(local.default-tags, var.tags, lookup(each.value, "tags", {}))
image_id = lookup(each.value, "image_id", data.aws_ami.al2[each.key].id)
user_data = data.cloudinit_config.ng[each.key].rendered
instance_type = lookup(each.value, "instance_type", local.default_ec2["instance_type"])
key_name = lookup(each.value, "key_name", local.default_ec2["keypair"])
iam_instance_profile {
arn = aws_iam_instance_profile.ng[each.key].arn
}
block_device_mappings {
device_name = "/dev/xvda"
ebs {
volume_size = lookup(each.value, "disk_size", local.default_ec2["volume_size"])
volume_type = lookup(each.value, "volume_type", local.default_ec2["volume_type"])
delete_on_termination = true
}
}
network_interfaces {
security_groups = lookup(each.value, "security_groups", local.default_ec2["security_groups"])
delete_on_termination = true
}
tag_specifications {
resource_type = "instance"
tags = merge(local.default-tags, var.tags, lookup(each.value, "tags", {}))
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "ng" {
for_each = { for ng in var.node_groups : ng.name => ng }
name = join("-", [local.name, each.key])
vpc_zone_identifier = var.subnets
max_size = lookup(each.value, "max_size", local.default_asg["max_size"])
min_size = lookup(each.value, "min_size", local.default_asg["min_size"])
desired_capacity = lookup(each.value, "desired_size", local.default_asg["desired_size"])
target_group_arns = lookup(each.value, "target_group_arns", local.default_asg["target_group_arns"])
force_delete = true
protect_from_scale_in = false
termination_policies = ["Default"]
enabled_metrics = [
"GroupPendingInstances", "GroupStandbyInstances", "GroupInServiceInstances", "GroupMinSize",
"GroupTerminatingInstances", "GroupDesiredCapacity", "GroupTotalInstances", "GroupMaxSize",
]
dynamic "mixed_instances_policy" {
for_each = (length(lookup(each.value, "warm_pool", [])) == 0 ? [each.value] : [])
content {
launch_template {
launch_template_specification {
launch_template_id = aws_launch_template.ng[mixed_instances_policy.value.name].id
version = aws_launch_template.ng[mixed_instances_policy.value.name].latest_version
}
dynamic "override" {
for_each = lookup(mixed_instances_policy.value, "instances_override", [])
content {
instance_type = lookup(override.value, "instance_type", null)
weighted_capacity = lookup(override.value, "weighted_capacity", null)
}
}
}
dynamic "instances_distribution" {
for_each = { for k, v in mixed_instances_policy.value : k => v if k == "instances_distribution" }
content {
on_demand_allocation_strategy = lookup(instances_distribution.value, "on_demand_allocation_strategy", null)
on_demand_base_capacity = lookup(instances_distribution.value, "on_demand_base_capacity", null)
on_demand_percentage_above_base_capacity = lookup(instances_distribution.value, "on_demand_percentage_above_base_capacity", null)
spot_allocation_strategy = lookup(instances_distribution.value, "spot_allocation_strategy", null)
spot_instance_pools = lookup(instances_distribution.value, "spot_instance_pools", null)
spot_max_price = lookup(instances_distribution.value, "spot_max_price", null)
}
}
}
}
dynamic "launch_template" {
for_each = (length(lookup(each.value, "warm_pool", [])) > 0 ? [each.value] : [])
content {
id = aws_launch_template.ng[launch_template.value.name].id
version = aws_launch_template.ng[launch_template.value.name].latest_version
}
}
dynamic "warm_pool" {
for_each = flatten([lookup(each.value, "warm_pool", [])])
content {
pool_state = lookup(warm_pool.value, "pool_state", "Stopped")
min_size = lookup(warm_pool.value, "min_pool_size", 0)
max_group_prepared_capacity = lookup(warm_pool.value, "max_group_prepared_capacity", 0)
}
}
# name tag must be applied last
dynamic "tag" {
for_each = merge(
local.default-tags, var.tags,
lookup(each.value, "tags", {}),
{ "Name" = join("-", [local.name, each.key]) }
)
content {
key = tag.key
value = tag.value
propagate_at_launch = true
}
}
lifecycle {
create_before_destroy = true
}
depends_on = [aws_launch_template.ng]
}