feat(module): add default value [] to input variable assume_role_account_names

This commit is contained in:
Julien Dessaux 2025-04-12 08:53:00 +02:00
parent a046131bd2
commit 998b78cbe6
Signed by: adyxax
GPG key ID: F92E51B86E07177E
3 changed files with 52 additions and 40 deletions

View file

@ -2,8 +2,14 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## 1.0.1 - 2025-04-12
### Added
- Added default value `[]` to input variable `assume_role_account_names`.
## 1.0.0 - 2025-04-11 ## 1.0.0 - 2025-04-11
### Added ### Added
- initial import - Initial import.

83
main.tf
View file

@ -1,9 +1,10 @@
data "aws_organizations_organization" "main" {} data "aws_organizations_organization" "main" {}
locals { locals {
aws_account_ids = { for info in data.aws_organizations_organization.main.accounts : aws_account_ids = length(var.assume_role_account_names) > 0 ? {
for info in data.aws_organizations_organization.main.accounts :
info.name => info.id info.name => info.id
} } : {}
} }
resource "aws_iam_user" "main" { resource "aws_iam_user" "main" {
@ -14,43 +15,47 @@ resource "aws_iam_user" "main" {
resource "aws_iam_user_policy" "main" { resource "aws_iam_user_policy" "main" {
name = var.name name = var.name
policy = jsonencode({ policy = jsonencode({
Statement = concat([ Statement = concat(
{ # Assume roles in AWS sub-accounts length(var.assume_role_account_names) > 0 ? [
Action = "sts:AssumeRole" { # Assume roles in AWS sub-accounts
Effect = "Allow" Action = "sts:AssumeRole"
Resource = [for name in var.assume_role_account_names : Effect = "Allow"
format( Resource = [for name in var.assume_role_account_names :
"arn:aws:iam::%s:role/%s", format(
local.aws_account_ids[name], "arn:aws:iam::%s:role/%s",
var.name, local.aws_account_ids[name],
) var.name,
] )
}, ]
{ }
Action = [ ] : [],
# Manage the user's own IAM access key [
"iam:CreateAccessKey", {
"iam:DeleteAccessKey", Action = [
"iam:UpdateAccessKey", # Manage the user's own IAM access key
# Read only access to the user's IAM object "iam:CreateAccessKey",
"iam:Get*", "iam:DeleteAccessKey",
"iam:List*", "iam:UpdateAccessKey",
] # Read only access to the user's IAM object
Effect = "Allow" "iam:Get*",
Resource = aws_iam_user.main.arn "iam:List*",
}, ]
{ Effect = "Allow"
Action = [ Resource = aws_iam_user.main.arn
# Necessary for removing an IAM user },
"iam:ListVirtualMFADevices", {
# Describe and list the organization accounts Action = [
"organizations:DescribeOrganization", # Necessary for removing an IAM user
"organizations:List*", "iam:ListVirtualMFADevices",
] # Describe and list the organization accounts
Effect = "Allow" "organizations:DescribeOrganization",
Resource = "*" "organizations:List*",
}, ]
]) Effect = "Allow"
Resource = "*"
},
]
)
Version = "2012-10-17" Version = "2012-10-17"
}) })
user = aws_iam_user.main.name user = aws_iam_user.main.name

View file

@ -1,4 +1,5 @@
variable "assume_role_account_names" { variable "assume_role_account_names" {
default = []
description = "The names of the AWS sub-accounts this IAM user can assume roles in." description = "The names of the AWS sub-accounts this IAM user can assume roles in."
nullable = false nullable = false
type = list(string) type = list(string)