-
Notifications
You must be signed in to change notification settings - Fork 1
/
vpc.tf
106 lines (86 loc) · 3.07 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
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = var.enable_dns_hostnames
enable_dns_support = var.enable_dns_support
tags = merge(var.common_tags,
{ "Name" : "${var.project_name}-${var.environment}-vpc" }
)
}
resource "aws_subnet" "publicsubnets" {
count = length(var.public_subnet_cidrs)
vpc_id = aws_vpc.main.id
cidr_block = element(var.public_subnet_cidrs, count.index)
availability_zone = data.aws_availability_zones.availability_zones.names[count.index]
map_public_ip_on_launch = true
tags = merge(var.common_tags,
{
"Name" : "${var.project_name}-${var.environment}-vpc-publicsubnet-${count.index + 1}",
"kubernetes.io/role/elb" : "1"
}
)
}
resource "aws_subnet" "privatesubnets" {
count = length(var.public_subnet_cidrs)
vpc_id = aws_vpc.main.id
cidr_block = element(var.private_subnet_cidrs, count.index)
availability_zone = data.aws_availability_zones.availability_zones.names[count.index]
tags = merge(var.common_tags,
{
"Name" : "${var.project_name}-${var.environment}-vpc-privatesubnet-${count.index + 1}",
"kubernetes.io/role/internal-elb" : "1"
}
)
}
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = merge(var.common_tags,
{ "Name" : "${var.project_name}-${var.environment}-vpc-igw" }
)
}
resource "aws_route_table" "publicroute" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
tags = merge(var.common_tags,
{ "Name" : "${var.project_name}-${var.environment}-vpc-publicrt" }
)
}
resource "aws_route_table_association" "publicrt_association" {
count = length(var.public_subnet_cidrs)
route_table_id = aws_route_table.publicroute.id
subnet_id = aws_subnet.publicsubnets[count.index].id
}
resource "aws_eip" "eips" {
count = length(var.public_subnet_cidrs)
domain = "vpc"
tags = merge(var.common_tags,
{ "Name" : "${var.project_name}-${var.environment}-vpc-eip-${count.index + 1}" }
)
}
resource "aws_nat_gateway" "natgateways" {
count = length(var.public_subnet_cidrs)
allocation_id = aws_eip.eips[count.index].id
subnet_id = aws_subnet.publicsubnets[count.index].id
tags = merge(var.common_tags,
{ "Name" : "${var.project_name}-${var.environment}-vpc-natgateway-${count.index + 1}" }
)
depends_on = [aws_internet_gateway.main]
}
resource "aws_route_table" "privateroutes" {
count = length(var.private_subnet_cidrs)
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.natgateways[count.index].id
}
tags = merge(var.common_tags,
{ "Name" : "${var.project_name}-${var.environment}-vpc-privateroute-${count.index + 1}" }
)
}
resource "aws_route_table_association" "privateroutes_association" {
count = length(var.private_subnet_cidrs)
subnet_id = aws_subnet.privatesubnets[count.index].id
route_table_id = aws_route_table.privateroutes[count.index].id
}