forked from paulo-tinoco/terraform-policies-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
42 lines (34 loc) · 1.05 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
locals {
policies = {
for policy in var.policies : policy.iam_reference => {
statements = {
for index, statement in policy.statements : "statement-${index}" => {
actions = statement.actions
resources = statement.resources
}
}
}
}
policies_user = {
for policy in var.policies : policy.iam_reference => {} if policy.iam_type == "user"
}
policies_role = {
for policy in var.policies : policy.iam_reference => {} if policy.iam_type == "role"
}
}
resource "aws_iam_policy" "policies" {
for_each = local.policies
name = "policy-${each.key}"
description = "Policy for ${each.key}"
policy = data.aws_iam_policy_document.policies[each.key].json
}
resource "aws_iam_user_policy_attachment" "policies" {
for_each = local.policies_user
user = each.key
policy_arn = aws_iam_policy.policies[each.key].arn
}
resource "aws_iam_role_policy_attachment" "policies" {
for_each = local.policies_role
role = each.key
policy_arn = aws_iam_policy.policies[each.key].arn
}