generated from clowdhaus/terraform-aws-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.tf
196 lines (153 loc) · 5.8 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
data "aws_partition" "current" {}
locals {
partition = data.aws_partition.current.partition
}
################################################################################
# IAM Role Trust Policy
################################################################################
data "aws_iam_policy_document" "assume" {
count = var.create ? 1 : 0
statement {
effect = "Allow"
actions = [
"sts:AssumeRole",
"sts:TagSession",
]
principals {
type = "Service"
identifiers = ["pods.eks.amazonaws.com"]
}
dynamic "condition" {
for_each = var.trust_policy_conditions
content {
test = condition.value.test
values = condition.value.values
variable = condition.value.variable
}
}
}
dynamic "statement" {
for_each = var.trust_policy_statements
content {
sid = try(statement.value.sid, null)
actions = try(statement.value.actions, null)
not_actions = try(statement.value.not_actions, null)
effect = try(statement.value.effect, null)
resources = try(statement.value.resources, null)
not_resources = try(statement.value.not_resources, null)
dynamic "principals" {
for_each = try(statement.value.principals, [])
content {
type = principals.value.type
identifiers = principals.value.identifiers
}
}
dynamic "not_principals" {
for_each = try(statement.value.not_principals, [])
content {
type = not_principals.value.type
identifiers = not_principals.value.identifiers
}
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
values = condition.value.values
variable = condition.value.variable
}
}
}
}
}
################################################################################
# IAM Role
################################################################################
resource "aws_iam_role" "this" {
count = var.create ? 1 : 0
name = var.use_name_prefix ? null : var.name
name_prefix = var.use_name_prefix ? "${var.name}-" : null
path = var.path
description = var.description
assume_role_policy = data.aws_iam_policy_document.assume[0].json
max_session_duration = var.max_session_duration
permissions_boundary = var.permissions_boundary_arn
force_detach_policies = true
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "this" {
for_each = { for k, v in var.additional_policy_arns : k => v if var.create }
role = aws_iam_role.this[0].name
policy_arn = each.value
}
################################################################################
# Base IAM Policy Document
################################################################################
data "aws_iam_policy_document" "base" {
count = var.create ? 1 : 0
source_policy_documents = var.source_policy_documents
# Override will happen on each final document
# Here, it is only applicable for the custom policy document to avoid duplicate statements
override_policy_documents = var.attach_custom_policy ? var.override_policy_documents : []
dynamic "statement" {
for_each = var.policy_statements
content {
sid = try(statement.value.sid, null)
actions = try(statement.value.actions, null)
not_actions = try(statement.value.not_actions, null)
effect = try(statement.value.effect, null)
resources = try(statement.value.resources, null)
not_resources = try(statement.value.not_resources, null)
dynamic "principals" {
for_each = try(statement.value.principals, [])
content {
type = principals.value.type
identifiers = principals.value.identifiers
}
}
dynamic "not_principals" {
for_each = try(statement.value.not_principals, [])
content {
type = not_principals.value.type
identifiers = not_principals.value.identifiers
}
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
values = condition.value.values
variable = condition.value.variable
}
}
}
}
}
################################################################################
# Custom IAM Policy
################################################################################
resource "aws_iam_policy" "custom" {
count = var.create && var.attach_custom_policy ? 1 : 0
name = var.use_name_prefix ? null : var.name
name_prefix = var.use_name_prefix ? "${var.name}-" : null
path = var.path
description = var.custom_policy_description
policy = data.aws_iam_policy_document.base[0].json
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "custom" {
count = var.create && var.attach_custom_policy ? 1 : 0
role = aws_iam_role.this[0].name
policy_arn = aws_iam_policy.custom[0].arn
}
################################################################################
# Pod Identity Association
################################################################################
resource "aws_eks_pod_identity_association" "this" {
for_each = { for k, v in var.associations : k => v if var.create }
cluster_name = try(each.value.cluster_name, var.association_defaults.cluster_name)
namespace = try(each.value.namespace, var.association_defaults.namespace)
service_account = try(each.value.service_account, var.association_defaults.service_account)
role_arn = aws_iam_role.this[0].arn
tags = merge(var.tags, try(each.value.tags, var.association_defaults.tags, {}))
}