forked from dhaiducek/ocp311_tf_aws
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vpc.tf
124 lines (106 loc) · 2.8 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
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
# Create a VPC
resource "aws_vpc" "ocp311" {
cidr_block = var.vpc_cidr
enable_dns_support = true
enable_dns_hostnames = true
tags = merge(
local.common_tags,
tomap({
"Name" = "${local.cluster_id}-vpc"
})
)
}
# Create an Internet Gateway
resource "aws_internet_gateway" "ocp311" {
vpc_id = aws_vpc.ocp311.id
tags = merge(
local.common_tags,
tomap({
"Name" = "${local.cluster_id}-internet-gateway"
})
)
}
# Create a public subnet
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.ocp311.id
cidr_block = var.public_subnet_cidr
availability_zone = data.aws_availability_zones.zones.names[0]
tags = merge(
local.common_tags,
tomap({
"Name" = "${local.cluster_id}-public-subnet"
})
)
}
# Create a route table allowing all addresses access to the Internet Gateway
resource "aws_route_table" "public_route_table" {
vpc_id = aws_vpc.ocp311.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.ocp311.id
}
tags = merge(
local.common_tags,
tomap({
"Name" = "${local.cluster_id}-public-route-table"
})
)
}
# Associate the route table with the public subnet
resource "aws_route_table_association" "public-subnet" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public_route_table.id
}
# Create an Elastic IP for NAT gateway
resource "aws_eip" "natgateway_eip" {
vpc = true
tags = merge(
local.common_tags,
tomap({
"Name" = "${local.cluster_id}-nat-gateway-eip"
})
)
}
# Create the NAT gateway
resource "aws_nat_gateway" "private_natgateway" {
allocation_id = aws_eip.natgateway_eip.id
subnet_id = aws_subnet.public_subnet.id
depends_on = [aws_internet_gateway.ocp311]
tags = merge(
local.common_tags,
tomap({
"Name" = "${local.cluster_id}-private-nat-gateway"
})
)
}
# Create a private subnet
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.ocp311.id
cidr_block = var.private_subnet_cidr
availability_zone = data.aws_availability_zones.zones.names[0]
tags = merge(
local.common_tags,
tomap({
"Name" = "${local.cluster_id}-private-subnet"
})
)
}
# Create a route table allowing private subnet access to the NAT Gateway
resource "aws_route_table" "private_route_table" {
vpc_id = aws_vpc.ocp311.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.private_natgateway.id
}
tags = merge(
local.common_tags,
tomap({
"Name" = "${local.cluster_id}-private-route-table"
})
)
}
# Associate the route table with the private subnet
resource "aws_route_table_association" "private-subnet" {
subnet_id = aws_subnet.private_subnet.id
route_table_id = aws_route_table.private_route_table.id
}