1
0

feat: added policy attachment to iam role module

This commit is contained in:
xpk
2026-03-31 08:13:10 +08:00
parent c8eba9a6f8
commit 36036bb6f5
4 changed files with 20 additions and 14 deletions
+3 -8
View File
@@ -40,14 +40,9 @@ module "CsiPodIdentity" {
] ]
} }
) )
} # 2 policies are required for the ebs csi to work
attach-managed-policies = [
# 2 policies are required for the ebs csi to work
resource "aws_iam_role_policy_attachment" "CsiPodIdentity" {
for_each = toset([
"arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess", "arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess",
"arn:aws:iam::aws:policy/service-role/AmazonEBSCSIDriverPolicy" "arn:aws:iam::aws:policy/service-role/AmazonEBSCSIDriverPolicy"
]) ]
role = module.CsiPodIdentity.name
policy_arn = each.value
} }
@@ -1,16 +1,13 @@
<!-- This readme file is generated with terraform-docs --> <!-- This readme file is generated with terraform-docs -->
## Requirements ## Requirements
| Name | Version | No requirements.
|------|---------|
| terraform | >= 1.3.0 |
| aws | ~> 5.0 |
## Providers ## Providers
| Name | Version | | Name | Version |
|------|---------| |------|---------|
| aws | ~> 5.0 | | aws | n/a |
## Modules ## Modules
@@ -23,17 +20,19 @@ No modules.
| [aws_iam_instance_profile.ip](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile) | resource | | [aws_iam_instance_profile.ip](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile) | resource |
| [aws_iam_policy.p](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource | | [aws_iam_policy.p](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_role.r](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource | | [aws_iam_role.r](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role_policy_attachment.CsiPodIdentity](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.pa](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource | | [aws_iam_role_policy_attachment.pa](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
## Inputs ## Inputs
| Name | Description | Type | Default | Required | | Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:| |------|-------------|------|---------|:--------:|
| attach-managed-policies | List of managed policy arns to attach to the role | `list(string)` | `[]` | no |
| create-instance-profile | Determines whether instance profile will be created | `bool` | `false` | no | | create-instance-profile | Determines whether instance profile will be created | `bool` | `false` | no |
| description | Description of IAM role | `string` | n/a | yes | | description | Description of IAM role | `string` | n/a | yes |
| max-session-duration | Max session duration in seconds | `number` | `3600` | no | | max-session-duration | Max session duration in seconds | `number` | `3600` | no |
| path | Path of IAM role. Defaults to /Customer/ | `string` | `"/Customer/"` | no | | path | Path of IAM role. Defaults to /Customer/ | `string` | `"/Customer/"` | no |
| policies | Map of policies to be created and attached | <pre>map(<br> object(<br> {<br> description = string<br> policy = string<br> }<br> )<br> )</pre> | `{}` | no | | policies | Map of policies to be created and attached | <pre>map(<br/> object(<br/> {<br/> description = string<br/> policy = string<br/> }<br/> )<br/> )</pre> | `{}` | no |
| role-name | Name of IAM role | `string` | n/a | yes | | role-name | Name of IAM role | `string` | n/a | yes |
| tags | Tags additional to default tags | `map(string)` | `{}` | no | | tags | Tags additional to default tags | `map(string)` | `{}` | no |
| trusted-entity | AWS service allowed to assume this role or a full assume role policy | `string` | n/a | yes | | trusted-entity | AWS service allowed to assume this role or a full assume role policy | `string` | n/a | yes |
@@ -47,4 +47,10 @@ resource "aws_iam_role_policy_attachment" "pa" {
for_each = aws_iam_policy.p for_each = aws_iam_policy.p
role = aws_iam_role.r.name role = aws_iam_role.r.name
policy_arn = each.value.arn policy_arn = each.value.arn
}
resource "aws_iam_role_policy_attachment" "CsiPodIdentity" {
for_each = toset(var.attach-managed-policies)
role = aws_iam_role.r.name
policy_arn = each.value
} }
@@ -22,6 +22,12 @@ variable "policies" {
default = {} default = {}
} }
variable "attach-managed-policies" {
type = list(string)
description = "List of managed policy arns to attach to the role"
default = []
}
variable "role-name" { variable "role-name" {
description = "Name of IAM role" description = "Name of IAM role"
type = string type = string