-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.tf
More file actions
384 lines (319 loc) · 11.7 KB
/
Copy pathmain.tf
File metadata and controls
384 lines (319 loc) · 11.7 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
locals {
instance_type_chars = split("", var.instance_type)
# Validate that only 'arm64' architecture is used with 'g' processor instances to ensure compatibility.
# https://docs.aws.amazon.com/ec2/latest/instancetypes/instance-type-names.html
is_instance_compatible = (
# True if does not contain 'g' in the third position when architecture is x86_64
(var.architecture == "x86_64" && element(local.instance_type_chars, 2) != "g") ||
# True if contains 'g' in the third position when architecture is arm64
(var.architecture == "arm64" && element(local.instance_type_chars, 2) == "g")
)
# Check if user_data contains architecture-specific references
user_data_has_arm64 = can(regex("arm64", var.user_data))
user_data_has_amd64 = can(regex("amd64", var.user_data))
# Full compatibility: instance type + architecture + user_data script must all align
is_fully_compatible = local.is_instance_compatible && (
# If user_data references arm64, architecture must be arm64
(!local.user_data_has_arm64 || var.architecture == "arm64") &&
# If user_data references amd64, architecture must be x86_64
(!local.user_data_has_amd64 || var.architecture == "x86_64")
)
# Get the root device name from the provided/default AMI.
root_volume_device_name = (
length(var.ami) > 0 ? element(data.aws_ami.instance, 0).root_device_name : "/dev/xvda"
)
# VPC and Subnet ID resolution - names take precedence over IDs
vpc_id = var.vpc_name != null ? one(data.aws_vpc.selected[*].id) : var.vpc_id
subnet_ids = length(var.subnet_names) > 0 ? values(data.aws_subnet.selected)[*].id : var.subnet_ids
}
resource "terraform_data" "validate_configuration" {
lifecycle {
precondition {
condition = local.is_fully_compatible
error_message = <<-EOT
Configuration mismatch detected. Ensure instance_type, architecture, and user_data are aligned:
- instance_type: "${var.instance_type}"
- architecture: "${var.architecture}"
- user_data contains: ${local.user_data_has_arm64 ? "arm64" : local.user_data_has_amd64 ? "amd64" : "no architecture-specific reference"}
Update var.instance_type, var.architecture, and/or var.user_data to fix. See variable descriptions for valid combinations.
EOT
}
}
}
module "role_label" {
source = "cloudposse/label/null"
version = "0.25.0"
context = module.this.context
attributes = compact(concat(["role"], var.attributes))
}
module "logs_label" {
source = "cloudposse/label/null"
version = "0.25.0"
context = module.this.context
attributes = compact(concat(["logs"], var.attributes))
}
locals {
region = coalesce(var.region, data.aws_region.current.region)
account_id = data.aws_caller_identity.current.account_id
session_logging_bucket_name = try(coalesce(var.session_logging_bucket_name, module.logs_label.id), "")
session_logging_kms_key_arn = try(coalesce(var.session_logging_kms_key_arn, module.kms_key.key_arn), "")
session_logging_bucket_arn = var.session_logging_enabled ? "arn:aws:s3:::${local.session_logging_bucket_name}" : ""
session_logging_log_group_arn = var.session_logging_enabled ? "arn:aws:logs:${local.region}:${local.account_id}:log-group:${module.logs_label.id}" : ""
logs_bucket_enabled = var.session_logging_enabled && length(var.session_logging_bucket_name) == 0
}
#####################
## SSM AGENT ROLE ##
###################
resource "aws_iam_role" "default" {
name = module.role_label.id
assume_role_policy = data.aws_iam_policy_document.default.json
permissions_boundary = var.permissions_boundary
tags = module.role_label.tags
}
resource "aws_iam_role_policy_attachment" "default" {
role = aws_iam_role.default.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
resource "aws_iam_role_policy" "session_logging" {
count = var.session_logging_enabled ? 1 : 0
name = "${module.role_label.id}-session-logging"
role = aws_iam_role.default.name
policy = join("", data.aws_iam_policy_document.session_logging.*.json)
}
resource "aws_iam_role_policy" "custom" {
count = length(var.custom_policy_document) > 0 ? 1 : 0
name = "${module.role_label.id}-${var.custom_policy_name}"
role = aws_iam_role.default.name
policy = var.custom_policy_document
}
resource "aws_iam_instance_profile" "default" {
name = module.role_label.id
role = aws_iam_role.default.name
}
#####################
## SECURITY GROUP ##
###################
resource "aws_security_group" "default" {
vpc_id = local.vpc_id
name = module.this.id
description = "Allow ALL egress from SSM Agent."
tags = module.this.tags
}
# trivy:ignore:aws-vpc-no-public-egress-sgr SSM Agent requires broad egress to communicate with AWS services
resource "aws_security_group_rule" "allow_all_egress" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
description = "Allow all outbound traffic for SSM Agent communication with AWS services"
security_group_id = aws_security_group.default.id
}
resource "aws_security_group_rule" "additional" {
for_each = var.additional_security_group_rules
type = lookup(each.value, "type")
from_port = lookup(each.value, "from_port")
to_port = lookup(each.value, "to_port")
protocol = lookup(each.value, "protocol")
description = lookup(each.value, "description", null)
cidr_blocks = lookup(each.value, "cidr_blocks", null)
ipv6_cidr_blocks = lookup(each.value, "ipv6_cidr_blocks", null)
prefix_list_ids = lookup(each.value, "prefix_list_ids", null)
self = lookup(each.value, "self", null)
security_group_id = aws_security_group.default.id
}
#######################
## SECURITY LOGGING ##
#####################
module "kms_key" {
source = "cloudposse/kms-key/aws"
version = "0.12.2"
enabled = var.session_logging_enabled && var.session_logging_encryption_enabled && length(var.session_logging_kms_key_arn) == 0
context = module.logs_label.context
description = "KMS key for encrypting Session Logs in S3 and CloudWatch."
deletion_window_in_days = 10
enable_key_rotation = true
alias = var.session_logging_kms_key_alias
policy = <<DOC
{
"Version" : "2012-10-17",
"Id" : "${module.logs_label.id}-policy",
"Statement" : [
{
"Sid" : "Enable IAM User Permissions",
"Effect" : "Allow",
"Principal" : {
"AWS" : "arn:aws:iam::${local.account_id}:root"
},
"Action" : "kms:*",
"Resource" : "*"
},
{
"Effect": "Allow",
"Principal": {
"Service": "logs.${local.region}.amazonaws.com"
},
"Action": [
"kms:Encrypt*",
"kms:Decrypt*",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Describe*"
],
"Resource": "*",
"Condition": {
"ArnLike": {
"kms:EncryptionContext:aws:logs:arn": "arn:aws:logs:${local.region}:${local.account_id}:log-group:${module.logs_label.id}"
}
}
}
]
}
DOC
}
module "logs_bucket" {
source = "cloudposse/s3-bucket/aws"
version = "4.14.0"
enabled = local.logs_bucket_enabled
context = module.logs_label.context
# Encryption / Security
sse_algorithm = "aws:kms"
kms_master_key_arn = local.session_logging_kms_key_arn
allow_ssl_requests_only = var.allow_ssl_requests_only
allow_encrypted_uploads_only = var.allow_encrypted_uploads_only
force_destroy = true
lifecycle_configuration_rules = [{
enabled = true
id = module.logs_label.id
abort_incomplete_multipart_upload_days = 90
filter_and = null
expiration = {
days = 0
}
noncurrent_version_expiration = {
noncurrent_days = 365
}
noncurrent_version_transition = [{
noncurrent_days = 30
storage_class = "GLACIER"
}, ]
transition = [{
days = 90
storage_class = "GLACIER"
}, ]
}]
}
resource "aws_cloudwatch_log_group" "session_logging" {
count = var.session_logging_enabled ? 1 : 0
name = module.logs_label.id
retention_in_days = var.cloudwatch_retention_in_days
kms_key_id = var.session_logging_encryption_enabled ? local.session_logging_kms_key_arn : ""
tags = module.logs_label.tags
}
resource "aws_ssm_document" "session_logging" {
count = var.session_logging_enabled && var.create_run_shell_document ? 1 : 0
name = var.session_logging_ssm_document_name
document_type = "Session"
tags = module.logs_label.tags
content = <<DOC
{
"schemaVersion": "1.0",
"description": "Document to hold regional settings for Session Manager",
"sessionType": "Standard_Stream",
"inputs": {
"s3BucketName": "${local.session_logging_bucket_name}",
"s3KeyPrefix": "logs/",
"s3EncryptionEnabled": true,
"cloudWatchLogGroupName": "${module.this.id}",
"cloudWatchEncryptionEnabled": true,
"kmsKeyId": "${local.session_logging_kms_key_arn}",
"runAsEnabled": false,
"runAsDefaultUser": ""
}
}
DOC
}
############################
## LAUNCH TEMPLATE + ASG ##
##########################
resource "aws_launch_template" "default" {
name_prefix = module.this.id
image_id = coalesce(var.ami, data.aws_ami.amazon_linux_2023.id)
instance_type = var.instance_type
key_name = var.key_pair_name
user_data = base64encode(var.user_data)
update_default_version = true
monitoring {
enabled = var.monitoring_enabled
}
network_interfaces {
associate_public_ip_address = var.associate_public_ip_address
delete_on_termination = true
security_groups = concat(var.additional_security_group_ids, [aws_security_group.default.id])
}
iam_instance_profile {
name = aws_iam_instance_profile.default.name
}
tag_specifications {
resource_type = "instance"
tags = module.this.tags
}
tag_specifications {
resource_type = "volume"
tags = module.this.tags
}
lifecycle {
create_before_destroy = true
}
block_device_mappings {
device_name = local.root_volume_device_name
ebs {
encrypted = true
volume_size = var.volume_size
}
}
metadata_options {
http_endpoint = var.metadata_http_endpoint_enabled ? "enabled" : "disabled"
http_tokens = var.metadata_imdsv2_enabled ? "required" : "optional"
http_protocol_ipv6 = var.metadata_http_protocol_ipv6_enabled ? "enabled" : "disabled"
}
}
resource "aws_autoscaling_group" "default" {
name_prefix = "${module.this.id}-asg"
max_size = var.max_size
min_size = var.min_size
desired_capacity = var.desired_capacity
# By default, we don't care to protect from scale in as we want to roll instances frequently
protect_from_scale_in = var.protect_from_scale_in
vpc_zone_identifier = local.subnet_ids
default_cooldown = 180
health_check_grace_period = 180
health_check_type = "EC2"
termination_policies = [
"OldestLaunchConfiguration",
]
launch_template {
id = aws_launch_template.default.id
version = aws_launch_template.default.latest_version
}
instance_refresh {
strategy = "Rolling"
triggers = ["tag"]
preferences {
scale_in_protected_instances = var.scale_in_protected_instances
min_healthy_percentage = 50
}
}
dynamic "tag" {
for_each = module.this.tags
content {
key = tag.key
value = tag.value
propagate_at_launch = true
}
}
lifecycle {
create_before_destroy = true
}
}