-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
444 lines (377 loc) · 13.2 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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
data "aws_region" "current" {}
# ------------------------------------------------------------------------------
# IAM
# ------------------------------------------------------------------------------
data "aws_iam_policy_document" "this" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_policy" "AmazonEC2SSMS3Logs" {
name = "AmazonEC2SSMS3Logs"
description = "Allow EC2 instances to write to ${var.name}-ssm/logs/ S3 bucket path."
path = "/${var.name}/"
policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Action" : [
"s3:PutObject"
],
"Resource" : "${module.s3_bucket.s3_bucket_arn}/logs/*"
}
]
})
}
resource "aws_iam_policy" "AmazonEC2SSMS3Playbooks" {
name = "AmazonEC2SSMS3Playbooks"
description = "Allow EC2 instances to read from ${var.name}-ssm/ansible/ S3 bucket path."
path = "/${var.name}/"
policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Action" : [
"s3:GetObject",
],
"Resource" : "${module.s3_bucket.s3_bucket_arn}/ansible/*"
},
{
"Effect" : "Allow",
"Action" : [
"s3:ListBucket",
],
"Resource" : "${module.s3_bucket.s3_bucket_arn}"
}
]
})
}
resource "aws_iam_policy" "AmazonEC2SSMCloudwatch" {
name = "AmazonEC2SSMCloudwatch"
description = "Allow EC2 instances to interact with ${var.name}-ssm cloudwatch group."
path = "/${var.name}/"
policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Action" : [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:DescribeLogStreams",
"logs:PutLogEvents"
],
"Resource" : "${aws_cloudwatch_log_group.this.arn}:*"
}
]
})
}
resource "aws_iam_role" "this" {
name = "${var.name}-ssm-managed-instance"
path = "/${var.name}/"
assume_role_policy = data.aws_iam_policy_document.this.json
}
resource "aws_iam_role_policy_attachment" "AmazonSSMManagedInstanceCore" {
role = aws_iam_role.this.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
resource "aws_iam_role_policy_attachment" "AmazonSSMPatchAssociation" {
role = aws_iam_role.this.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMPatchAssociation"
}
resource "aws_iam_role_policy_attachment" "AmazonEC2SSMS3Logs" {
role = aws_iam_role.this.name
policy_arn = aws_iam_policy.AmazonEC2SSMS3Logs.arn
}
resource "aws_iam_role_policy_attachment" "AmazonEC2SSMS3Playbooks" {
role = aws_iam_role.this.name
policy_arn = aws_iam_policy.AmazonEC2SSMS3Playbooks.arn
}
resource "aws_iam_role_policy_attachment" "AmazonEC2SSMCloudwatch" {
role = aws_iam_role.this.name
policy_arn = aws_iam_policy.AmazonEC2SSMCloudwatch.arn
}
resource "aws_iam_instance_profile" "this" {
name = "${var.name}-ssm-managed-instance-profile"
role = aws_iam_role.this.name
path = "/${var.name}/"
}
# ----------------------------------------------------------------------
# Cloudwatch
# ----------------------------------------------------------------------
resource "aws_cloudwatch_log_group" "this" {
name = "${var.name}-ssm"
}
# ------------------------------------------------------------------------------
# S3
# ------------------------------------------------------------------------------
module "s3_bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
version = "~> 3.0"
bucket = "${var.name}-ssm"
acl = "private"
versioning = {
enabled = true
}
force_destroy = true
}
resource "aws_s3_object" "this" {
for_each = fileset(path.root, "ansible/**")
bucket = module.s3_bucket.s3_bucket_id
key = each.value
source = "${path.root}/${each.value}"
etag = filemd5("${path.root}/${each.value}")
}
# ------------------------------------------------------------------------------
# SSM Documents
# ------------------------------------------------------------------------------
# NOTE: The AWS-RunAnsiblePlaybook document allows to execute a single playbook
# file from an s3:// location but not from a directory structure. AWS is
# deprecating it and doesn't recomend using it. Instead, the recommendation is
# to use AWS-ApplyAnsiblePlaybooks.
#
# NOTE: The AWS-ApplyAnsiblePlaybooks document allows to execute playbooks from
# a directory structure which is previously downloaded from a public s3 bucket
# to the EC2 instance where ssm-agent is running. This document doesn't support
# s3://.
#
# Neither options fit our need. So we've cloned AWS-ApplyAnsiblePlaybooks
# document into `${var.name}-ApplyAnsiblePlaybooks' document and introduced
# changes to fit our needs related to ansible playbooks execution.
#
# The `${var.name}-ApplyAnsiblePlaybooks' is intended to run regularly, as SSM
# association, to keep our hypothetical application configuration we are
# running in the EC2 instance in compliance with whatever rules specified in
# the configuration playbook. It is also used by SSM Maintenance Windows to
# check that everything is working as expected after automatic patching task.
resource "aws_ssm_document" "ApplyAnsiblePlaybooks" {
name = "${var.name}-ApplyAnsiblePlaybooks"
document_format = "JSON"
document_type = "Command"
content = templatefile("${path.module}/documents/ApplyAnsiblePlaybooks.json", { "name" = var.name })
}
# ------------------------------------------------------------------------------
# SSM Associations
# ------------------------------------------------------------------------------
locals {
# Define targets of the SSM association. AWS currently supports a maximum of
# 5 targets. By default, and for consistency, we are using `tag:Patch Group`
# as reference for all associations."
association_targets = [{
key = "tag:Patch Group"
values = [var.name]
}]
}
resource "aws_ssm_association" "UpdateSSMAgent" {
name = "AWS-UpdateSSMAgent"
association_name = "${var.name}-UpdateSSMAgent"
schedule_expression = "rate(14 days)"
max_concurrency = var.max_concurrency
max_errors = var.max_errors
compliance_severity = var.approved_patches_compliance_level
parameters = {
allowDowngrade = false
}
dynamic "targets" {
for_each = local.association_targets
content {
key = lookup(targets.value, "key")
values = lookup(targets.value, "values")
}
}
output_location {
s3_bucket_name = module.s3_bucket.s3_bucket_id
s3_key_prefix = "logs/AWS-UpdateSSMAgent"
}
}
resource "aws_ssm_association" "RunPatchBaseline" {
name = "AWS-RunPatchBaseline"
association_name = "${var.name}-RunPatchBaseline"
max_concurrency = var.max_concurrency
max_errors = var.max_errors
compliance_severity = var.approved_patches_compliance_level
schedule_expression = "rate(24 hours)"
parameters = {
Operation = "Scan"
}
dynamic "targets" {
for_each = local.association_targets
content {
key = lookup(targets.value, "key")
values = lookup(targets.value, "values")
}
}
output_location {
s3_bucket_name = module.s3_bucket.s3_bucket_id
s3_key_prefix = "logs/AWS-RunPatchBaseline"
}
}
resource "aws_ssm_association" "GatherSoftwareInventory" {
name = "AWS-GatherSoftwareInventory"
association_name = "${var.name}-GatherSoftwareInventory"
schedule_expression = "rate(30 minutes)"
compliance_severity = var.approved_patches_compliance_level
dynamic "targets" {
for_each = local.association_targets
content {
key = targets.value["key"]
values = targets.value["values"]
}
}
output_location {
s3_bucket_name = module.s3_bucket.s3_bucket_id
s3_key_prefix = "logs/AWS-GatherSoftwareInventory"
}
}
resource "aws_ssm_association" "ApplyAnsiblePlaybooks" {
name = aws_ssm_document.ApplyAnsiblePlaybooks.name
association_name = aws_ssm_document.ApplyAnsiblePlaybooks.name
schedule_expression = "rate(30 minutes)"
compliance_severity = var.approved_patches_compliance_level
dynamic "targets" {
for_each = local.association_targets
content {
key = targets.value["key"]
values = targets.value["values"]
}
}
parameters = {
SourceInfo = "s3://${var.name}-ssm/ansible/"
}
output_location {
s3_bucket_name = module.s3_bucket.s3_bucket_id
s3_key_prefix = "logs/${aws_ssm_document.ApplyAnsiblePlaybooks.name}"
}
}
# ------------------------------------------------------------------------------
# SSM Patch Baseline
# ------------------------------------------------------------------------------
resource "aws_ssm_patch_baseline" "this" {
name = var.name
description = "Patch baseline for ${var.name} instances."
operating_system = var.operating_system
approved_patches_compliance_level = var.approved_patches_compliance_level
approved_patches_enable_non_security = var.approved_patches_enable_non_security
dynamic "approval_rule" {
for_each = var.approval_rules
content {
approve_after_days = lookup(approval_rule.value, "approve_after_days")
compliance_level = lookup(approval_rule.value, "compliance_level")
enable_non_security = lookup(approval_rule.value, "enable_non_security")
dynamic "patch_filter" {
for_each = lookup(approval_rule.value, "patch_filters")
content {
key = lookup(patch_filter.value, "key")
values = lookup(patch_filter.value, "values")
}
}
}
}
}
# ------------------------------------------------------------------------------
# SSM Patch Group
# ------------------------------------------------------------------------------
resource "aws_ssm_patch_group" "this" {
baseline_id = aws_ssm_patch_baseline.this.id
patch_group = var.name
}
# ------------------------------------------------------------------------------
# SSM Maintenance Windows
# ------------------------------------------------------------------------------
resource "aws_ssm_maintenance_window" "this" {
name = var.name
description = "Maintenance window for ${var.name} instances."
enabled = lookup(var.maintenance_window, "enabled")
schedule = lookup(var.maintenance_window, "schedule")
schedule_timezone = lookup(var.maintenance_window, "schedule_timezone")
cutoff = lookup(var.maintenance_window, "cutoff")
duration = lookup(var.maintenance_window, "duration")
}
resource "aws_ssm_maintenance_window_target" "this" {
name = var.name
description = "Target instances for ${var.name} maintenance window."
window_id = aws_ssm_maintenance_window.this.id
resource_type = "INSTANCE"
owner_information = var.name
dynamic "targets" {
for_each = local.association_targets
content {
key = lookup(targets.value, "key")
values = lookup(targets.value, "values")
}
}
}
resource "aws_ssm_maintenance_window_task" "SystemPatches" {
name = "SystemPatches"
description = "Run system patching on patch group instances using the patch baseline configured."
priority = 10
max_concurrency = var.max_concurrency
max_errors = var.max_errors
task_arn = "AWS-RunPatchBaseline"
task_type = "RUN_COMMAND"
window_id = aws_ssm_maintenance_window.this.id
targets {
key = "WindowTargetIds"
values = [aws_ssm_maintenance_window_target.this.id]
}
task_invocation_parameters {
run_command_parameters {
output_s3_bucket = module.s3_bucket.s3_bucket_id
output_s3_key_prefix = "logs/AWS-RunPatchBaseline"
service_role_arn = aws_iam_role.this.arn
timeout_seconds = 600
cloudwatch_config {
cloudwatch_log_group_name = aws_cloudwatch_log_group.this.name
cloudwatch_output_enabled = true
}
parameter {
name = "Operation"
values = ["Install"]
}
parameter {
name = "RebootOption"
values = ["RebootIfNeeded"]
}
}
}
}
resource "aws_ssm_maintenance_window_task" "ApplyAnsiblePlaybooks" {
name = "ApplyAnsiblePlaybooks"
description = "Apply configuration and automated tests on application to validate that it is working as expected after patching."
priority = 20
max_concurrency = var.max_concurrency
max_errors = var.max_errors
task_arn = aws_ssm_document.ApplyAnsiblePlaybooks.name
task_type = "RUN_COMMAND"
window_id = aws_ssm_maintenance_window.this.id
targets {
key = "WindowTargetIds"
values = [aws_ssm_maintenance_window_target.this.id]
}
task_invocation_parameters {
run_command_parameters {
output_s3_bucket = module.s3_bucket.s3_bucket_id
output_s3_key_prefix = "logs/${aws_ssm_document.ApplyAnsiblePlaybooks.name}"
service_role_arn = aws_iam_role.this.arn
timeout_seconds = 600
parameter {
name = "SourceInfo"
values = ["s3://${var.name}-ssm/ansible/"]
}
}
}
}