generated from clowdhaus/terraform-aws-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
main.tf
248 lines (193 loc) · 7.82 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
################################################################################
# File System
################################################################################
resource "aws_efs_file_system" "this" {
count = var.create ? 1 : 0
availability_zone_name = var.availability_zone_name
creation_token = var.creation_token
performance_mode = var.performance_mode
encrypted = var.encrypted
kms_key_id = var.kms_key_arn
provisioned_throughput_in_mibps = var.provisioned_throughput_in_mibps
throughput_mode = var.throughput_mode
dynamic "lifecycle_policy" {
for_each = [for k, v in var.lifecycle_policy : { (k) = v }]
content {
transition_to_ia = try(lifecycle_policy.value.transition_to_ia, null)
transition_to_primary_storage_class = try(lifecycle_policy.value.transition_to_primary_storage_class, null)
}
}
tags = merge(
var.tags,
{ Name = var.name },
)
}
################################################################################
# File System Policy
################################################################################
data "aws_iam_policy_document" "policy" {
count = var.create && var.attach_policy ? 1 : 0
source_policy_documents = var.source_policy_documents
override_policy_documents = 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, [aws_efs_file_system.this[0].arn], 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, statement.value.condition, [])
content {
test = condition.value.test
values = condition.value.values
variable = condition.value.variable
}
}
}
}
dynamic "statement" {
for_each = var.deny_nonsecure_transport ? [1] : []
content {
sid = "NonSecureTransport"
effect = "Deny"
actions = ["*"]
resources = [aws_efs_file_system.this[0].arn]
principals {
type = "AWS"
identifiers = ["*"]
}
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = ["false"]
}
}
}
}
resource "aws_efs_file_system_policy" "this" {
count = var.create && var.attach_policy ? 1 : 0
file_system_id = aws_efs_file_system.this[0].id
bypass_policy_lockout_safety_check = var.bypass_policy_lockout_safety_check
policy = data.aws_iam_policy_document.policy[0].json
}
################################################################################
# Mount Target(s)
################################################################################
resource "aws_efs_mount_target" "this" {
for_each = { for k, v in var.mount_targets : k => v if var.create }
file_system_id = aws_efs_file_system.this[0].id
ip_address = try(each.value.ip_address, null)
security_groups = var.create_security_group ? concat([aws_security_group.this[0].id], try(each.value.security_groups, [])) : try(each.value.security_groups, null)
subnet_id = each.value.subnet_id
}
################################################################################
# Security Group
################################################################################
locals {
security_group_name = try(coalesce(var.security_group_name, var.name), "")
}
resource "aws_security_group" "this" {
count = var.create && var.create_security_group && length(var.mount_targets) > 0 ? 1 : 0
name = var.security_group_use_name_prefix ? null : local.security_group_name
name_prefix = var.security_group_use_name_prefix ? "${local.security_group_name}-" : null
description = var.security_group_description
revoke_rules_on_delete = true
vpc_id = var.security_group_vpc_id
tags = var.tags
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group_rule" "this" {
for_each = { for k, v in var.security_group_rules : k => v if var.create && var.create_security_group }
security_group_id = aws_security_group.this[0].id
description = try(each.value.description, null)
type = try(each.value.type, "ingress")
from_port = try(each.value.from_port, 2049)
to_port = try(each.value.to_port, 2049)
protocol = try(each.value.protocol, "tcp")
cidr_blocks = try(each.value.cidr_blocks, null)
ipv6_cidr_blocks = try(each.value.ipv6_cidr_blocks, null)
prefix_list_ids = try(each.value.prefix_list_ids, null)
self = try(each.value.self, null)
source_security_group_id = try(each.value.source_security_group_id, null)
lifecycle {
create_before_destroy = true
}
}
################################################################################
# Access Point(s)
################################################################################
resource "aws_efs_access_point" "this" {
for_each = { for k, v in var.access_points : k => v if var.create }
file_system_id = aws_efs_file_system.this[0].id
dynamic "posix_user" {
for_each = try([each.value.posix_user], [])
content {
gid = posix_user.value.gid
uid = posix_user.value.uid
secondary_gids = try(posix_user.value.secondary_gids, null)
}
}
dynamic "root_directory" {
for_each = try([each.value.root_directory], [])
content {
path = try(root_directory.value.path, null)
dynamic "creation_info" {
for_each = try([root_directory.value.creation_info], [])
content {
owner_gid = creation_info.value.owner_gid
owner_uid = creation_info.value.owner_uid
permissions = creation_info.value.permissions
}
}
}
}
tags = merge(
var.tags,
try(each.value.tags, {}),
{ Name = try(each.value.name, each.key) },
)
}
################################################################################
# Backup Policy
################################################################################
resource "aws_efs_backup_policy" "this" {
count = var.create && var.create_backup_policy ? 1 : 0
file_system_id = aws_efs_file_system.this[0].id
backup_policy {
status = var.enable_backup_policy ? "ENABLED" : "DISABLED"
}
}
################################################################################
# Replication Configuration
################################################################################
resource "aws_efs_replication_configuration" "this" {
count = var.create && var.create_replication_configuration ? 1 : 0
source_file_system_id = aws_efs_file_system.this[0].id
dynamic "destination" {
for_each = [var.replication_configuration_destination]
content {
availability_zone_name = try(destination.value.availability_zone_name, null)
kms_key_id = try(destination.value.kms_key_id, null)
region = try(destination.value.region, null)
}
}
}