-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
119 lines (104 loc) · 2.92 KB
/
main.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
resource "aws_vpc" "vpc" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
enable_dns_support = true
tags = {
"Name" = var.name
}
}
resource "aws_vpc_dhcp_options" "vpc" {
domain_name = "${var.region}.compute.internal"
domain_name_servers = [
"AmazonProvidedDNS"]
tags = {
"Name" = var.name
}
}
resource "aws_vpc_dhcp_options_association" "vpc" {
dhcp_options_id = aws_vpc_dhcp_options.vpc.id
vpc_id = aws_vpc.vpc.id
}
resource "aws_eip" "elastic_ips" {
for_each = local.private_cidrs
tags = {
"Name" = "${var.region}${each.key}.${var.name}"
}
domain = "vpc"
}
resource "aws_subnet" "private" {
for_each = local.private_cidrs
availability_zone = "${var.region}${each.key}"
cidr_block = each.value
vpc_id = aws_vpc.vpc.id
tags = merge({
"Name" = "${var.region}${each.key}.${var.name}"
"SubnetType" = "Private"
"kubernetes.io/role/internal-elb" = "1"
}, var.additional_private_subnet_tags)
lifecycle {
ignore_changes = [
tags,
]
}
}
resource "aws_subnet" "public" {
for_each = local.public_cidrs
availability_zone = "${var.region}${each.key}"
cidr_block = each.value
vpc_id = aws_vpc.vpc.id
tags = merge({
"Name" = "public-${var.region}${each.key}.${var.name}"
"SubnetType" = "Utility"
"kubernetes.io/role/elb" = "1"
}, var.additional_public_subnet_tags)
lifecycle {
ignore_changes = [
tags,
]
}
}
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vpc.id
}
resource "aws_nat_gateway" "nat_gw" {
for_each = local.create_private_subnets ? aws_subnet.public : {}
allocation_id = aws_eip.elastic_ips[each.key].id
subnet_id = each.value.id
tags = {
"Name" = "${var.region}${each.key}.${var.name}"
}
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.vpc.id
tags = {
"Name" = var.name
}
}
resource "aws_route_table" "private" {
for_each = aws_subnet.private
vpc_id = aws_vpc.vpc.id
tags = {
"Name" = "private-${var.region}${each.key}.${var.name}"
}
}
resource "aws_route_table_association" "private" {
for_each = aws_subnet.private
subnet_id = each.value.id
route_table_id = aws_route_table.private[each.key].id
}
resource "aws_route_table_association" "public" {
for_each = aws_subnet.public
subnet_id = each.value.id
route_table_id = aws_route_table.public.id
}
resource "aws_route" "igw_route" {
route_table_id = aws_route_table.public.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
resource "aws_route" "route_private" {
for_each = aws_nat_gateway.nat_gw
route_table_id = aws_route_table.private[each.key].id
nat_gateway_id = aws_nat_gateway.nat_gw[each.key].id
destination_cidr_block = "0.0.0.0/0"
}