-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpc.tf
81 lines (67 loc) · 2.11 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
# Create VPC
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/24"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "vpc ${var.tagNameDate}"
}
}
# Create Internet Gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vpc.id
tags = {
Name = "igw ${var.tagNameDate}"
}
}
# Create Public Subnets
resource "aws_subnet" "public" {
count = length(var.public_subnet_cidr_blocks)
vpc_id = aws_vpc.vpc.id
cidr_block = var.public_subnet_cidr_blocks[count.index]
availability_zone = var.availability_zones[count.index]
map_public_ip_on_launch = true
tags = {
Name = "Public_Subnet ${var.tagNameDate}_${count.index + 1}"
}
}
# Create Private Subnets
resource "aws_subnet" "private" {
count = length(var.private_subnet_cidr_blocks)
vpc_id = aws_vpc.vpc.id
cidr_block = var.private_subnet_cidr_blocks[count.index]
availability_zone = var.availability_zones[count.index % length(var.availability_zones)]
tags = {
Name = "Private_Subnet ${var.tagNameDate}_${count.index + 1}"
}
}
# Create Route Tables
resource "aws_route_table" "public" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = var.cidr_blocks[0]
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "Public_Route_Table ${var.tagNameDate}"
}
}
resource "aws_route_table" "private" {
count = length(var.availability_zones)
vpc_id = aws_vpc.vpc.id
tags = {
Name = "Private_Route_Table ${var.tagNameDate}_${count.index + 1}"
}
}
# Associate Public Subnets with Public Route Table
resource "aws_route_table_association" "public_assoc" {
count = length(var.public_subnet_cidr_blocks)
subnet_id = aws_subnet.public[count.index].id
route_table_id = aws_route_table.public.id
}
# Associate Private Subnets with Private Route Tables
resource "aws_route_table_association" "private_assoc" {
count = length(var.private_subnet_cidr_blocks)
subnet_id = aws_subnet.private[count.index].id
route_table_id = aws_route_table.private[count.index].id
}