-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpc.tf
95 lines (80 loc) · 2.57 KB
/
vpc.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
# creating a new vpc with dns resolution support
resource "aws_vpc" "vpc_tony" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "Guilherme VPC"
BuildWith = "terraform"
}
}
# adding public subnet
resource "aws_subnet" "public_subnet" {
vpc_id = "${ aws_vpc.vpc_tony.id }"
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
availability_zone = "us-east-2a"
tags = {
Name = "Public Subnet"
BuildWith = "terraform"
}
}
# adding private subnet
resource "aws_subnet" "private_subnet" {
vpc_id = "${ aws_vpc.vpc_tony.id }"
cidr_block = "10.0.2.0/24"
availability_zone = "us-east-2a"
tags = {
Name = "Private Subnet"
BuildWith = "terraform"
}
}
# adding internet gateway for external communication
resource "aws_internet_gateway" "internet_gateway" {
vpc_id = "${ aws_vpc.vpc_tony.id }"
tags = {
Name = "Internet Gateway"
BuildWith = "terraform"
}
}
# create external route to IGW
resource "aws_route" "external_route" {
route_table_id = "${ aws_vpc.vpc_tony.main_route_table_id }"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${ aws_internet_gateway.internet_gateway.id }"
}
# adding an elastic IP
resource "aws_eip" "elastic_ip" {
vpc = true
depends_on = ["aws_internet_gateway.internet_gateway"]
}
# creating the NAT gateway
resource "aws_nat_gateway" "nat" {
allocation_id = "${ aws_eip.elastic_ip.id }"
subnet_id = "${ aws_subnet.public_subnet.id }"
depends_on = ["aws_internet_gateway.internet_gateway"]
}
# creating private route table
resource "aws_route_table" "private_route_table" {
vpc_id = "${ aws_vpc.vpc_tony.id }"
tags {
Name = "Private Subnet Route Table"
BuildWith = "terraform"
}
}
# adding private route table to nat
resource "aws_route" "private_route" {
route_table_id = "${ aws_route_table.private_route_table.id }"
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = "${ aws_nat_gateway.nat.id }"
}
# associate subnet public to public route table
resource "aws_route_table_association" "public_subnet_association" {
subnet_id = "${ aws_subnet.public_subnet.id }"
route_table_id = "${ aws_vpc.vpc_tony.main_route_table_id }"
}
# associate subnet private subnet to private route table
resource "aws_route_table_association" "private_subnet_association" {
subnet_id = "${ aws_subnet.private_subnet.id }"
route_table_id = "${ aws_route_table.private_route_table.id }"
}