Skip to content

Commit 0828b6e

Browse files
committed
feat: use latest pattern for applying policies to roles
1 parent 2f5bf94 commit 0828b6e

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

main.tf

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ locals {
33
enabled = module.this.enabled
44
aws_account_id = try(coalesce(var.aws_account_id, data.aws_caller_identity.current[0].account_id), "")
55
aws_region_name = try(coalesce(var.aws_region_name, data.aws_region.current[0].name), "")
6+
aws_partition = one(data.aws_partition.current.*.partition)
67

78
email_sender_enabled = local.enabled && var.email_sender_enabled
89
email_sender_policy_path = "./policy.rego"
@@ -12,6 +13,10 @@ locals {
1213
sms_sender_policy_path = "./policy.wasm"
1314
sms_sender_policy_content = var.sms_sender_policy_content
1415
sms_sender_throttle_period_in_minutes = 15
16+
17+
iam_role_policies = {
18+
message-sender-access = one(data.aws_iam_policy_document.this.*.json)
19+
}
1520
}
1621

1722
data "aws_caller_identity" "current" {
@@ -22,6 +27,11 @@ data "aws_region" "current" {
2227
count = local.enabled ? 1 : 0
2328
}
2429

30+
data "aws_partition" "current" {
31+
count = module.this.enabled ? 1 : 0
32+
}
33+
34+
2535
# ============================================================ message-sender ===
2636

2737
module "message_sender_label" {
@@ -61,18 +71,25 @@ resource "aws_iam_role" "this" {
6171
}]
6272
})
6373

64-
managed_policy_arns = [
65-
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
66-
]
74+
tags = module.message_sender_label.tags
75+
}
6776

68-
inline_policy {
69-
name = "message-sender-access"
70-
policy = data.aws_iam_policy_document.this[0].json
71-
}
77+
resource "aws_iam_role_policy_attachment" "ssm_managed_instance_core" {
78+
count = module.this.enabled ? 1 : 0
7279

73-
tags = module.message_sender_label.tags
80+
role = aws_iam_role.this[0].name
81+
policy_arn = "arn:${local.aws_partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
7482
}
7583

84+
resource "aws_iam_role_policy" "this" {
85+
for_each = { for k, v in local.iam_role_policies : k => v if v != null }
86+
87+
name = each.key
88+
role = resource.aws_iam_role.this[0].name
89+
policy = each.value
90+
}
91+
92+
7693
data "aws_iam_policy_document" "this" {
7794
count = local.enabled ? 1 : 0
7895

0 commit comments

Comments
 (0)