-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.tf
392 lines (329 loc) · 10.5 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
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
385
386
387
388
389
390
391
392
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")
)
}
resource "null_resource" "validate_instance_type" {
count = local.is_instance_compatible ? 0 : 1
lifecycle {
precondition {
condition = local.is_instance_compatible
error_message = "The instance_type must be compatible with the specified architecture. For x86_64, you cannot use instance types with ARM processors (e.g., t3, m5, c5). For arm64, use instance types with 'g' indicating ARM processor (e.g., t4g, c6g, m6g)."
}
}
}
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.name)
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), "")
logs_bucket_enabled = var.session_logging_enabled && length(var.session_logging_bucket_name) == 0
}
#####################
## SSM AGENT ROLE ##
###################
data "aws_iam_policy_document" "default" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
data "aws_s3_bucket" "logs_bucket" {
count = var.session_logging_enabled ? 1 : 0
bucket = try(coalesce(var.session_logging_bucket_name, module.logs_bucket.bucket_id), "")
}
# https://docs.aws.amazon.com/systems-manager/latest/userguide/getting-started-create-iam-instance-profile.html#create-iam-instance-profile-ssn-logging
data "aws_iam_policy_document" "session_logging" {
count = var.session_logging_enabled ? 1 : 0
statement {
sid = "SSMAgentSessionAllowS3Logging"
effect = "Allow"
actions = [
"s3:PutObject"
]
resources = ["${join("", data.aws_s3_bucket.logs_bucket.*.arn)}/*"]
}
statement {
sid = "SSMAgentSessionAllowCloudWatchLogging"
effect = "Allow"
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams"
]
resources = ["*"]
}
statement {
sid = "SSMAgentSessionAllowGetEncryptionConfig"
effect = "Allow"
actions = [
"s3:GetEncryptionConfiguration"
]
resources = ["*"]
}
statement {
sid = "SSMAgentSessionAllowKMSDataKey"
effect = "Allow"
actions = [
"kms:GenerateDataKey"
]
resources = ["*"]
}
}
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_instance_profile" "default" {
name = module.role_label.id
role = aws_iam_role.default.name
}
#####################
## SECURITY GROUP ##
###################
resource "aws_security_group" "default" {
vpc_id = var.vpc_id
name = module.this.id
description = "Allow ALL egress from SSM Agent."
tags = module.this.tags
}
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"]
security_group_id = aws_security_group.default.id
}
#######################
## SECURITY LOGGING ##
#####################
module "kms_key" {
source = "cloudposse/kms-key/aws"
version = "0.12.1"
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 = "3.1.2"
enabled = local.logs_bucket_enabled
context = module.logs_label.context
# Encryption / Security
acl = "private"
sse_algorithm = "aws:kms"
kms_master_key_arn = local.session_logging_kms_key_arn
allow_encrypted_uploads_only = false
force_destroy = true
# Feature enablement
user_enabled = false
versioning_enabled = 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 = "/dev/xvda"
ebs {
encrypted = true
}
}
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 = var.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
}
}