-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
118 lines (91 loc) · 3.15 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
data "aws_availability_zones" "available" {
state = "available"
}
# ---------------------------------------------------------------------------------------------------------------------
# DEPLOY A SIMPLE NETWORK
# The network has an internet gateway and two subnets - private and public - in the same availability zone.
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_vpc" "main" {
cidr_block = var.main_vpc_cidr
tags = {
Name = var.tag_name
}
}
resource "aws_internet_gateway" "main_gateway" {
vpc_id = aws_vpc.main.id
tags = {
Name = var.tag_name
}
}
resource "aws_subnet" "private" {
vpc_id = aws_vpc.main.id
cidr_block = var.private_subnet_cidr
map_public_ip_on_launch = false
tags = {
Name = var.tag_name
}
availability_zone = data.aws_availability_zones.available.names[0]
}
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = var.public_subnet_cidr
map_public_ip_on_launch = true
tags = {
Name = var.tag_name
}
availability_zone = data.aws_availability_zones.available.names[0]
}
# ---------------------------------------------------------------------------------------------------------------------
# CREATE AND ATTACH A ROUTING TABLE FOR THE PUBLIC NETWORK
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "91.189.0.0/24"
gateway_id = aws_internet_gateway.main_gateway.id
}
tags = {
Name = var.tag_name
}
}
resource "aws_route_table_association" "public" {
subnet_id = aws_subnet.public.id
route_table_id = aws_route_table.public.id
}
# ---------------------------------------------------------------------------------------------------------------------
# CREATE NAT GATEWAY FOR THE PRIVATE SUBNET
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_eip" "nat" {
vpc = true
tags = {
Name = var.tag_name
}
}
resource "aws_nat_gateway" "nat" {
allocation_id = aws_eip.nat.id
subnet_id = aws_subnet.public.id
depends_on = [aws_internet_gateway.main_gateway]
}
# ---------------------------------------------------------------------------------------------------------------------
# CREATE AND ATTACH A ROUTING TABLE FOR THE PRIVATE NETWORK
# ---------------------------------------------------------------------------------------------------------------------
resource "aws_route_table" "private" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat.id
}
tags = {
Name = var.tag_name
}
//lifecycle {
// postcondition {
// condition = self.tags["Name"] == var.tag_name
// error_message = "the 'Name' tag does not match ${var.tag_name}"
// }
//}
}
resource "aws_route_table_association" "private" {
subnet_id = aws_subnet.private.id
route_table_id = aws_route_table.private.id
}