-
-
Notifications
You must be signed in to change notification settings - Fork 132
/
main.tf
159 lines (133 loc) · 5.12 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
locals {
enabled = module.this.enabled
security_group_enabled = local.enabled && var.create_security_group
dns_name = format("%s.efs.%s.amazonaws.com", join("", aws_efs_file_system.default[*].id), var.region)
# Returning null in the lookup function gives type errors and is not omitting the parameter.
# This work around ensures null is returned.
posix_users = {
for k, v in var.access_points :
k => lookup(var.access_points[k], "posix_user", {})
}
secondary_gids = {
for k, v in var.access_points :
k => lookup(local.posix_users[k], "secondary_gids", null)
}
}
resource "aws_efs_file_system" "default" {
#bridgecrew:skip=BC_AWS_GENERAL_48: BC complains about not having an AWS Backup plan. We ignore this because this can be done outside of this module.
count = local.enabled ? 1 : 0
tags = module.this.tags
availability_zone_name = var.availability_zone_name
encrypted = var.encrypted
kms_key_id = var.kms_key_id
performance_mode = var.performance_mode
provisioned_throughput_in_mibps = var.provisioned_throughput_in_mibps
throughput_mode = var.throughput_mode
dynamic "lifecycle_policy" {
for_each = length(var.transition_to_ia) > 0 ? [1] : []
content {
transition_to_ia = try(var.transition_to_ia[0], null)
}
}
dynamic "lifecycle_policy" {
for_each = length(var.transition_to_archive) > 0 ? [1] : []
content {
transition_to_archive = try(var.transition_to_archive[0], null)
}
}
dynamic "lifecycle_policy" {
for_each = length(var.transition_to_primary_storage_class) > 0 ? [1] : []
content {
transition_to_primary_storage_class = try(var.transition_to_primary_storage_class[0], null)
}
}
}
resource "aws_efs_mount_target" "default" {
count = local.enabled && length(var.subnets) > 0 ? length(var.subnets) : 0
file_system_id = join("", aws_efs_file_system.default[*].id)
ip_address = var.mount_target_ip_address
subnet_id = var.subnets[count.index]
security_groups = compact(
(concat(
[module.security_group.id],
var.associated_security_group_ids
))
)
}
resource "aws_efs_access_point" "default" {
for_each = local.enabled ? var.access_points : {}
file_system_id = join("", aws_efs_file_system.default[*].id)
dynamic "posix_user" {
for_each = local.posix_users[each.key] != null ? ["true"] : []
content {
gid = local.posix_users[each.key]["gid"]
uid = local.posix_users[each.key]["uid"]
secondary_gids = local.secondary_gids[each.key] != null ? split(",", local.secondary_gids[each.key]) : null
}
}
root_directory {
path = "/${each.key}"
dynamic "creation_info" {
for_each = try(var.access_points[each.key]["creation_info"]["gid"], "") != "" ? ["true"] : []
content {
owner_gid = var.access_points[each.key]["creation_info"]["gid"]
owner_uid = var.access_points[each.key]["creation_info"]["uid"]
permissions = var.access_points[each.key]["creation_info"]["permissions"]
}
}
}
tags = module.this.tags
}
module "security_group" {
source = "cloudposse/security-group/aws"
version = "2.2.0"
enabled = local.security_group_enabled
security_group_name = var.security_group_name
create_before_destroy = var.security_group_create_before_destroy
security_group_create_timeout = var.security_group_create_timeout
security_group_delete_timeout = var.security_group_delete_timeout
security_group_description = var.security_group_description
allow_all_egress = var.allow_all_egress
rules = var.additional_security_group_rules
rule_matrix = [
{
source_security_group_ids = local.allowed_security_group_ids
cidr_blocks = var.allowed_cidr_blocks
rules = [
{
key = "in"
type = "ingress"
from_port = 2049
to_port = 2049
protocol = "tcp"
description = "Allow ingress EFS traffic"
}
]
}
]
vpc_id = var.vpc_id
context = module.this.context
}
module "dns" {
source = "cloudposse/route53-cluster-hostname/aws"
version = "0.13.0"
enabled = local.enabled && length(var.zone_id) > 0
dns_name = var.dns_name == "" ? module.this.id : var.dns_name
ttl = 60
zone_id = try(var.zone_id[0], null)
records = [local.dns_name]
context = module.this.context
}
resource "aws_efs_backup_policy" "policy" {
count = module.this.enabled ? 1 : 0
file_system_id = join("", aws_efs_file_system.default[*].id)
backup_policy {
status = var.efs_backup_policy_enabled ? "ENABLED" : "DISABLED"
}
}
resource "aws_efs_file_system_policy" "policy" {
count = local.enabled && length(var.efs_file_system_policy) > 0 ? 1 : 0
file_system_id = aws_efs_file_system.default[0].id
bypass_policy_lockout_safety_check = var.bypass_policy_lockout_safety_check
policy = var.efs_file_system_policy
}