-
Notifications
You must be signed in to change notification settings - Fork 2
/
patch.tf
95 lines (82 loc) · 2.69 KB
/
patch.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
# Auto Patch all targeted instances running Amazon Linux 2023
resource "aws_ssm_patch_baseline" "patch-example" {
name = "patch-example-baseline"
description = "Amazon Linux 2023 Patch Baseline"
operating_system = "AMAZON_LINUX_2023"
approval_rule {
enable_non_security = false # Set to true to install non-security updates
approve_after_days = 7
patch_filter {
key = "CLASSIFICATION"
values = ["*"]
}
}
}
resource "aws_ssm_patch_group" "patch-example" {
baseline_id = aws_ssm_patch_baseline.patch-example.id
patch_group = local.patching.amazon_linux.tag
}
resource "aws_ssm_maintenance_window" "patch-example" {
name = "patch-example-maintenance-install"
schedule = "cron(0 0 ? * SUN *)" # Every Sunday at 12 AM UTC
description = local.patching.amazon_linux.description
duration = 3
cutoff = 1
}
resource "aws_ssm_maintenance_window_target" "patch-example" {
window_id = aws_ssm_maintenance_window.patch-example.id
resource_type = "INSTANCE"
description = local.patching.amazon_linux.description
targets {
key = "tag:Patch Group"
values = [local.patching.amazon_linux.tag]
}
}
resource "aws_ssm_maintenance_window_task" "patch-example" {
window_id = aws_ssm_maintenance_window.patch-example.id
description = local.patching.amazon_linux.description
task_type = "RUN_COMMAND"
task_arn = "AWS-RunPatchBaseline"
priority = 1
service_role_arn = data.aws_iam_role.aws-service-role-for-amazon-ssm.arn
max_concurrency = "100%"
max_errors = "100%"
targets {
key = "WindowTargetIds"
values = [aws_ssm_maintenance_window_target.patch-example.id]
}
task_invocation_parameters {
run_command_parameters {
comment = "Amazon Linux 2023 Patch Baseline Install"
document_version = "$LATEST"
timeout_seconds = 3600
cloudwatch_config {
cloudwatch_log_group_name = aws_cloudwatch_log_group.patch-example.id
cloudwatch_output_enabled = true
}
parameter {
name = "Operation"
values = ["Install"]
}
}
}
}
resource "aws_cloudwatch_log_group" "patch-example" {
name = var.project
retention_in_days = 7
tags = {
Project = var.project
}
}
# Auto Update SSM agents on existing instances
resource "aws_ssm_association" "patch-example-ssm-agent-update" {
name = "AWS-UpdateSSMAgent"
association_name = "CustomAutoUpdateSSMAgent"
schedule_expression = "cron(0 0 ? * SAT *)" // Every Saturday at 12 AM UTC */
max_concurrency = "100%"
max_errors = "100%"
targets {
key = "tag:AutoPatch"
values = ["true"]
}
}