-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
207 lines (163 loc) · 5.41 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
resource "random_string" "random" {
length = 8
special = false
numeric = false
lower = true
}
data "aws_availability_zones" "available" {
state = "available"
}
locals {
create_vpc = (null == var.existing_vpc_id)
create_outside_subnet = local.outside_subnets_len > 0
create_local_subnet = local.local_subnets_len > 0
create_inside_subnet = local.inside_subnets_len > 0
create_workload_subnet = local.workload_subnets_len > 0
outside_subnets_len = length(var.outside_subnets)
local_subnets_len = length(var.local_subnets)
inside_subnets_len = length(var.inside_subnets)
workload_subnets_len = length(var.workload_subnets)
vpc_name = var.name != null ? var.name : format("%s-vpc", random_string.random.result)
vpc_id = var.existing_vpc_id != null ? var.existing_vpc_id : aws_vpc.this[0].id
az_names = length(var.az_names) > 0 ? var.az_names : slice(data.aws_availability_zones.available.names, 0, max(local.outside_subnets_len, local.local_subnets_len, local.inside_subnets_len, local.workload_subnets_len))
}
resource "aws_vpc" "this" {
count = local.create_vpc ? 1 : 0
cidr_block = var.vpc_cidr
instance_tenancy = var.vpc_instance_tenancy
enable_dns_hostnames = var.vpc_enable_dns_hostnames
enable_dns_support = var.vpc_enable_dns_support
enable_network_address_usage_metrics = var.vpc_enable_network_address_usage_metrics
tags = merge(
{
Name = local.vpc_name
},
var.tags,
)
}
resource "aws_subnet" "outside" {
count = local.create_outside_subnet ? local.outside_subnets_len : 0
availability_zone = element(local.az_names, count.index)
cidr_block = element(var.outside_subnets, count.index)
vpc_id = local.vpc_id
tags = merge(
{
Name = format("%s-outside-%s", local.vpc_name, element(local.az_names, count.index))
},
var.tags,
)
depends_on = [
aws_vpc.this,
]
}
resource "aws_subnet" "local" {
count = local.create_local_subnet ? local.local_subnets_len : 0
availability_zone = element(local.az_names, count.index)
cidr_block = element(var.local_subnets, count.index)
vpc_id = local.vpc_id
tags = merge(
{
Name = format("%s-local-%s", local.vpc_name, element(local.az_names, count.index))
},
var.tags,
)
depends_on = [
aws_vpc.this,
]
}
resource "aws_subnet" "inside" {
count = local.create_inside_subnet ? local.inside_subnets_len : 0
availability_zone = element(local.az_names, count.index)
cidr_block = element(var.inside_subnets, count.index)
vpc_id = local.vpc_id
tags = merge(
{
Name = format("%s-inside-%s", local.vpc_name, element(local.az_names, count.index))
},
var.tags,
)
depends_on = [
aws_vpc.this,
]
}
resource "aws_subnet" "workload" {
count = local.create_workload_subnet ? local.workload_subnets_len : 0
availability_zone = element(local.az_names, count.index)
cidr_block = element(var.workload_subnets, count.index)
vpc_id = local.vpc_id
tags = merge(
{
Name = format("%s-workload-%s", local.vpc_name, element(local.az_names, count.index))
},
var.tags,
)
depends_on = [
aws_vpc.this,
]
}
resource "aws_route_table" "outside" {
count = var.create_outside_route_table && (null != var.outside_subnets || null != var.local_subnets) ? 1 : 0
vpc_id = local.vpc_id
tags = merge(
{
Name = format("%s-outside-rt", local.vpc_name)
},
var.tags,
)
depends_on = [
aws_vpc.this,
]
}
resource "aws_route_table_association" "outside" {
count = var.create_outside_route_table ? local.outside_subnets_len : 0
subnet_id = element(aws_subnet.outside[*].id, count.index)
route_table_id = aws_route_table.outside[0].id
}
resource "aws_route_table_association" "local" {
count = var.create_outside_route_table ? local.local_subnets_len : 0
subnet_id = element(aws_subnet.local[*].id, count.index)
route_table_id = aws_route_table.outside[0].id
}
resource "aws_route" "internet_gateway" {
count = var.create_internet_gateway ? 1 : 0
route_table_id = aws_route_table.outside[0].id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.this[0].id
}
resource "aws_internet_gateway" "this" {
count = var.create_internet_gateway ? 1 : 0
vpc_id = local.vpc_id
tags = merge(
{
Name = format("%s-igw", local.vpc_name)
},
var.tags,
)
depends_on = [
aws_vpc.this,
]
}
resource "aws_default_security_group" "default" {
count = local.create_vpc ? 1 : 0
vpc_id = local.vpc_id
tags = merge(
{
Name = format("%s-default-sg", local.vpc_name)
},
var.tags,
)
depends_on = [
aws_vpc.this,
]
}
module "aws_vpc_sg" {
source = "./modules/aws-sg"
vpc_id = local.vpc_id
prefix = local.vpc_name
create_inside_security_group = var.create_inside_security_group
create_outside_security_group = var.create_outside_security_group
create_udp_security_group_rules = var.create_udp_security_group_rules
depends_on = [
aws_vpc.this,
]
}