-
Notifications
You must be signed in to change notification settings - Fork 4
/
patch-manager.tf
167 lines (145 loc) · 4.87 KB
/
patch-manager.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
resource "aws_ssm_maintenance_window" "scan" {
count = var.enable_patching ? 1 : 0
name = "scan-${var.infrastructurename}"
cutoff = 0
description = "Maintenance window for scanning for patch compliance"
duration = var.maintainance_duration
schedule = var.scan_schedule
schedule_timezone = "UTC"
tags = var.tags
}
resource "aws_ssm_maintenance_window" "install" {
count = var.enable_patching ? 1 : 0
name = "install-${var.infrastructurename}"
cutoff = 0
description = "Maintenance window for applying patches"
duration = var.maintainance_duration
schedule = var.install_schedule
schedule_timezone = "UTC"
tags = var.tags
}
resource "aws_ssm_maintenance_window_target" "scan" {
count = var.enable_patching ? 1 : 0
window_id = aws_ssm_maintenance_window.scan[0].id
resource_type = "INSTANCE"
targets {
key = "tag:Patch Group"
values = [local.patchgroupid]
}
}
resource "aws_ssm_maintenance_window_target" "scan_eks_nodes" {
count = var.enable_patching ? 1 : 0
window_id = aws_ssm_maintenance_window.scan[0].id
resource_type = "INSTANCE"
targets {
key = "tag:eks:cluster-name"
values = [module.eks.eks_cluster_id]
}
}
resource "aws_ssm_maintenance_window_target" "install" {
count = var.enable_patching && var.licenseServer ? 1 : 0
window_id = aws_ssm_maintenance_window.install[0].id
resource_type = "INSTANCE"
targets {
key = "tag:Patch Group"
values = [local.patchgroupid]
}
}
resource "aws_ssm_maintenance_window_task" "scan" {
count = var.enable_patching ? 1 : 0
max_concurrency = 50
max_errors = 0
priority = 1
task_type = "RUN_COMMAND"
task_arn = "AWS-RunPatchBaseline"
window_id = aws_ssm_maintenance_window.scan[0].id
targets {
key = "WindowTargetIds"
values = [aws_ssm_maintenance_window_target.scan[0].id, aws_ssm_maintenance_window_target.scan_eks_nodes[0].id]
}
task_invocation_parameters {
run_command_parameters {
comment = "Runs a compliance scan"
timeout_seconds = 600
cloudwatch_config {
cloudwatch_log_group_name = aws_cloudwatch_log_group.ssm_scan_log_group[0].name
cloudwatch_output_enabled = true
}
parameter {
name = "Operation"
values = ["Scan"]
}
}
}
}
resource "aws_ssm_maintenance_window_task" "install" {
count = var.enable_patching && var.licenseServer ? 1 : 0
max_concurrency = 50
max_errors = 0
priority = 1
task_type = "RUN_COMMAND"
task_arn = "AWS-RunPatchBaseline"
window_id = aws_ssm_maintenance_window.install[0].id
targets {
key = "WindowTargetIds"
values = [aws_ssm_maintenance_window_target.install[0].id]
}
task_invocation_parameters {
run_command_parameters {
comment = "Installs necessary patches"
timeout_seconds = 600
cloudwatch_config {
cloudwatch_log_group_name = aws_cloudwatch_log_group.ssm_install_log_group[0].name
cloudwatch_output_enabled = true
}
parameter {
name = "Operation"
values = ["Install"]
}
}
}
}
resource "aws_cloudwatch_log_group" "ssm_scan_log_group" {
count = var.enable_patching ? 1 : 0
name = local.patch_manager_cloudwatch_loggroup_scan
retention_in_days = var.cloudwatch_retention
kms_key_id = aws_kms_key.kms_key_cloudwatch_log_group.arn
tags = var.tags
}
resource "aws_cloudwatch_log_group" "ssm_install_log_group" {
count = var.enable_patching && var.licenseServer ? 1 : 0
name = local.patch_manager_cloudwatch_loggroup_install
retention_in_days = var.cloudwatch_retention
kms_key_id = aws_kms_key.kms_key_cloudwatch_log_group.arn
tags = var.tags
}
resource "aws_ssm_patch_baseline" "production" {
count = var.enable_patching && var.licenseServer ? 1 : 0
name = "${var.infrastructurename}-patch-baseline"
description = "Default Patch Baseline for Amazon Linux 2 Provided by AWS but with Medium Severity Security Patches."
operating_system = "AMAZON_LINUX_2"
approval_rule {
approve_after_days = 7
patch_filter {
key = "SEVERITY"
values = ["Important", "Critical", "Medium"]
}
patch_filter {
key = "CLASSIFICATION"
values = ["Security"]
}
}
approval_rule {
approve_after_days = 7
patch_filter {
key = "CLASSIFICATION"
values = ["Bugfix"]
}
}
tags = var.tags
}
resource "aws_ssm_patch_group" "patch_group" {
count = var.enable_patching && var.licenseServer ? 1 : 0
baseline_id = aws_ssm_patch_baseline.production[0].id
patch_group = local.patchgroupid
}