-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
150 lines (126 loc) · 4.79 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
# ----------------------------------------------------------------------------------------------------------------------
# REQUIRE A SPECIFIC TERRAFORM VERSION OR HIGHER
# ----------------------------------------------------------------------------------------------------------------------
terraform {
required_version = "< 0.12"
}
resource "aws_vpc" "vpc" {
cidr_block = "${var.cidr_block}"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "${var.name}"
terraform = "true"
}
}
resource "aws_subnet" "public" {
count = "${length(var.public_subnets)}"
map_public_ip_on_launch = true
vpc_id = "${aws_vpc.vpc.id}"
availability_zone = "${element(var.zones, count.index)}"
cidr_block = "${element(var.public_subnets, count.index)}"
tags = {
Name = "${format("pub-%s-%s-%d", var.name, element(var.zones, count.index), count.index / length(var.zones))}"
immutable_metadata = "{ \"purpose\": \"public-${var.name}\" }"
terraform = "true"
}
}
resource "aws_subnet" "private" {
count = "${length(var.private_subnets)}"
map_public_ip_on_launch = false
vpc_id = "${aws_vpc.vpc.id}"
availability_zone = "${element(var.zones, count.index)}"
cidr_block = "${element(var.private_subnets, count.index)}"
tags = {
Name = "${format("priv-%s-%s-%d", var.name, element(var.zones, count.index), count.index / length(var.zones))}"
immutable_metadata = "{ \"purpose\": \"private-${var.name}\" }"
terraform = "true"
}
}
resource "aws_subnet" "db" {
count = "${length(var.db_subnets)}"
map_public_ip_on_launch = false
vpc_id = "${aws_vpc.vpc.id}"
availability_zone = "${element(var.zones, count.index)}"
cidr_block = "${element(var.db_subnets, count.index)}"
tags = {
Name = "${format("db-%s-%s-%d", var.name, element(var.zones, count.index), count.index / length(var.zones))}"
terraform = "true"
}
}
# Create gateways
resource "aws_internet_gateway" "igw" {
vpc_id = "${aws_vpc.vpc.id}"
tags = {
Name = "igw-${var.name}"
terraform = "true"
}
}
resource "aws_eip" "nat_eips" {
count = "${length(var.zones)}"
vpc = "true"
}
resource "aws_nat_gateway" "nats" {
count = "${length(var.zones)}"
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
allocation_id = "${element(aws_eip.nat_eips.*.id, count.index)}"
depends_on = ["aws_internet_gateway.igw"]
}
# Create route tables
resource "aws_route_table" "public" {
vpc_id = "${aws_vpc.vpc.id}"
tags = {
Name = "rt-${var.name}-public"
terraform = "true"
}
}
resource "aws_route_table" "private" {
count = "${length(var.zones)}"
vpc_id = "${aws_vpc.vpc.id}"
tags = {
Name = "${format("rt-%s-private-%s-%d", var.name, element(var.zones, count.index), count.index / length(var.zones))}"
terraform = "true"
}
}
resource "aws_route_table" "db" {
count = "${length(var.zones)}"
vpc_id = "${aws_vpc.vpc.id}"
tags = {
Name = "${format("rt-%s-db-%s-%d", var.name, element(var.zones, count.index), count.index / length(var.zones))}"
terraform = "true"
}
}
# Associate gateways to route tables as default routes
resource "aws_route" "public" {
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.igw.id}"
route_table_id = "${aws_route_table.public.id}"
}
resource "aws_route" "private" {
count = "${length(var.zones)}"
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = "${element(aws_nat_gateway.nats.*.id, count.index)}"
route_table_id = "${element(aws_route_table.private.*.id, count.index)}"
}
resource "aws_route" "db" {
count = "${length(var.zones)}"
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = "${element(aws_nat_gateway.nats.*.id, count.index)}"
route_table_id = "${element(aws_route_table.db.*.id, count.index)}"
}
# Associate subnets to route tables
resource "aws_route_table_association" "public" {
count = "${length(var.public_subnets)}"
route_table_id = "${aws_route_table.public.id}"
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
}
resource "aws_route_table_association" "private" {
count = "${length(var.private_subnets)}"
route_table_id = "${element(aws_route_table.private.*.id, count.index)}"
subnet_id = "${element(aws_subnet.private.*.id, count.index)}"
}
resource "aws_route_table_association" "db" {
count = "${length(var.db_subnets)}"
route_table_id = "${element(aws_route_table.db.*.id, count.index)}"
subnet_id = "${element(aws_subnet.db.*.id, count.index)}"
}