2017-03-02 01:25:58 +08:00
|
|
|
resource "aws_vpc" "cluster-vpc" {
|
2020-06-05 15:05:43 +08:00
|
|
|
cidr_block = var.aws_vpc_cidr_block
|
2017-03-02 01:25:58 +08:00
|
|
|
|
2019-04-08 17:22:24 +08:00
|
|
|
#DNS Related Entries
|
|
|
|
enable_dns_support = true
|
|
|
|
enable_dns_hostnames = true
|
2017-03-02 01:25:58 +08:00
|
|
|
|
2021-05-12 22:34:17 +08:00
|
|
|
tags = merge(var.default_tags, tomap({
|
|
|
|
Name = "kubernetes-${var.aws_cluster_name}-vpc"
|
|
|
|
}))
|
2017-03-02 01:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_eip" "cluster-nat-eip" {
|
2020-06-05 15:05:43 +08:00
|
|
|
count = length(var.aws_cidr_subnets_public)
|
2019-04-08 17:22:24 +08:00
|
|
|
vpc = true
|
2017-03-02 01:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_internet_gateway" "cluster-vpc-internetgw" {
|
2020-06-05 15:05:43 +08:00
|
|
|
vpc_id = aws_vpc.cluster-vpc.id
|
2017-03-02 01:25:58 +08:00
|
|
|
|
2021-05-12 22:34:17 +08:00
|
|
|
tags = merge(var.default_tags, tomap({
|
|
|
|
Name = "kubernetes-${var.aws_cluster_name}-internetgw"
|
|
|
|
}))
|
2017-03-02 01:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "cluster-vpc-subnets-public" {
|
2020-06-05 15:05:43 +08:00
|
|
|
vpc_id = aws_vpc.cluster-vpc.id
|
|
|
|
count = length(var.aws_avail_zones)
|
|
|
|
availability_zone = element(var.aws_avail_zones, count.index)
|
|
|
|
cidr_block = element(var.aws_cidr_subnets_public, count.index)
|
2017-03-02 01:25:58 +08:00
|
|
|
|
2021-05-12 22:34:17 +08:00
|
|
|
tags = merge(var.default_tags, tomap({
|
|
|
|
Name = "kubernetes-${var.aws_cluster_name}-${element(var.aws_avail_zones, count.index)}-public"
|
|
|
|
"kubernetes.io/cluster/${var.aws_cluster_name}" = "member"
|
|
|
|
}))
|
2017-03-02 01:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_nat_gateway" "cluster-nat-gateway" {
|
2020-06-05 15:05:43 +08:00
|
|
|
count = length(var.aws_cidr_subnets_public)
|
|
|
|
allocation_id = element(aws_eip.cluster-nat-eip.*.id, count.index)
|
|
|
|
subnet_id = element(aws_subnet.cluster-vpc-subnets-public.*.id, count.index)
|
2017-03-02 01:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "cluster-vpc-subnets-private" {
|
2020-06-05 15:05:43 +08:00
|
|
|
vpc_id = aws_vpc.cluster-vpc.id
|
|
|
|
count = length(var.aws_avail_zones)
|
|
|
|
availability_zone = element(var.aws_avail_zones, count.index)
|
|
|
|
cidr_block = element(var.aws_cidr_subnets_private, count.index)
|
2017-03-02 01:25:58 +08:00
|
|
|
|
2021-05-12 22:34:17 +08:00
|
|
|
tags = merge(var.default_tags, tomap({
|
|
|
|
Name = "kubernetes-${var.aws_cluster_name}-${element(var.aws_avail_zones, count.index)}-private"
|
|
|
|
}))
|
2017-03-02 01:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#Routing in VPC
|
|
|
|
|
|
|
|
#TODO: Do we need two routing tables for each subnet for redundancy or is one enough?
|
|
|
|
|
|
|
|
resource "aws_route_table" "kubernetes-public" {
|
2020-06-05 15:05:43 +08:00
|
|
|
vpc_id = aws_vpc.cluster-vpc.id
|
2017-10-18 18:44:32 +08:00
|
|
|
|
2019-04-08 17:22:24 +08:00
|
|
|
route {
|
|
|
|
cidr_block = "0.0.0.0/0"
|
2020-06-05 15:05:43 +08:00
|
|
|
gateway_id = aws_internet_gateway.cluster-vpc-internetgw.id
|
2019-04-08 17:22:24 +08:00
|
|
|
}
|
|
|
|
|
2021-05-12 22:34:17 +08:00
|
|
|
tags = merge(var.default_tags, tomap({
|
|
|
|
Name = "kubernetes-${var.aws_cluster_name}-routetable-public"
|
|
|
|
}))
|
2017-03-02 01:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table" "kubernetes-private" {
|
2020-06-05 15:05:43 +08:00
|
|
|
count = length(var.aws_cidr_subnets_private)
|
|
|
|
vpc_id = aws_vpc.cluster-vpc.id
|
2019-04-08 17:22:24 +08:00
|
|
|
|
|
|
|
route {
|
|
|
|
cidr_block = "0.0.0.0/0"
|
2020-06-05 15:05:43 +08:00
|
|
|
nat_gateway_id = element(aws_nat_gateway.cluster-nat-gateway.*.id, count.index)
|
2019-04-08 17:22:24 +08:00
|
|
|
}
|
|
|
|
|
2021-05-12 22:34:17 +08:00
|
|
|
tags = merge(var.default_tags, tomap({
|
|
|
|
Name = "kubernetes-${var.aws_cluster_name}-routetable-private-${count.index}"
|
|
|
|
}))
|
2017-03-02 01:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table_association" "kubernetes-public" {
|
2020-06-05 15:05:43 +08:00
|
|
|
count = length(var.aws_cidr_subnets_public)
|
|
|
|
subnet_id = element(aws_subnet.cluster-vpc-subnets-public.*.id, count.index)
|
|
|
|
route_table_id = aws_route_table.kubernetes-public.id
|
2017-03-02 01:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table_association" "kubernetes-private" {
|
2020-06-05 15:05:43 +08:00
|
|
|
count = length(var.aws_cidr_subnets_private)
|
|
|
|
subnet_id = element(aws_subnet.cluster-vpc-subnets-private.*.id, count.index)
|
|
|
|
route_table_id = element(aws_route_table.kubernetes-private.*.id, count.index)
|
2017-03-02 01:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#Kubernetes Security Groups
|
|
|
|
|
|
|
|
resource "aws_security_group" "kubernetes" {
|
2019-04-08 17:22:24 +08:00
|
|
|
name = "kubernetes-${var.aws_cluster_name}-securitygroup"
|
2020-06-05 15:05:43 +08:00
|
|
|
vpc_id = aws_vpc.cluster-vpc.id
|
2017-03-02 01:25:58 +08:00
|
|
|
|
2021-05-12 22:34:17 +08:00
|
|
|
tags = merge(var.default_tags, tomap({
|
|
|
|
Name = "kubernetes-${var.aws_cluster_name}-securitygroup"
|
|
|
|
}))
|
2017-03-02 01:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "allow-all-ingress" {
|
2019-04-08 17:22:24 +08:00
|
|
|
type = "ingress"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 65535
|
|
|
|
protocol = "-1"
|
2020-06-05 15:05:43 +08:00
|
|
|
cidr_blocks = [var.aws_vpc_cidr_block]
|
|
|
|
security_group_id = aws_security_group.kubernetes.id
|
2017-03-02 01:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "allow-all-egress" {
|
2019-04-08 17:22:24 +08:00
|
|
|
type = "egress"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 65535
|
|
|
|
protocol = "-1"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
2020-06-05 15:05:43 +08:00
|
|
|
security_group_id = aws_security_group.kubernetes.id
|
2017-03-02 01:25:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "allow-ssh-connections" {
|
2019-04-08 17:22:24 +08:00
|
|
|
type = "ingress"
|
|
|
|
from_port = 22
|
|
|
|
to_port = 22
|
|
|
|
protocol = "TCP"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
2020-06-05 15:05:43 +08:00
|
|
|
security_group_id = aws_security_group.kubernetes.id
|
2017-03-02 01:25:58 +08:00
|
|
|
}
|