Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add create multiple IAM users #2

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
resource "aws_iam_user" "test" {
name = var.aws_iam_username
tags = var.aws_iam_username_tags
for_each = var.aws_iam_username
name = each.key
tags = {
name = "tf-created-${each.value}"
}
}

resource "aws_iam_user_login_profile" "test_user_login_profile" {
user = aws_iam_user.test.name
for_each = var.aws_iam_username
user = aws_iam_user.test[each.key].name
password_length = var.aws_iam_user_login_profile_password_len
password_reset_required = false
# pgp_key = "keybase:your_key" can be used if the password requires encoding
Expand All @@ -16,10 +20,31 @@ data "aws_iam_policy" "iamadmin_policy" {
}

resource "aws_iam_user_policy_attachment" "test_attachment" {
for_each = var.aws_iam_username
policy_arn = data.aws_iam_policy.iamadmin_policy.arn
user = aws_iam_user.test.name
user = aws_iam_user.test[each.key].name
}

# resource "aws_iam_account_alias" "test_account_alias" {
# account_alias = "non-linear-trap"
# }


resource "aws_iam_group" "test_group" {
name = var.aws_iam_group_name
}

data "aws_iam_policy" "iamadmin_group_policy" {
arn = var.aws_iam_group_policy_arn
}

resource "aws_iam_group_policy_attachment" "test-group-attach" {
group = aws_iam_group.test_group.name
policy_arn = data.aws_iam_policy.iamadmin_group_policy.arn
}

resource "aws_iam_user_group_membership" "test_user_group_attach" {
for_each = var.aws_iam_username
user = aws_iam_user.test[each.key].name
groups = [aws_iam_group.test_group.name]
}
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

output "iamadmin_userpassword" {
value = aws_iam_user_login_profile.test_user_login_profile.password
value = [for out in values(aws_iam_user_login_profile.test_user_login_profile) : "${out.id}'s password is (${out.password})"]
sensitive = true
}
35 changes: 28 additions & 7 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
variable "aws_iam_username" {
type = string
default = "iamadmin-tf"
type = map(string)
# default = ["iamadmin-tf1", "iamadmin-tf2", "iamadmin-tf3", "iamadmin-tf4"]
default = {
"iamadmin-tf1" = "user1"
"iamadmin-tf2" = "user2"
"iamadmin-tf3" = "user3"
"iamadmin-tf4" = "user4"
}
description = "Name of the iam user"
}

Expand All @@ -22,10 +28,25 @@ variable "aws_iam_user_login_profile_password_len" {
variable "aws_iam_policy_arn" {
type = string
default = "arn:aws:iam::aws:policy/AdministratorAccess"
description = "AWS Managed IAM Admin Access Policy ARN"
description = "AWS Managed EC2 Full Access Policy ARN"
}

variable "aws_iam_group_name" {
type = string
default = "test-group"
}


variable "aws_iam_group_tag" {
type = map(string)
default = {
"name" = "tf-created"
}
}

# variable "aws_secret_key" {
# type = string
# # sensitive = true
# }
variable "aws_iam_group_policy_arn" {
type = string
# default = "arn:aws:iam::aws:policy/AdministratorAccess"
default = "arn:aws:iam::aws:policy/AmazonEC2FullAccess"
description = "AWS Managed IAM Admin Access Policy ARN"
}