-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvpc.tf
More file actions
129 lines (100 loc) · 2.74 KB
/
vpc.tf
File metadata and controls
129 lines (100 loc) · 2.74 KB
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
data "aws_availability_zones" "available"{
state = "available"
}
#vpc creation
resource "aws_vpc" "own-vpc" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
enable_dns_hostnames="true"
tags = {
Name = "own-vpc"
}
}
#internet gateway
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.own-vpc.id
tags = {
Name = "own-igw"
}
}
# creating subnet
resource "aws_subnet" "public" {
count=length(data.aws_availability_zones.available.names)
vpc_id = aws_vpc.own-vpc.id
cidr_block = element(var.pub_cidr,count.index)
map_public_ip_on_launch = "true"
availability_zone= element(data.aws_availability_zones.available.names, count.index)
tags = {
Name = "own-Pub-subnet-${count.index + 1}"
}
}
resource "aws_subnet" "private" {
count=length(data.aws_availability_zones.available.names)
vpc_id = aws_vpc.own-vpc.id
cidr_block = element(var.private_cidr,count.index)
# map_public_ip_on_launch = "true"
availability_zone= element(data.aws_availability_zones.available.names, count.index)
tags = {
Name = "own-private-subnet-${count.index + 1}"
}
}
resource "aws_subnet" "data" {
count=length(data.aws_availability_zones.available.names)
vpc_id = aws_vpc.own-vpc.id
cidr_block = element(var.data_cidr,count.index)
# map_public_ip_on_launch = "true"
availability_zone= element(data.aws_availability_zones.available.names, count.index)
tags = {
Name ="own-data-subnet-${count.index + 1}"
}
}
resource "aws_eip" "nat" {
vpc = true
tags = {
Name = "eip_nat"
}
}
resource "aws_nat_gateway" "nat-gateway" {
allocation_id = aws_eip.nat.id
subnet_id = aws_subnet.public[0].id
tags = {
Name = "gw NAT"
}
}
#routing tables
resource "aws_route_table" "public" {
vpc_id = aws_vpc.own-vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
tags = {
Name = "public_routing"
}
}
resource "aws_route_table" "private" {
vpc_id = aws_vpc.own-vpc.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat-gateway.id
}
tags = {
Name = "private_routing"
}
}
#route_association
resource "aws_route_table_association" "public" {
count=length(aws_subnet.public[*].id)
subnet_id = element(aws_subnet.public[*].id,count.index)
route_table_id = aws_route_table.public.id
}
resource "aws_route_table_association" "private" {
count=length(aws_subnet.private[*].id)
subnet_id = element(aws_subnet.private[*].id,count.index)
route_table_id = aws_route_table.private.id
}
resource "aws_route_table_association" "data" {
count=length(aws_subnet.data[*].id)
subnet_id = element(aws_subnet.data[*].id,count.index)
route_table_id = aws_route_table.private.id
}