-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpc.tf
145 lines (125 loc) · 3.49 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
resource "aws_vpc" "main" {
cidr_block = var.vpc-cidr
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "VPC for Main Network"
}
}
# we want to create subnets each in each availability Zone so lets get the availabilty zone details using data block
data "aws_availability_zones" "zones" {
state = "available"
}
# Creating Public Subnets from Main VPC
resource "aws_subnet" "public-1" {
cidr_block = var.pusub-1
vpc_id = aws_vpc.main.id
map_public_ip_on_launch = true
availability_zone = data.aws_availability_zones.zones.names[0]
tags = {
Name = "Public Subnet 1 for Main VPC"
}
}
resource "aws_subnet" "public-2" {
cidr_block = var.pusub-2
vpc_id = aws_vpc.main.id
map_public_ip_on_launch = true
availability_zone = data.aws_availability_zones.zones.names[1]
tags = {
Name = "Public Subnet 2 for Main VPC"
}
}
resource "aws_subnet" "public-3" {
cidr_block = var.pusub-3
map_public_ip_on_launch = true
availability_zone = data.aws_availability_zones.zones.names[2]
vpc_id = aws_vpc.main.id
tags = {
Name = "Public Subnet 3 for Main VPC"
}
}
# lets create a Internet GW to route public subnets traffic to internet
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "Internet GW for Public Access to Subnets"
}
}
resource "aws_route_table" "igw-rt" {
vpc_id = aws_vpc.main.id
route {
cidr_block = var.internet
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "Route Table for IG"
}
}
resource "aws_route_table_association" "igw-rt-a" {
subnet_id = aws_subnet.public-1.id
route_table_id = aws_route_table.igw-rt.id
}
resource "aws_route_table_association" "igw-rt-b" {
subnet_id = aws_subnet.public-2.id
route_table_id = aws_route_table.igw-rt.id
}
resource "aws_route_table_association" "igw-rt-c" {
subnet_id = aws_subnet.public-3.id
route_table_id = aws_route_table.igw-rt.id
}
#creating private subnet to launch to instances
resource "aws_subnet" "private-1" {
cidr_block = var.prisub-1
vpc_id = aws_vpc.main.id
availability_zone = data.aws_availability_zones.zones.names[0]
tags = {
Name = "Private Subnet 1 for Main VPC"
}
}
resource "aws_subnet" "private-2" {
cidr_block = var.prisub-2
vpc_id = aws_vpc.main.id
availability_zone = data.aws_availability_zones.zones.names[1]
tags = {
Name = "Private Subnet 2 for Main VPC"
}
}
resource "aws_subnet" "private-3" {
cidr_block = var.prisub-3
availability_zone = data.aws_availability_zones.zones.names[2]
vpc_id = aws_vpc.main.id
tags = {
Name = "Private Subnet 3 for Main VPC"
}
}
resource "aws_security_group" "web" {
name = "allow_http"
description = "Allow http inbound traffic"
vpc_id = aws_vpc.main.id
ingress {
description = "http from VPC"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [var.internet]
ipv6_cidr_blocks = ["::/0"]
}
ingress {
description = "ssh from VPC"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [var.internet]
#ipv6_cidr_blocks = ["::/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [var.internet]
# ipv6_cidr_blocks = ["::/0"]
}
tags = {
Name = "allow_http_ssh"
}
}