2017-03-01 17:25:58 +00:00
|
|
|
resource "aws_vpc" "cluster-vpc" {
|
2020-06-05 07:05:43 +00:00
|
|
|
cidr_block = var.aws_vpc_cidr_block
|
2017-03-01 17:25:58 +00:00
|
|
|
|
2019-04-08 09:22:24 +00:00
|
|
|
#DNS Related Entries
|
|
|
|
enable_dns_support = true
|
|
|
|
enable_dns_hostnames = true
|
2017-03-01 17:25:58 +00:00
|
|
|
|
2021-05-12 14:34:17 +00:00
|
|
|
tags = merge(var.default_tags, tomap({
|
|
|
|
Name = "kubernetes-${var.aws_cluster_name}-vpc"
|
|
|
|
}))
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_eip" "cluster-nat-eip" {
|
2020-06-05 07:05:43 +00:00
|
|
|
count = length(var.aws_cidr_subnets_public)
|
2019-04-08 09:22:24 +00:00
|
|
|
vpc = true
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_internet_gateway" "cluster-vpc-internetgw" {
|
2020-06-05 07:05:43 +00:00
|
|
|
vpc_id = aws_vpc.cluster-vpc.id
|
2017-03-01 17:25:58 +00:00
|
|
|
|
2021-05-12 14:34:17 +00:00
|
|
|
tags = merge(var.default_tags, tomap({
|
|
|
|
Name = "kubernetes-${var.aws_cluster_name}-internetgw"
|
|
|
|
}))
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "cluster-vpc-subnets-public" {
|
2020-06-05 07:05:43 +00:00
|
|
|
vpc_id = aws_vpc.cluster-vpc.id
|
2022-02-14 21:12:30 +00:00
|
|
|
count = length(var.aws_cidr_subnets_public)
|
|
|
|
availability_zone = element(var.aws_avail_zones, count.index % length(var.aws_avail_zones))
|
2020-06-05 07:05:43 +00:00
|
|
|
cidr_block = element(var.aws_cidr_subnets_public, count.index)
|
2017-03-01 17:25:58 +00:00
|
|
|
|
2021-05-12 14:34:17 +00:00
|
|
|
tags = merge(var.default_tags, tomap({
|
|
|
|
Name = "kubernetes-${var.aws_cluster_name}-${element(var.aws_avail_zones, count.index)}-public"
|
2022-04-12 17:05:23 +00:00
|
|
|
"kubernetes.io/cluster/${var.aws_cluster_name}" = "shared"
|
|
|
|
"kubernetes.io/role/elb" = "1"
|
2021-05-12 14:34:17 +00:00
|
|
|
}))
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_nat_gateway" "cluster-nat-gateway" {
|
2020-06-05 07:05:43 +00: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-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "cluster-vpc-subnets-private" {
|
2020-06-05 07:05:43 +00:00
|
|
|
vpc_id = aws_vpc.cluster-vpc.id
|
2022-02-14 21:12:30 +00:00
|
|
|
count = length(var.aws_cidr_subnets_private)
|
|
|
|
availability_zone = element(var.aws_avail_zones, count.index % length(var.aws_avail_zones))
|
2020-06-05 07:05:43 +00:00
|
|
|
cidr_block = element(var.aws_cidr_subnets_private, count.index)
|
2017-03-01 17:25:58 +00:00
|
|
|
|
2021-05-12 14:34:17 +00:00
|
|
|
tags = merge(var.default_tags, tomap({
|
|
|
|
Name = "kubernetes-${var.aws_cluster_name}-${element(var.aws_avail_zones, count.index)}-private"
|
2022-04-12 17:05:23 +00:00
|
|
|
"kubernetes.io/cluster/${var.aws_cluster_name}" = "shared"
|
|
|
|
"kubernetes.io/role/internal-elb" = "1"
|
2021-05-12 14:34:17 +00:00
|
|
|
}))
|
2017-03-01 17:25:58 +00: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 07:05:43 +00:00
|
|
|
vpc_id = aws_vpc.cluster-vpc.id
|
2017-10-18 10:44:32 +00:00
|
|
|
|
2019-04-08 09:22:24 +00:00
|
|
|
route {
|
|
|
|
cidr_block = "0.0.0.0/0"
|
2020-06-05 07:05:43 +00:00
|
|
|
gateway_id = aws_internet_gateway.cluster-vpc-internetgw.id
|
2019-04-08 09:22:24 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 14:34:17 +00:00
|
|
|
tags = merge(var.default_tags, tomap({
|
|
|
|
Name = "kubernetes-${var.aws_cluster_name}-routetable-public"
|
|
|
|
}))
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table" "kubernetes-private" {
|
2020-06-05 07:05:43 +00:00
|
|
|
count = length(var.aws_cidr_subnets_private)
|
|
|
|
vpc_id = aws_vpc.cluster-vpc.id
|
2019-04-08 09:22:24 +00:00
|
|
|
|
|
|
|
route {
|
|
|
|
cidr_block = "0.0.0.0/0"
|
2020-06-05 07:05:43 +00:00
|
|
|
nat_gateway_id = element(aws_nat_gateway.cluster-nat-gateway.*.id, count.index)
|
2019-04-08 09:22:24 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 14:34:17 +00:00
|
|
|
tags = merge(var.default_tags, tomap({
|
|
|
|
Name = "kubernetes-${var.aws_cluster_name}-routetable-private-${count.index}"
|
|
|
|
}))
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table_association" "kubernetes-public" {
|
2020-06-05 07:05:43 +00: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-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table_association" "kubernetes-private" {
|
2020-06-05 07:05:43 +00: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-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#Kubernetes Security Groups
|
|
|
|
|
|
|
|
resource "aws_security_group" "kubernetes" {
|
2019-04-08 09:22:24 +00:00
|
|
|
name = "kubernetes-${var.aws_cluster_name}-securitygroup"
|
2020-06-05 07:05:43 +00:00
|
|
|
vpc_id = aws_vpc.cluster-vpc.id
|
2017-03-01 17:25:58 +00:00
|
|
|
|
2021-05-12 14:34:17 +00:00
|
|
|
tags = merge(var.default_tags, tomap({
|
|
|
|
Name = "kubernetes-${var.aws_cluster_name}-securitygroup"
|
|
|
|
}))
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "allow-all-ingress" {
|
2019-04-08 09:22:24 +00:00
|
|
|
type = "ingress"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 65535
|
|
|
|
protocol = "-1"
|
2020-06-05 07:05:43 +00:00
|
|
|
cidr_blocks = [var.aws_vpc_cidr_block]
|
|
|
|
security_group_id = aws_security_group.kubernetes.id
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "allow-all-egress" {
|
2019-04-08 09:22:24 +00:00
|
|
|
type = "egress"
|
|
|
|
from_port = 0
|
|
|
|
to_port = 65535
|
|
|
|
protocol = "-1"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
2020-06-05 07:05:43 +00:00
|
|
|
security_group_id = aws_security_group.kubernetes.id
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group_rule" "allow-ssh-connections" {
|
2019-04-08 09:22:24 +00:00
|
|
|
type = "ingress"
|
|
|
|
from_port = 22
|
|
|
|
to_port = 22
|
|
|
|
protocol = "TCP"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
2020-06-05 07:05:43 +00:00
|
|
|
security_group_id = aws_security_group.kubernetes.id
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|