Skip to content

Commit f2b890c

Browse files
authored
feat: switching to the aws_role_policy and away from the deprecated field (#52)
1 parent 600d526 commit f2b890c

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

modules/role/main.tf

+29-25
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

22
locals {
3-
## The name of the iam role to create for the readonly
3+
## The name of the iam role to create for the readonly
44
readonly_role_name = format("%s-ro", var.name)
55
## The name of the iam role to create for the readwrite
66
readwrite_role_name = var.name
7-
## The name of the iam role to create for the state reader
7+
## The name of the iam role to create for the state reader
88
state_reader_role_name = format("%s-sr", var.name)
99
}
1010

@@ -53,20 +53,22 @@ resource "aws_iam_role" "ro" {
5353
path = var.role_path
5454
permissions_boundary = local.permission_boundary_arn
5555
tags = merge(var.tags, { Name = local.readonly_role_name })
56+
}
5657

57-
inline_policy {
58-
name = "tfstate_plan"
59-
policy = data.aws_iam_policy_document.tfstate_plan.json
60-
}
58+
## Create an inline policy for the read only role
59+
resource "aws_iam_role_policy" "tfstate_plan_ro" {
60+
name = "tfstate_plan"
61+
role = aws_iam_role.ro.id
62+
policy = data.aws_iam_policy_document.tfstate_plan.json
63+
}
6164

62-
dynamic "inline_policy" {
63-
for_each = merge(var.read_only_inline_policies, var.default_inline_policies)
65+
## Provision the inline policies for the read only role
66+
resource "aws_iam_role_policy" "inline_policies_ro" {
67+
for_each = merge(var.read_only_inline_policies, var.default_inline_policies)
6468

65-
content {
66-
name = inline_policy.key
67-
policy = inline_policy.value
68-
}
69-
}
69+
name = each.key
70+
role = aws_iam_role.ro.id
71+
policy = each.value
7072
}
7173

7274
## Attach the read only policies to the read only role
@@ -129,7 +131,7 @@ data "aws_iam_policy_document" "rw" {
129131
}
130132
}
131133

132-
## Provision the read write role
134+
## Provision the read write role
133135
resource "aws_iam_role" "rw" {
134136
assume_role_policy = data.aws_iam_policy_document.rw.json
135137
description = var.description
@@ -139,20 +141,22 @@ resource "aws_iam_role" "rw" {
139141
path = var.role_path
140142
permissions_boundary = local.permission_boundary_arn
141143
tags = merge(var.tags, { Name = local.readwrite_role_name })
144+
}
142145

143-
inline_policy {
144-
name = "tfstate_apply"
145-
policy = data.aws_iam_policy_document.tfstate_apply.json
146-
}
146+
## Provision the inline terraform policy for the rw role
147+
resource "aws_iam_role_policy" "tfstate_apply_rw" {
148+
name = "tfstate_apply"
149+
role = aws_iam_role.rw.id
150+
policy = data.aws_iam_policy_document.tfstate_apply.json
151+
}
147152

148-
dynamic "inline_policy" {
149-
for_each = merge(var.read_write_inline_policies, var.default_inline_policies)
153+
## Provision the inline policies for the read write role
154+
resource "aws_iam_role_policy" "inline_policies_rw" {
155+
for_each = merge(var.read_write_inline_policies, var.default_inline_policies)
150156

151-
content {
152-
name = inline_policy.key
153-
policy = inline_policy.value
154-
}
155-
}
157+
name = each.key
158+
role = aws_iam_role.rw.id
159+
policy = each.value
156160
}
157161

158162
## Attach the read write policies to the read write role

0 commit comments

Comments
 (0)