-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
66 lines (53 loc) · 1.43 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
/**
* Create groups
*/
resource "aws_iam_group" "groups" {
for_each = var.groups
name = each.key
}
/**
* Attach policies to groups
*/
locals {
// Create a list of group/policy association in the format [{group, policy}]
policy_groups = [
for group, prop in var.groups : [
for policy in prop["policies"] : merge({ group = group }, { policy = policy })
] if length(lookup(prop, "policies", [])) > 0
]
}
resource "aws_iam_group_policy_attachment" "group_policies" {
for_each = zipmap([for k, v in flatten(local.policy_groups) : k], flatten(local.policy_groups))
group = aws_iam_group.groups[each.value.group].name
policy_arn = each.value.policy
}
/**
* Create assume role policies for groups
*/
locals {
// Create a list of group/role association in the format {group => [role1, role2]}]
assumable_groups = {
for group, prop in var.groups :
group => prop["assume_roles"] if length(lookup(prop, "assume_roles", [])) > 0
}
}
resource "aws_iam_group_policy" "assume_role_policies" {
for_each = local.assumable_groups
name = "AssumeRoles"
group = aws_iam_group.groups[each.key].name
policy = <<-EOF
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": [
%{for index, role in each.value}
"${role}"
%{if(index != length(each.value) - 1)},%{endif}
%{endfor}
]
}
}
EOF
}