-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.tf
161 lines (128 loc) · 5.44 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
# --- 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
}
# ---------- 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 try(v.name, null) == null }
service_identifier = each.value.identifier
}
# ---------- 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
}