-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathusers.tf
More file actions
70 lines (56 loc) · 2.71 KB
/
Copy pathusers.tf
File metadata and controls
70 lines (56 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# The non-admin users being created
resource "aws_iam_user" "users" {
provider = aws.users
for_each = toset(keys(var.users))
name = each.key
}
# The login profile for each user; note that the user's initial console
# password is set here, and the user is required to change it at first login.
resource "aws_iam_user_login_profile" "users" {
provider = aws.users
# for_each = toset(keys(var.users))
for_each = { for k, v in var.users : k => v if v["console_access"] }
password_reset_required = true
user = aws_iam_user.users[each.key].name
lifecycle {
# Required so that Terraform doesn't reset the password if the user login
# profile was created outside of Terraform (password_length) or after the
# user has changed their initial password (password_reset_required).
ignore_changes = [
password_length,
password_reset_required
]
}
}
# Attach the self-administration (with MFA required) policy to each user
# where self_managed is true and require_mfa is true
resource "aws_iam_user_policy_attachment" "self_managed_creds_with_mfa" {
provider = aws.users
# Ensure that the user exists before attempting to attach the
# policy. Ideally the depends_on could live inside the scope of the
# for_each, so that we could depend only on the specific user that
# this policy attachment references, but that is not possible;
# hence, we must depend on _all_ users. The effect is the same,
# although getting there is less efficient since _all_ the users are
# created before _any_ policy attachments.
depends_on = [aws_iam_user.users]
for_each = { for k, v in var.users : k => v if v["self_managed"] && v["require_mfa"] }
policy_arn = data.terraform_remote_state.users.outputs.selfmanagedcredswithmfa_policy.arn
user = each.key
}
# Attach the self-administration (without MFA required) policy to each user
# where self_managed is true and require_mfa is false
resource "aws_iam_user_policy_attachment" "self_managed_creds_without_mfa" {
provider = aws.users
# Ensure that the user exists before attempting to attach the
# policy. Ideally the depends_on could live inside the scope of the
# for_each, so that we could depend only on the specific user that
# this policy attachment references, but that is not possible;
# hence, we must depend on _all_ users. The end result is the same,
# although getting there is less efficient since _all_ the users are
# created before _any_ policy attachments.
depends_on = [aws_iam_user.users]
for_each = { for k, v in var.users : k => v if v["self_managed"] && !v["require_mfa"] }
policy_arn = data.terraform_remote_state.users.outputs.selfmanagedcredswithoutmfa_policy.arn
user = each.key
}