Skip to content

Commit e4184f8

Browse files
authored
fix: automatically determine the number of role policy arns (#119)
1 parent 355669c commit e4184f8

File tree

5 files changed

+5
-4
lines changed

5 files changed

+5
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
terraform.tfstate
33
*.tfstate*
44
terraform.tfvars
5+
.terraform.lock.hcl

examples/iam-assumable-role-with-oidc/main.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ module "iam_assumable_role_admin" {
2222
role_policy_arns = [
2323
"arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy",
2424
]
25-
number_of_role_policy_arns = 1
2625

2726
oidc_fully_qualified_subjects = ["system:serviceaccount:default:sa1", "system:serviceaccount:default:sa2"]
2827
}

modules/iam-assumable-role-with-oidc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This module supports IAM Roles for kubernetes service accounts as described in t
2828
| create\_role | Whether to create a role | `bool` | `false` | no |
2929
| force\_detach\_policies | Whether policies should be detached from this role when destroying | `bool` | `false` | no |
3030
| max\_session\_duration | Maximum CLI/API session duration in seconds between 3600 and 43200 | `number` | `3600` | no |
31-
| number\_of\_role\_policy\_arns | Number of IAM policies to attach to IAM role | `number` | `0` | no |
31+
| number\_of\_role\_policy\_arns | Number of IAM policies to attach to IAM role | `number` | `null` | no |
3232
| oidc\_fully\_qualified\_subjects | The fully qualified OIDC subjects to be added to the role policy | `set(string)` | `[]` | no |
3333
| oidc\_subjects\_with\_wildcards | The OIDC subject using wildcards to be added to the role policy | `set(string)` | `[]` | no |
3434
| provider\_url | URL of the OIDC Provider. Use provider\_urls to specify several URLs. | `string` | `""` | no |

modules/iam-assumable-role-with-oidc/main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ locals {
99
for url in local.urls :
1010
"arn:${data.aws_partition.current.partition}:iam::${local.aws_account_id}:oidc-provider/${url}"
1111
]
12+
number_of_role_policy_arns = coalesce(var.number_of_role_policy_arns, length(var.role_policy_arns))
1213
}
1314

1415
data "aws_caller_identity" "current" {}
@@ -68,7 +69,7 @@ resource "aws_iam_role" "this" {
6869
}
6970

7071
resource "aws_iam_role_policy_attachment" "custom" {
71-
count = var.create_role ? var.number_of_role_policy_arns : 0
72+
count = var.create_role ? local.number_of_role_policy_arns : 0
7273

7374
role = join("", aws_iam_role.this.*.name)
7475
policy_arn = var.role_policy_arns[count.index]

modules/iam-assumable-role-with-oidc/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ variable "role_policy_arns" {
7373
variable "number_of_role_policy_arns" {
7474
description = "Number of IAM policies to attach to IAM role"
7575
type = number
76-
default = 0
76+
default = null
7777
}
7878

7979

0 commit comments

Comments
 (0)