generated from cloudposse/terraform-example-module
-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.tf
236 lines (202 loc) · 7.02 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
locals {
enabled = module.this.enabled
}
data "aws_caller_identity" "current" {
count = local.enabled ? 1 : 0
}
data "aws_partition" "current" {
count = local.enabled ? 1 : 0
}
module "scan_window_label" {
source = "cloudposse/label/null"
version = "0.25.0"
enabled = local.enabled
attributes = ["scan-window"]
context = module.this.context
}
resource "aws_ssm_maintenance_window" "scan_window" {
count = local.enabled ? 1 : 0
name = module.scan_window_label.id
schedule = var.scan_maintenance_window_schedule
duration = var.scan_maintenance_window_duration
cutoff = var.scan_maintenance_window_cutoff
}
resource "aws_ssm_maintenance_window_task" "task_scan_patches" {
count = local.enabled ? 1 : 0
window_id = aws_ssm_maintenance_window.scan_window[0].id
task_type = "RUN_COMMAND"
task_arn = "AWS-RunPatchBaseline"
priority = var.task_scan_priority
service_role_arn = var.service_role_arn
max_concurrency = var.max_concurrency
max_errors = var.max_errors
targets {
key = "WindowTargetIds"
values = aws_ssm_maintenance_window_target.target_scan[*].id
}
task_invocation_parameters {
run_command_parameters {
parameter {
name = "Operation"
values = ["Scan"]
}
parameter {
name = "RebootOption"
values = ["NoReboot"]
}
output_s3_bucket = var.s3_log_output_enabled ? local.bucket_id : null
output_s3_key_prefix = var.s3_log_output_enabled ? var.s3_bucket_prefix_scan_logs : null
service_role_arn = var.sns_notification_role_arn
dynamic "notification_config" {
for_each = var.scan_sns_notification_enabled ? [1] : []
content {
notification_arn = var.notification_arn
notification_events = var.notification_events
notification_type = var.notification_type
}
}
dynamic "cloudwatch_config" {
for_each = var.cloudwatch_log_output_enabled ? [1] : []
content {
cloudwatch_output_enabled = var.cloudwatch_log_output_enabled
cloudwatch_log_group_name = var.cloudwatch_log_group_name
}
}
}
}
}
resource "aws_ssm_maintenance_window_target" "target_scan" {
count = local.enabled ? 1 : 0
window_id = aws_ssm_maintenance_window.scan_window[0].id
resource_type = "INSTANCE"
dynamic "targets" {
for_each = toset(var.scan_maintenance_windows_targets)
content {
key = targets.value.key
values = targets.value.values
}
}
dynamic "targets" {
for_each = length(var.scan_maintenance_windows_targets) == 0 ? [1] : []
content {
key = "tag:Patch Group"
values = var.scan_patch_groups
}
}
}
# Maintenance Windows for patching
module "install_window_label" {
source = "cloudposse/label/null"
version = "0.25.0"
enabled = local.enabled
attributes = ["install-window"]
context = module.this.context
}
resource "aws_ssm_maintenance_window" "install_window" {
count = local.enabled ? 1 : 0
name = module.install_window_label.id
schedule = var.install_maintenance_window_schedule
duration = var.install_maintenance_window_duration
cutoff = var.install_maintenance_window_cutoff
}
resource "aws_ssm_maintenance_window_task" "task_install_patches" {
count = local.enabled ? 1 : 0
window_id = aws_ssm_maintenance_window.install_window[0].id
task_type = "RUN_COMMAND"
task_arn = "AWS-RunPatchBaseline"
priority = var.task_install_priority
service_role_arn = var.service_role_arn
max_concurrency = var.max_concurrency
max_errors = var.max_errors
targets {
key = "WindowTargetIds"
values = aws_ssm_maintenance_window_target.target_install[*].id
}
task_invocation_parameters {
run_command_parameters {
parameter {
name = "Operation"
values = ["Install"]
}
parameter {
name = "RebootOption"
values = [var.reboot_option]
}
output_s3_bucket = var.s3_log_output_enabled ? local.bucket_id : null
output_s3_key_prefix = var.s3_log_output_enabled ? var.s3_bucket_prefix_scan_logs : null
service_role_arn = var.sns_notification_role_arn
dynamic "notification_config" {
for_each = var.install_sns_notification_enabled ? [1] : []
content {
notification_arn = var.notification_arn
notification_events = var.notification_events
notification_type = var.notification_type
}
}
dynamic "cloudwatch_config" {
for_each = var.cloudwatch_log_output_enabled ? [1] : []
content {
cloudwatch_output_enabled = var.cloudwatch_log_output_enabled
cloudwatch_log_group_name = var.cloudwatch_log_group_name
}
}
}
}
}
resource "aws_ssm_maintenance_window_target" "target_install" {
count = local.enabled ? 1 : 0
window_id = aws_ssm_maintenance_window.install_window[0].id
resource_type = "INSTANCE"
dynamic "targets" {
for_each = toset(var.install_maintenance_windows_targets)
content {
key = targets.value.key
values = targets.value.values
}
}
dynamic "targets" {
for_each = length(var.install_maintenance_windows_targets) == 0 ? [1] : []
content {
key = "tag:Patch Group"
values = var.install_patch_groups
}
}
}
# Patch Baselines
resource "aws_ssm_patch_baseline" "baseline" {
count = local.enabled ? 1 : 0
name = "${module.this.id}-${var.operating_system}"
description = "${var.operating_system} baseline"
operating_system = var.operating_system
approved_patches = var.approved_patches
rejected_patches = var.rejected_patches
approved_patches_compliance_level = var.approved_patches_compliance_level
dynamic "approval_rule" {
for_each = toset(var.patch_baseline_approval_rules)
content {
approve_after_days = approval_rule.value.approve_after_days
approve_until_date = approval_rule.value.approve_until_date
compliance_level = approval_rule.value.compliance_level
enable_non_security = approval_rule.value.enable_non_security
# https://docs.aws.amazon.com/cli/latest/reference/ssm/describe-patch-properties.html
dynamic "patch_filter" {
for_each = approval_rule.value.patch_baseline_filters
content {
key = patch_filter.value.name
values = patch_filter.value.values
}
}
}
}
tags = module.this.tags
}
resource "aws_ssm_patch_group" "install_patchgroup" {
count = (local.enabled ? 1 : 0) * length(var.install_patch_groups)
baseline_id = aws_ssm_patch_baseline.baseline[0].id
patch_group = element(var.install_patch_groups, count.index)
}
resource "aws_ssm_patch_group" "scan_patchgroup" {
count = (local.enabled ? 1 : 0) * length(var.scan_patch_groups)
baseline_id = aws_ssm_patch_baseline.baseline[0].id
patch_group = element(var.scan_patch_groups, count.index)
}