Skip to content

Commit 1aef75e

Browse files
committed
[NRL-1739] Add automatic scheduled updates for Window PowerBI GW instances
1 parent 7b6d4e6 commit 1aef75e

File tree

2 files changed

+63
-2
lines changed
  • terraform/account-wide-infrastructure/modules/powerbi-gw-ec2

2 files changed

+63
-2
lines changed

terraform/account-wide-infrastructure/modules/powerbi-gw-ec2/ec2.tf

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,71 @@ resource "aws_instance" "powerbi_gw" {
1515
user_data = file("${path.module}/scripts/user_data.tpl")
1616

1717
tags = {
18-
Name = "${var.name_prefix}-ec2"
18+
Name = "${var.name_prefix}-ec2"
19+
PatchGroup = local.windows_patching_tag
1920
}
2021

2122
}
2223

24+
resource "aws_ssm_maintenance_window" "updates" {
25+
name = "windows-updates"
26+
schedule = "cron(0 2 ? * SUN *)" # Sunday 2am UTC
27+
duration = 3
28+
cutoff = 1
29+
}
30+
31+
resource "aws_ssm_maintenance_window_target" "windows_instances" {
32+
window_id = aws_ssm_maintenance_window.updates.id
33+
resource_type = "INSTANCE"
34+
35+
targets {
36+
key = "tag:PatchGroup"
37+
values = [local.windows_patching_tag]
38+
}
39+
}
40+
41+
resource "aws_ssm_maintenance_window_task" "patch_task" {
42+
window_id = aws_ssm_maintenance_window.updates.id
43+
task_type = "RUN_COMMAND"
44+
task_arn = "AWS-RunPatchBaseline"
45+
priority = 1
46+
service_role_arn = aws_iam_role.maintenance_window_role.arn
47+
48+
targets {
49+
key = "WindowTargetIds"
50+
values = [aws_ssm_maintenance_window_target.windows_instances.id]
51+
}
52+
53+
task_invocation_parameters {
54+
run_command_parameters {
55+
parameter {
56+
name = "Operation"
57+
values = ["Install"]
58+
}
59+
}
60+
}
61+
}
62+
63+
resource "aws_iam_role" "maintenance_window_role" {
64+
name = "maintenance-window-role"
65+
66+
assume_role_policy = jsonencode({
67+
Version = "2012-10-17"
68+
Statement = [{
69+
Action = "sts:AssumeRole"
70+
Effect = "Allow"
71+
Principal = {
72+
Service = "ssm.amazonaws.com"
73+
}
74+
}]
75+
})
76+
}
77+
78+
resource "aws_iam_role_policy_attachment" "maintenance_window_policy" {
79+
role = aws_iam_role.maintenance_window_role.name
80+
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonSSMMaintenanceWindowRole"
81+
}
82+
2383
resource "tls_private_key" "instance_key_pair" {
2484
algorithm = "RSA"
2585
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
locals {
2-
selected_ami_id = var.use_custom_ami ? data.aws_ami.PowerBI_Gateway[0].id : data.aws_ami.windows-2019.id
2+
selected_ami_id = var.use_custom_ami ? data.aws_ami.PowerBI_Gateway[0].id : data.aws_ami.windows-2019.id
3+
windows_patching_tag = "windows_scheduled_patching"
34
}

0 commit comments

Comments
 (0)