-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
139 lines (121 loc) · 5.53 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
locals {
name = var.vpc_name
extra_tags = {
role = "vpc"
Name = local.name
}
vpc_tags = merge(var.common_tags, local.extra_tags, tomap({ "Name" : local.name }))
}
resource "aws_vpc" "primary" { # primary should be the main vpc, or hub.
count = var.create_vpc ? 1 : 0
cidr_block = var.vpc_cidr
enable_dns_support = true
enable_dns_hostnames = true
tags = local.vpc_tags
}
resource "aws_vpc_dhcp_options" "primary" {
count = var.create_vpc ? 1 : 0
domain_name = "service.consul"
domain_name_servers = ["127.0.0.1", "AmazonProvidedDNS"]
ntp_servers = ["127.0.0.1"]
netbios_name_servers = ["127.0.0.1"]
netbios_node_type = 2
tags = merge(var.common_tags, local.extra_tags, tomap({ "Name" : format("dhcpoptions_%s", local.name) }))
}
resource "aws_vpc_dhcp_options_association" "dns_resolver" {
count = var.create_vpc ? 1 : 0
vpc_id = local.vpc_id
dhcp_options_id = local.aws_vpc_dhcp_options_id
}
resource "aws_internet_gateway" "gw" {
count = var.create_vpc ? 1 : 0
vpc_id = local.vpc_id
tags = merge(var.common_tags, local.extra_tags, tomap({ "Name" : format("igw_%s", local.name) }))
}
locals {
vpc_id = element(concat(aws_vpc.primary.*.id, tolist([""])), 0)
aws_vpc_dhcp_options_id = element(concat(aws_vpc_dhcp_options.primary.*.id, tolist([""])), 0)
aws_internet_gateway = element(concat(aws_internet_gateway.gw.*.id, tolist([""])), 0)
private_subnets = var.create_vpc ? aws_subnet.private_subnet.*.id : []
subnet_names = [
for i in range(length(var.private_subnets)) : format("private%s_%s", i, local.name)
]
public_subnets = var.create_vpc ? aws_subnet.public_subnet.*.id : []
public_subnets_cidr_blocks = var.public_subnets
private_route_table_ids = aws_route_table.private.*.id
public_route_table_ids = aws_route_table.public.*.id
}
data "aws_availability_zones" "available" {
state = "available"
}
resource "aws_subnet" "public_subnet" {
depends_on = [aws_vpc.primary, aws_internet_gateway.gw]
count = var.create_vpc ? length(var.public_subnets) : 0
vpc_id = local.vpc_id
availability_zone = element(data.aws_availability_zones.available.names, count.index)
cidr_block = element(var.public_subnets, count.index)
map_public_ip_on_launch = true
tags = merge(var.common_tags, local.extra_tags, tomap({ "area" : "public" }), tomap({ "Name" : format("public%s_%s", count.index, local.name) }))
}
locals {
}
resource "aws_subnet" "private_subnet" {
depends_on = [aws_vpc.primary]
count = var.create_vpc ? length(var.private_subnets) : 0
vpc_id = local.vpc_id
availability_zone = element(data.aws_availability_zones.available.names, count.index)
cidr_block = element(var.private_subnets, count.index)
tags = merge(var.common_tags, local.extra_tags, tomap({"area": "private"}), tomap({"Name": format("private%s_%s", count.index, local.name)}))
}
resource "aws_eip" "nat" {
count = var.create_vpc && var.enable_nat_gateway && var.sleep == false ? 1 : 0
vpc = true
depends_on = [aws_internet_gateway.gw]
tags = merge(var.common_tags, local.extra_tags, tomap({"Name": format("%s", local.name)}))
}
resource "aws_nat_gateway" "gw" { # We use a single nat gateway currently to save cost.
count = var.create_vpc && var.enable_nat_gateway && var.sleep == false ? 1 : 0
allocation_id = aws_eip.nat[count.index].id
subnet_id = element(aws_subnet.public_subnet.*.id, count.index)
tags = merge(var.common_tags, local.extra_tags, tomap({"Name": format("%s", local.name)}))
}
resource "aws_route_table" "private" {
count = var.create_vpc ? 1 : 0
vpc_id = local.vpc_id
tags = merge(var.common_tags, local.extra_tags, tomap({ "area" : "private" }), tomap({ "Name" : "${local.name}_private" }))
}
resource "aws_route" "private_nat_gateway" {
count = var.create_vpc && var.enable_nat_gateway && var.sleep == false ? 1 : 0
route_table_id = element(concat(aws_route_table.private.*.id, tolist([""])), 0)
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = element(concat(aws_nat_gateway.gw.*.id, tolist([""])), 0)
timeouts {
create = "5m"
}
}
resource "aws_route_table" "public" {
count = var.create_vpc ? 1 : 0
vpc_id = local.vpc_id
tags = merge(var.common_tags, local.extra_tags, tomap({ "area" : "public" }), tomap({ "Name" : "${local.name}_public" }))
}
resource "aws_route" "public_gateway" {
count = var.create_vpc ? 1 : 0
route_table_id = element(concat(aws_route_table.public.*.id, tolist([""])), 0)
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw[count.index].id
timeouts {
create = "5m"
}
}
resource "aws_route_table_association" "private_associations" {
depends_on = [aws_vpc.primary, aws_subnet.private_subnet]
count = var.create_vpc ? length(var.public_subnets) : 0
subnet_id = element(aws_subnet.private_subnet.*.id, count.index)
route_table_id = element(aws_route_table.private.*.id, 0)
}
resource "aws_route_table_association" "public_associations" {
depends_on = [aws_vpc.primary, aws_subnet.public_subnet]
count = var.create_vpc ? length(var.public_subnets) : 0
subnet_id = element(aws_subnet.public_subnet.*.id, count.index)
route_table_id = element(aws_route_table.public.*.id, 0)
}