-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
122 lines (92 loc) · 2.51 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
# VPC
resource "aws_vpc" "this" {
cidr_block = var.cidr
enable_dns_hostnames = var.enable_dns_hostnames
tags = {
Name = var.name
}
}
resource "aws_default_security_group" "this" {
vpc_id = aws_vpc.this.id
ingress {
protocol = -1
from_port = 0
to_port = 0
cidr_blocks = [aws_vpc.this.cidr_block]
}
egress {
protocol = -1
from_port = 0
to_port = 0
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_eip" "this" {
vpc = var.enable_eip
}
# Route tables
resource "aws_route_table" "private" {
vpc_id = aws_vpc.this.id
tags = { Name = "${var.name}-private-route-table" }
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.this.id
tags = { Name = "${var.name}-public-route-table" }
}
# public
resource "aws_subnet" "public" {
count = length(var.public_subnets)
vpc_id = aws_vpc.this.id
cidr_block = var.public_subnets[count.index]
availability_zone_id = var.azs[count.index]
tags = {
Name = "${var.name}-public-${var.azs[count.index]}"
}
}
resource "aws_route_table_association" "public" {
count = length(var.public_subnets)
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}
# private
resource "aws_subnet" "private" {
count = length(var.private_subnets)
vpc_id = aws_vpc.this.id
cidr_block = var.private_subnets[count.index]
availability_zone_id = var.azs[count.index]
tags = {
Name = "${var.name}-private-${var.azs[count.index]}"
}
}
resource "aws_route_table_association" "private" {
count = length(var.private_subnets)
subnet_id = aws_subnet.private[count.index].id
route_table_id = aws_route_table.private.id
}
# Gateways
resource "aws_nat_gateway" "this" {
count = length(var.public_subnets) > 0 ? 1 : 0
allocation_id = aws_eip.this.id
subnet_id = aws_subnet.public[count.index].id
tags = {
Name = "${var.name}-nat-gateway"
}
}
resource "aws_internet_gateway" "this" {
count = length(var.public_subnets) > 0 ? 1 : 0
vpc_id = aws_vpc.this.id
tags = {
Name = "${var.name}-internet-gateway"
}
}
# Routes
resource "aws_route" "public" {
route_table_id = aws_route_table.public.id
gateway_id = aws_internet_gateway.this[0].id
destination_cidr_block = "0.0.0.0/0"
}
resource "aws_route" "private" {
route_table_id = aws_route_table.private.id
nat_gateway_id = aws_nat_gateway.this[0].id
destination_cidr_block = "0.0.0.0/0"
}