forked from Flaconi/terraform-aws-iam-roles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
304 lines (234 loc) · 10.6 KB
/
main.tf
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# -------------------------------------------------------------------------------------------------
# 1. Account Settings
# -------------------------------------------------------------------------------------------------
# Create account alias (if not empty)
resource "aws_iam_account_alias" "default" {
count = var.account_alias != "" ? 1 : 0
account_alias = var.account_alias
}
# Setup account password policy
resource "aws_iam_account_password_policy" "default" {
count = var.account_pass_policy.manage == true ? 1 : 0
allow_users_to_change_password = var.account_pass_policy.allow_users_to_change_password
hard_expiry = var.account_pass_policy.hard_expiry
max_password_age = var.account_pass_policy.max_password_age
minimum_password_length = var.account_pass_policy.minimum_password_length
password_reuse_prevention = var.account_pass_policy.password_reuse_prevention
require_lowercase_characters = var.account_pass_policy.require_lowercase_characters
require_numbers = var.account_pass_policy.require_numbers
require_symbols = var.account_pass_policy.require_symbols
require_uppercase_characters = var.account_pass_policy.require_uppercase_characters
}
# -------------------------------------------------------------------------------------------------
# 2. Identity Providers
# -------------------------------------------------------------------------------------------------
# Create SAML providers
resource "aws_iam_saml_provider" "default" {
for_each = { for saml in var.providers_saml : saml.name => saml }
name = each.value.name
saml_metadata_document = file(each.value.file)
}
# Create OpenID Connect providers
resource "aws_iam_openid_connect_provider" "default" {
for_each = { for oidc in var.providers_oidc : md5(oidc.url) => oidc }
url = each.value.url
client_id_list = each.value.client_id_list
thumbprint_list = each.value.thumbprint_list
}
# -------------------------------------------------------------------------------------------------
# 3. Policies
# -------------------------------------------------------------------------------------------------
# Create customer managed policies
resource "aws_iam_policy" "policies" {
for_each = local.policies
name = lookup(each.value, "name")
path = lookup(each.value, "path", null) == null ? var.policy_path : lookup(each.value, "path")
description = lookup(each.value, "desc", null) == null ? var.policy_desc : lookup(each.value, "desc")
policy = templatefile(lookup(each.value, "file"), lookup(each.value, "vars"))
}
# -------------------------------------------------------------------------------------------------
# 4. Groups
# -------------------------------------------------------------------------------------------------
# Create groups
resource "aws_iam_group" "groups" {
for_each = { for group in var.groups : group.name => group }
name = lookup(each.value, "name")
path = lookup(each.value, "path", null) == null ? var.group_path : lookup(each.value, "path")
}
# Attach customer managed policies to group
resource "aws_iam_group_policy_attachment" "policy_attachments" {
for_each = local.group_policies
group = replace(each.key, format(":%s", each.value.name), "")
policy_arn = aws_iam_policy.policies[each.value.name].arn
# Terraform has no info that aws_iam_users and aws_iam_policies
# must be run first in order to create the groups,
# so we must explicitly tell it.
depends_on = [
aws_iam_group.groups,
aws_iam_policy.policies,
]
}
# Attach policy ARNs to group
resource "aws_iam_group_policy_attachment" "policy_arn_attachments" {
for_each = local.group_policy_arns
group = replace(each.key, format(":%s", each.value), "")
policy_arn = each.value
# Terraform has no info that aws_iam_users must be run first in order to create the groups,
# so we must explicitly tell it.
depends_on = [aws_iam_group.groups]
}
# Attach inline policies to group
resource "aws_iam_group_policy" "inline_policy_attachments" {
for_each = local.group_inline_policies
name = each.value.name
group = replace(each.key, format(":%s", each.value.name), "")
policy = templatefile(lookup(each.value, "file"), lookup(each.value, "vars"))
# Terraform has no info that aws_iam_users must be run first in order to create the users,
# so we must explicitly tell it.
depends_on = [aws_iam_group.groups]
}
# -------------------------------------------------------------------------------------------------
# 5. Users
# -------------------------------------------------------------------------------------------------
# Create users
resource "aws_iam_user" "users" {
for_each = { for user in var.users : user.name => user }
name = lookup(each.value, "name")
path = lookup(each.value, "path", null) == null ? var.user_path : lookup(each.value, "path")
# The boundary defines the maximum allowed permissions which cannot exceed.
# Even if the policy has higher permission, the boundary sets the final limit
permissions_boundary = each.value.permissions_boundary
tags = merge(
{
Name = lookup(each.value, "name")
},
var.tags
)
}
# Attach customer managed policies to user
resource "aws_iam_user_policy_attachment" "policy_attachments" {
for_each = local.user_policies
user = replace(each.key, format(":%s", each.value.name), "")
policy_arn = aws_iam_policy.policies[each.value.name].arn
# Terraform has no info that aws_iam_users and aws_iam_policies
# must be run first in order to create the users,
# so we must explicitly tell it.
depends_on = [
aws_iam_user.users,
aws_iam_policy.policies,
]
}
# Attach policy ARNs to user
resource "aws_iam_user_policy_attachment" "policy_arn_attachments" {
for_each = local.user_policy_arns
user = replace(each.key, format(":%s", each.value), "")
policy_arn = each.value
# Terraform has no info that aws_iam_users must be run first in order to create the users,
# so we must explicitly tell it.
depends_on = [aws_iam_user.users]
}
# Attach inline policies to user
resource "aws_iam_user_policy" "inline_policy_attachments" {
for_each = local.user_inline_policies
name = each.value.name
user = replace(each.key, format(":%s", each.value.name), "")
policy = templatefile(lookup(each.value, "file"), lookup(each.value, "vars"))
# Terraform has no info that aws_iam_users must be run first in order to create the users,
# so we must explicitly tell it.
depends_on = [aws_iam_user.users]
}
# Add 'Active' or 'Inactive' access key to an IAM user
resource "aws_iam_access_key" "access_key" {
for_each = local.user_access_keys
user = split(":", each.key)[0]
pgp_key = each.value.pgp_key
status = each.value.status
# Terraform has no info that aws_iam_users must be run first in order to create the users,
# so we must explicitly tell it.
depends_on = [aws_iam_user.users]
}
# Add users to groups
resource "aws_iam_user_group_membership" "group_membership" {
# Note: Only iterate over users which actually have a group specified
for_each = { for user in var.users : user.name => user if length(user.groups) > 0 }
user = each.value.name
groups = each.value.groups
# Terraform has no info that aws_iam_user|group must be run first in order to create the
# attachment, so we must explicitly tell it.
depends_on = [
aws_iam_user.users,
aws_iam_group.groups,
]
}
# -------------------------------------------------------------------------------------------------
# 6. Roles
# -------------------------------------------------------------------------------------------------
# Create roles
resource "aws_iam_role" "roles" {
for_each = { for role in var.roles : role.name => role }
name = lookup(each.value, "name")
path = lookup(each.value, "path", null) == null ? var.role_path : lookup(each.value, "path")
description = lookup(each.value, "desc", null) == null ? var.role_desc : lookup(each.value, "desc")
# This policy defines who/what is allowed to use the current role
assume_role_policy = lookup(each.value, "trust_policy_vars") == null ? file(lookup(each.value, "trust_policy_file")) : templatefile(lookup(each.value, "trust_policy_file"), lookup(each.value, "trust_policy_vars"))
# The boundary defines the maximum allowed permissions which cannot exceed.
# Even if the policy has higher permission, the boundary sets the final limit
permissions_boundary = each.value.permissions_boundary
# Allow session for X seconds
max_session_duration = var.role_max_session_duration
force_detach_policies = var.role_force_detach_policies
tags = merge(
{
Name = lookup(each.value, "name")
},
var.tags
)
}
# Attach customer managed policies to roles
resource "aws_iam_role_policy_attachment" "policy_attachments" {
for_each = local.role_policies
role = replace(each.key, format(":%s", each.value.name), "")
policy_arn = aws_iam_policy.policies[each.value.name].arn
# Terraform has no info that aws_iam_roles and aws_iam_policies
# must be run first in order to create the roles,
# so we must explicitly tell it.
depends_on = [
aws_iam_role.roles,
aws_iam_policy.policies,
]
}
# Attach policy ARNs to roles
resource "aws_iam_role_policy_attachment" "policy_arn_attachments" {
for_each = local.role_policy_arns
role = replace(each.key, format(":%s", each.value), "")
policy_arn = each.value
# Terraform has no info that aws_iam_roles must be run first in order to create the roles,
# so we must explicitly tell it.
depends_on = [aws_iam_role.roles]
}
# Attach inline policies to roles
resource "aws_iam_role_policy" "inline_policy_attachments" {
for_each = local.role_inline_policies
name = each.value.name
role = replace(each.key, format(":%s", each.value.name), "")
policy = templatefile(lookup(each.value, "file"), lookup(each.value, "vars"))
# Terraform has no info that aws_iam_roles must be run first in order to create the roles,
# so we must explicitly tell it.
depends_on = [aws_iam_role.roles]
}
# -------------------------------------------------------------------------------------------------
# 7. Instance profiles
# -------------------------------------------------------------------------------------------------
# Create roles
resource "aws_iam_instance_profile" "profiles" {
for_each = { for role in var.roles : role.name => role if role.instance_profile != null }
name = lookup(each.value, "instance_profile")
path = lookup(each.value, "path", null) == null ? var.role_path : lookup(each.value, "path")
role = lookup(each.value, "name")
tags = merge(
{
Name = lookup(each.value, "name")
},
var.tags
)
}