-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpc.tf
104 lines (83 loc) · 2.46 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
provider "aws" {
region = "us-east-1"
access_key = var.access_key
secret_key = var.secret_key
}
/*Networking*/
/*START*/
resource "aws_vpc" "main" {
cidr_block = "172.16.0.0/16"
tags = {
Name = "AWS-EC2-Basic-Architecture"
}
}
resource "aws_subnet" "public_subnets" {
count = length(var.public_subnet)
vpc_id = aws_vpc.main.id
cidr_block = element(var.public_subnet, count.index)
availability_zone = element(var.azs, count.index)
tags = {
Name = "Public Sub AWS-EC2-Basic-Architecture "
}
}
resource "aws_subnet" "private_subnets" {
count = length(var.private_subnet)
vpc_id = aws_vpc.main.id
cidr_block = element(var.private_subnet, count.index)
availability_zone = element(var.azs, count.index)
tags = {
Name = "Private Sub AWS-EC2-Basic-Architecture"
}
}
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "AWS-EC2-Basic-Architecture"
}
}
resource "aws_route_table" "public_rt" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
tags = {
Name = "Route Table Public"
}
}
resource "aws_route_table" "private_rt" {
vpc_id = aws_vpc.main.id
count = length(aws_nat_gateway.nat-gateway)
route {
cidr_block = "0.0.0.0/0"
gateway_id = element(aws_nat_gateway.nat-gateway[*].id, count.index)
}
tags = {
Name = "Route Table Private Sub NATGW"
}
}
resource "aws_route_table_association" "pub_associate" {
count = length(var.public_subnet)
subnet_id = element(aws_subnet.public_subnets[*].id, count.index)
route_table_id = aws_route_table.public_rt.id
}
resource "aws_route_table_association" "private_associate" {
count = length(var.private_subnet)
subnet_id = element(aws_subnet.private_subnets[*].id, count.index)
route_table_id = aws_route_table.private_rt[count.index].id
}
resource "aws_eip" "elasticip" {
count = length(var.private_subnet)
tags = {
Name = "AWS-EC2-Basic-Architecture ElasticIP"
}
}
resource "aws_nat_gateway" "nat-gateway" {
count = length(var.private_subnet)
allocation_id = element(aws_eip.elasticip[*].id, count.index)
subnet_id = element(aws_subnet.private_subnets[*].id, count.index)
tags = {
Name = "AWS-EC2-Basic-Architecture NATGW"
}
}
/*END*/