2022-09-15 16:31:30 +08:00
|
|
|
resource "aws_iam_user" "iam-user" {
|
|
|
|
name = var.iam-user-name
|
|
|
|
force_destroy = true
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_access_key" "iam-user-access-key" {
|
2024-03-26 14:46:25 +08:00
|
|
|
count = var.create-access-key ? 1 : 0
|
2022-09-15 16:31:30 +08:00
|
|
|
user = aws_iam_user.iam-user.name
|
|
|
|
}
|
|
|
|
|
2022-10-19 22:09:15 +08:00
|
|
|
resource "aws_iam_user_policy" "iam-user-policy" {
|
|
|
|
count = var.iam-user-policy != "" ? 1 : 0
|
|
|
|
name = var.iam-user-policy-name
|
|
|
|
user = aws_iam_user.iam-user.name
|
|
|
|
policy = var.iam-user-policy
|
|
|
|
}
|
2022-09-15 16:31:30 +08:00
|
|
|
|
2022-10-19 22:09:15 +08:00
|
|
|
resource "aws_iam_user_policy" "iam-user-selfservice-policy" {
|
|
|
|
name = "SelfServicePermissions"
|
2022-10-19 19:11:13 +08:00
|
|
|
user = aws_iam_user.iam-user.name
|
|
|
|
policy = data.aws_iam_policy_document.user-policy.json
|
|
|
|
}
|
|
|
|
|
2022-10-19 22:09:15 +08:00
|
|
|
data "aws_iam_policy_document" "user-policy" {
|
2022-10-19 19:11:13 +08:00
|
|
|
statement {
|
|
|
|
sid = "ManageOwnCredentials"
|
|
|
|
|
|
|
|
actions = [
|
|
|
|
"iam:ChangePassword",
|
2024-03-26 15:22:14 +08:00
|
|
|
"iam:UpdateLoginProfile",
|
2022-10-19 19:11:13 +08:00
|
|
|
"iam:CreateAccessKey",
|
|
|
|
"iam:DeleteAccessKey",
|
2024-03-26 14:37:49 +08:00
|
|
|
"iam:ListAccessKeys",
|
2022-10-19 19:11:13 +08:00
|
|
|
"iam:CreateVirtualMFADevice",
|
|
|
|
"iam:EnableMFADevice",
|
|
|
|
"iam:ListMFA*",
|
|
|
|
"iam:ListVirtualMFA*",
|
2024-03-26 15:22:14 +08:00
|
|
|
"iam:ResyncMFADevice",
|
|
|
|
"iam:GetUser"
|
2022-10-19 19:11:13 +08:00
|
|
|
]
|
|
|
|
|
2022-10-19 22:09:15 +08:00
|
|
|
effect = "Allow"
|
2024-03-26 14:37:49 +08:00
|
|
|
resources = ["arn:aws:iam::*:user/$${aws:username}"]
|
|
|
|
}
|
|
|
|
|
|
|
|
statement {
|
2024-03-26 15:22:14 +08:00
|
|
|
sid = "GetBasicUserInfo"
|
|
|
|
actions = [
|
|
|
|
"iam:GetAccountPasswordPolicy",
|
|
|
|
"iam:GetAccessKeyLastUsed",
|
|
|
|
"iam:GetUserPolicy"
|
|
|
|
]
|
2024-03-26 14:37:49 +08:00
|
|
|
effect = "Allow"
|
|
|
|
resources = ["*"]
|
2022-10-19 19:11:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 16:31:30 +08:00
|
|
|
resource "aws_iam_user_policy_attachment" "iam-user-managed-policies" {
|
2022-10-19 22:09:15 +08:00
|
|
|
count = length(var.add-to-groups) > 0 ? 0 : length(var.managed-policy-arns)
|
2022-09-15 16:31:30 +08:00
|
|
|
user = aws_iam_user.iam-user.name
|
2022-09-16 10:37:28 +08:00
|
|
|
policy_arn = var.managed-policy-arns[count.index]
|
2022-09-15 16:31:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_user_login_profile" "iam-user-profile" {
|
2024-03-26 15:22:14 +08:00
|
|
|
count = var.create-password ? 1 : 0
|
|
|
|
user = aws_iam_user.iam-user.name
|
|
|
|
password_length = 20
|
|
|
|
pgp_key = null
|
2022-10-20 09:12:29 +08:00
|
|
|
}
|
|
|
|
|
2024-03-26 14:37:49 +08:00
|
|
|
resource "random_id" "secrets-random-id" {
|
2022-10-20 09:12:29 +08:00
|
|
|
byte_length = 2
|
|
|
|
}
|
2024-03-26 14:46:25 +08:00
|
|
|
|
2022-09-15 16:31:30 +08:00
|
|
|
resource "aws_secretsmanager_secret" "secretmanager" {
|
|
|
|
count = var.create-access-key || var.create-password ? 1 : 0
|
2022-10-20 09:12:29 +08:00
|
|
|
name = "IamUserCredential-${random_id.secrets-random-id.dec}-${var.iam-user-name}"
|
2022-09-15 16:31:30 +08:00
|
|
|
description = "AWS resource credential"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_secretsmanager_secret_version" "iam-user-secret" {
|
|
|
|
count = var.create-access-key || var.create-password ? 1 : 0
|
|
|
|
secret_id = aws_secretsmanager_secret.secretmanager[0].id
|
|
|
|
secret_string = jsonencode(
|
2024-03-26 15:22:14 +08:00
|
|
|
{
|
|
|
|
"ConsolePassword" : length(aws_iam_user_login_profile.iam-user-profile[0].password) > 0 ? aws_iam_user_login_profile.iam-user-profile[0].password : "NotSet",
|
2022-09-15 16:31:30 +08:00
|
|
|
"AccessKeyId" : length(aws_iam_access_key.iam-user-access-key) > 0 ? aws_iam_access_key.iam-user-access-key[0].id : "NotSet",
|
|
|
|
"KeySecret" : length(aws_iam_access_key.iam-user-access-key) > 0 ? aws_iam_access_key.iam-user-access-key[0].secret : "NotSet"
|
2024-03-26 15:22:14 +08:00
|
|
|
}
|
|
|
|
)
|
2022-09-16 10:37:28 +08:00
|
|
|
}
|
|
|
|
|
2022-10-19 22:09:15 +08:00
|
|
|
resource "aws_iam_group_membership" "group-membership" {
|
|
|
|
for_each = toset(var.add-to-groups)
|
|
|
|
name = "MembershipToExistingGroups"
|
|
|
|
group = each.value
|
|
|
|
users = [aws_iam_user.iam-user.name]
|
2022-09-16 10:37:28 +08:00
|
|
|
}
|
2022-10-19 19:56:39 +08:00
|
|
|
|