forked from aws-ia/terraform-aws-amazon-vpc-lattice-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
283 lines (223 loc) · 10.4 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# --- root/main.tf ---
# ---------- VPC LATTICE SERVICE NETWORK ----------
resource "aws_vpclattice_service_network" "lattice_service_network" {
count = local.create_service_network ? 1 : 0
name = var.service_network.name
auth_type = try(var.service_network.auth_type, "NONE")
tags = module.tags.tags_aws
}
# Auth policy
resource "aws_vpclattice_auth_policy" "service_network_auth_policy" {
count = local.sn_auth_policy ? 1 : 0
resource_identifier = local.service_network_arn
policy = var.service_network.auth_policy
}
# Access log subscription
resource "aws_vpclattice_access_log_subscription" "service_network_cloudwatch_access_log" {
count = local.create_service_network && local.sn_access_log_cloudwatch ? 1 : 0
resource_identifier = local.service_network_arn
destination_arn = try(var.service_network.access_log_cloudwatch, null)
}
resource "aws_vpclattice_access_log_subscription" "service_network_s3_access_log" {
count = local.create_service_network && local.sn_access_log_s3 ? 1 : 0
resource_identifier = local.service_network_arn
destination_arn = try(var.service_network.access_log_s3, null)
}
resource "aws_vpclattice_access_log_subscription" "service_network_firehose_access_log" {
count = local.create_service_network && local.sn_access_log_firehose ? 1 : 0
resource_identifier = local.service_network_arn
destination_arn = try(var.service_network.access_log_firehose, null)
}
# ---------- VPC LATTICE VPC ASSOCIATIONS ----------
resource "aws_vpclattice_service_network_vpc_association" "lattice_vpc_association" {
for_each = var.vpc_associations
vpc_identifier = each.value.vpc_id
service_network_identifier = local.service_network
security_group_ids = try(each.value.security_group_ids, null)
tags = module.tags.tags_aws
}
# ---------- VPC LATTICE SERVICES ----------
# VPC Lattice Service
resource "aws_vpclattice_service" "lattice_service" {
for_each = {
for k, v in var.services : k => v
if try(v.name, null) != null
}
name = each.value.name
auth_type = try(each.value.auth_type, "NONE")
certificate_arn = try(each.value.certificate_arn, null)
custom_domain_name = try(each.value.custom_domain_name, null)
tags = module.tags.tags_aws
}
# Auth policy
resource "aws_vpclattice_auth_policy" "service_auth_policy" {
for_each = {
for k, v in var.services : k => v
if(try(v.auth_type, "NONE") == "AWS_IAM") && (try(v.auth_policy, null) != null)
}
resource_identifier = try(aws_vpclattice_service.lattice_service[each.key].arn, each.value.identifier)
policy = each.value.auth_policy
}
# VPC Lattice Service Association (only if service network is created or provided)
resource "aws_vpclattice_service_network_service_association" "lattice_service_association" {
for_each = {
for k, v in var.services : k => v
if local.create_service_association
}
service_identifier = try(each.value.identifier, aws_vpclattice_service.lattice_service[each.key].id)
service_network_identifier = local.service_network
tags = module.tags.tags_aws
}
# Data Source: Lattice Service. Used to obtain the information of the Services not created by the module, but its identifier was passed in var.services
data "aws_vpclattice_service" "lattice_service" {
for_each = { for k, v in var.services : k => v if contains(keys(v), "identifier") }
service_identifier = each.value.identifier
}
# Access log subscription (only if VPC Lattice service is created - not referenced)
resource "aws_vpclattice_access_log_subscription" "service_cloudwatch_access_log" {
for_each = {
for k, v in var.services : k => v
if contains(keys(v), "access_log_cloudwatch") && try(v.name, null) != null
}
resource_identifier = try(aws_vpclattice_service.lattice_service[each.key].arn, null)
destination_arn = try(each.value.access_log_cloudwatch, null)
}
resource "aws_vpclattice_access_log_subscription" "service_s3_access_log" {
for_each = {
for k, v in var.services : k => v
if contains(keys(v), "access_log_s3") && try(v.name, null) != null
}
resource_identifier = try(aws_vpclattice_service.lattice_service[each.key].arn, null)
destination_arn = try(each.value.access_log_s3, null)
}
resource "aws_vpclattice_access_log_subscription" "service_firehose_access_log" {
for_each = {
for k, v in var.services : k => v
if contains(keys(v), "access_log_firehose") && try(v.name, null) != null
}
resource_identifier = try(aws_vpclattice_service.lattice_service[each.key].arn, null)
destination_arn = try(each.value.access_log_firehose, null)
}
# ---------- AMAZON ROUTE 53 DNS CONFIGURATION ----------
# Alias record (IPv4)
resource "aws_route53_record" "custom_domain_name_a_record" {
for_each = local.services_with_dns_config
zone_id = try(each.value.hosted_zone_id, var.dns_configuration.hosted_zone_id)
name = try(var.services[each.key].custom_domain_name, data.aws_vpclattice_service.lattice_service[each.key].custom_domain_name)
type = "A"
alias {
name = try(aws_vpclattice_service.lattice_service[each.key].dns_entry[0].domain_name, data.aws_vpclattice_service.lattice_service[each.key].dns_entry[0].domain_name)
zone_id = try(aws_vpclattice_service.lattice_service[each.key].dns_entry[0].hosted_zone_id, data.aws_vpclattice_service.lattice_service[each.key].dns_entry[0].hosted_zone_id)
evaluate_target_health = false
}
}
# Alias record (IPv6)
resource "aws_route53_record" "custom_domain_name_aaaa_record" {
for_each = local.services_with_dns_config
zone_id = try(each.value.hosted_zone_id, var.dns_configuration.hosted_zone_id)
name = try(var.services[each.key].custom_domain_name, data.aws_vpclattice_service.lattice_service[each.key].custom_domain_name)
type = "AAAA"
alias {
name = try(aws_vpclattice_service.lattice_service[each.key].dns_entry[0].domain_name, data.aws_vpclattice_service.lattice_service[each.key].dns_entry[0].domain_name)
zone_id = try(aws_vpclattice_service.lattice_service[each.key].dns_entry[0].hosted_zone_id, data.aws_vpclattice_service.lattice_service[each.key].dns_entry[0].hosted_zone_id)
evaluate_target_health = false
}
}
# ---------- VPC LATTICE TARGET GROUPS ----------
# AWS Lambda Target Group (without config attribute)
resource "aws_vpclattice_target_group" "lambda_lattice_target_group" {
for_each = {
for k, v in var.target_groups : k => v
if v.type == "LAMBDA"
}
name = try(each.value.name, each.key)
type = each.value.type
config {
lambda_event_structure_version = try(each.value.config.lambda_event_structure_version, "V2")
}
tags = module.tags.tags_aws
}
# Other Target Groups
resource "aws_vpclattice_target_group" "lattice_target_group" {
for_each = {
for k, v in var.target_groups : k => v
if v.type != "LAMBDA"
}
name = try(each.value.name, each.key)
type = each.value.type
config {
port = try(each.value.config.port, null)
protocol = try(each.value.config.protocol, null)
vpc_identifier = try(each.value.config.vpc_identifier, null)
ip_address_type = try(each.value.config.ip_address_type, null)
protocol_version = try(each.value.config.protocol_version, null)
dynamic "health_check" {
for_each = each.value.type != "ALB" ? each.value.health_check[*] : []
content {
enabled = try(health_check.value.enabled, true)
health_check_interval_seconds = try(health_check.value.health_check_interval_seconds, null)
health_check_timeout_seconds = try(health_check.value.health_check_timeout_seconds, null)
healthy_threshold_count = try(health_check.value.healthy_threshold_count, null)
path = try(health_check.value.path, null)
port = try(health_check.value.port, null)
protocol = try(health_check.value.protocol, null)
protocol_version = try(health_check.value.protocol_version, null)
unhealthy_threshold_count = try(health_check.value.unhealthy_threshold_count, null)
matcher {
value = try(each.value.health_check.matcher, null)
}
}
}
}
tags = module.tags.tags_aws
}
# VPC Lattice Targets
module "targets" {
for_each = {
for k, v in var.target_groups : k => v
if contains(keys(v), "targets")
}
source = "./modules/targets"
target_type = each.value.type
target_group_identifier = try(aws_vpclattice_target_group.lambda_lattice_target_group[each.key].arn, aws_vpclattice_target_group.lattice_target_group[each.key].arn)
targets = each.value.targets
}
# ---------- LISTENERS AND RULES ----------
# VPC Lattice Listeners - rules are handled inside the "listeners" module
module "listeners" {
source = "./modules/listeners"
for_each = var.services
listener_information = try(each.value.listeners, {})
service_identifier = try(each.value.identifier, aws_vpclattice_service.lattice_service[each.key].id)
target_groups = local.target_group_ids
tags = module.tags.tags_aws
}
# ---------- AWS RESOURCE ACCESS MANAGER ----------
# Create AWS RAM Resource Share (if name provided)
resource "aws_ram_resource_share" "ram_resource_share" {
count = local.create_ram_resource_share ? 1 : 0
name = var.ram_share.resource_share_name
allow_external_principals = try(var.ram_share.allow_external_principals, false)
tags = module.tags.tags_aws
}
# AWS RAM principal association
resource "aws_ram_principal_association" "ram_principal_association" {
for_each = {
for k, v in local.principals_map : k => v
if local.config_ram_share
}
principal = each.value
resource_share_arn = local.resource_share_arn
}
# AWS RAM resource association - VPC Lattice service network
resource "aws_ram_resource_association" "ram_service_network_association" {
count = local.share_service_network ? 1 : 0
resource_arn = aws_vpclattice_service_network.lattice_service_network[0].arn
resource_share_arn = local.resource_share_arn
}
# AWS RAM resource association - VPC Lattice services
resource "aws_ram_resource_association" "ram_services_association" {
count = local.config_ram_share ? length(local.share_services) : 0
resource_arn = aws_vpclattice_service.lattice_service[local.share_services[count.index]].arn
resource_share_arn = local.resource_share_arn
}