Skip to content

Commit 133e1b0

Browse files
committed
feat: Add a flag to enable/disable role creation
The module previously created a role automatically, and allowed managed policies and inline policies to be attached to the role, this works well for simple setups but makes it difficult to do multiple account/multiple role OIDC configurations.
1 parent 54470d2 commit 133e1b0

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

main.tf

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ locals {
1212
}
1313

1414
resource "aws_iam_role" "github" {
15-
count = var.enabled ? 1 : 0
15+
count = var.enabled && var.create_iam_role ? 1 : 0
1616

1717
assume_role_policy = data.aws_iam_policy_document.assume_role[0].json
1818
description = "Role assumed by the GitHub OIDC provider."
@@ -22,7 +22,6 @@ resource "aws_iam_role" "github" {
2222
path = var.iam_role_path
2323
permissions_boundary = var.iam_role_permissions_boundary
2424
tags = var.tags
25-
2625
}
2726

2827
resource "aws_iam_role_policy" "inline_policies" {
@@ -33,21 +32,27 @@ resource "aws_iam_role_policy" "inline_policies" {
3332
}
3433

3534
resource "aws_iam_role_policy_attachment" "admin" {
36-
count = var.enabled && var.dangerously_attach_admin_policy ? 1 : 0
35+
count = var.enabled && var.create_iam_role && var.dangerously_attach_admin_policy ? 1 : 0
3736

38-
policy_arn = "arn:${local.partition}:iam::aws:policy/AdministratorAccess"
39-
role = aws_iam_role.github[0].id
37+
policy_arn = format(
38+
"arn:%v:iam::aws:policy/AdministratorAccess",
39+
local.partition,
40+
)
41+
role = aws_iam_role.github[0].id
4042
}
4143

4244
resource "aws_iam_role_policy_attachment" "read_only" {
43-
count = var.enabled && var.attach_read_only_policy ? 1 : 0
45+
count = var.enabled && var.create_iam_role && var.attach_read_only_policy ? 1 : 0
4446

45-
policy_arn = "arn:${local.partition}:iam::aws:policy/ReadOnlyAccess"
46-
role = aws_iam_role.github[0].id
47+
policy_arn = format(
48+
"arn:${local.partition}:iam::aws:policy/ReadOnlyAccess",
49+
local.partition,
50+
)
51+
role = aws_iam_role.github[0].id
4752
}
4853

4954
resource "aws_iam_role_policy_attachment" "custom" {
50-
count = var.enabled ? length(var.iam_role_policy_arns) : 0
55+
count = var.enabled && var.create_iam_role ? length(var.iam_role_policy_arns) : 0
5156

5257
policy_arn = var.iam_role_policy_arns[count.index]
5358
role = aws_iam_role.github[0].id

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
output "iam_role_arn" {
55
depends_on = [aws_iam_role.github]
66
description = "ARN of the IAM role."
7-
value = var.enabled ? aws_iam_role.github[0].arn : ""
7+
value = var.enabled && var.create_iam_role ? aws_iam_role.github[0].arn : ""
88
}
99

1010
output "iam_role_name" {
1111
depends_on = [aws_iam_role.github]
1212
description = "Name of the IAM role."
13-
value = var.enabled ? aws_iam_role.github[0].name : ""
13+
value = var.enabled && var.create_iam_role ? aws_iam_role.github[0].name : ""
1414
}
1515

1616
output "oidc_provider_arn" {

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ variable "create_oidc_provider" {
3030
type = bool
3131
}
3232

33+
variable "create_iam_role" {
34+
default = true
35+
description = ""
36+
type = bool
37+
}
38+
3339
variable "dangerously_attach_admin_policy" {
3440
default = false
3541
description = "Flag to enable/disable the attachment of the AdministratorAccess policy."

0 commit comments

Comments
 (0)