This repository has been archived by the owner on May 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpc.tf
108 lines (88 loc) · 3.41 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
#
# VPC Resources
# * VPC
# * Subnets
# * Internet Gateway
# * Route Table
#
resource "aws_vpc" "eks" {
count = var.vpc["create"] ? 1 : 0
cidr_block = var.vpc["cidr"]
enable_dns_hostnames = true
assign_generated_ipv6_cidr_block = true
tags = merge({ "Name" = "terraform-eks-${var.cluster-name}", "kubernetes.io/cluster/${var.cluster-name}" = "shared" }, local.common_tags, var.custom_tags)
}
resource "aws_subnet" "eks" {
count = var.vpc["create"] ? 3 : 0
availability_zone = data.aws_availability_zones.available.names[count.index]
cidr_block = cidrsubnet(
var.vpc["cidr"],
3,
length(aws_subnet.eks-private) + count.index,
)
vpc_id = aws_vpc.eks[0].id
map_public_ip_on_launch = true
tags = merge({ "Name" = "terraform-eks-node-${var.cluster-name}-public", "Public" = "yes", "kubernetes.io/cluster/${var.cluster-name}" = "shared", "kubernetes.io/role/elb" = "1" }, local.common_tags, var.custom_tags)
}
resource "aws_subnet" "eks-private" {
count = var.vpc["create"] ? 3 : 0
availability_zone = data.aws_availability_zones.available.names[count.index]
cidr_block = cidrsubnet(var.vpc["cidr"], 3, count.index)
vpc_id = aws_vpc.eks[0].id
tags = merge({ "Name" = "terraform-eks-node-${var.cluster-name}-private", "Public" = "no", "kubernetes.io/cluster/${var.cluster-name}" = "shared", "kubernetes.io/role/internal-elb" = "1" }, local.common_tags, var.custom_tags)
}
resource "aws_internet_gateway" "eks" {
count = var.vpc["create"] ? 1 : 0
vpc_id = aws_vpc.eks[0].id
tags = merge({ Name = "terraform-eks-${var.cluster-name}" }, local.common_tags, var.custom_tags)
}
resource "aws_route_table" "eks" {
count = var.vpc["create"] ? 1 : 0
vpc_id = aws_vpc.eks[0].id
tags = merge({ Name = "terraform-eks-${var.cluster-name}-public" }, local.common_tags, var.custom_tags)
}
resource "aws_route_table" "eks-private" {
count = var.vpc["create"] ? 3 : 0
vpc_id = aws_vpc.eks[0].id
tags = merge({ Name = "terraform-eks-${var.cluster-name}-private" }, local.common_tags, var.custom_tags)
}
resource "aws_route" "eks" {
count = var.vpc["create"] ? 1 : 0
route_table_id = aws_route_table.eks[0].id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.eks[0].id
}
resource "aws_route" "eks-private" {
count = var.vpc["create"] ? 3 : 0
route_table_id = aws_route_table.eks-private[count.index].id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.eks[count.index].id
}
resource "aws_route_table_association" "eks" {
count = var.vpc["create"] ? 3 : 0
subnet_id = aws_subnet.eks[count.index].id
route_table_id = aws_route_table.eks[0].id
}
resource "aws_route_table_association" "eks-private" {
count = var.vpc["create"] ? 3 : 0
subnet_id = aws_subnet.eks-private[count.index].id
route_table_id = aws_route_table.eks-private[count.index].id
}
resource "aws_eip" "eks" {
count = var.vpc["create"] ? 3 : 0
vpc = true
}
resource "aws_nat_gateway" "eks" {
count = var.vpc["create"] ? 3 : 0
allocation_id = aws_eip.eks[count.index].id
subnet_id = aws_subnet.eks[count.index].id
}
output "vpc-public-subnets" {
value = aws_subnet.eks.*.id
}
output "vpc-private-subnets" {
value = aws_subnet.eks-private.*.id
}
output "vpc-id" {
value = aws_vpc.eks.*.id
}