2017-03-01 17:25:58 +00:00
|
|
|
resource "aws_vpc" "cluster-vpc" {
|
2019-04-08 09:22:24 +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
|
|
|
|
2019-04-08 09:22:24 +00:00
|
|
|
tags = "${merge(var.default_tags, map(
|
2020-03-27 12:46:24 +00:00
|
|
|
"Name", "kubernetes-${var.aws_cluster_name}-vpc"
|
|
|
|
))}"
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_eip" "cluster-nat-eip" {
|
2019-04-08 09:22:24 +00:00
|
|
|
count = "${length(var.aws_cidr_subnets_public)}"
|
|
|
|
vpc = true
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_internet_gateway" "cluster-vpc-internetgw" {
|
|
|
|
vpc_id = "${aws_vpc.cluster-vpc.id}"
|
|
|
|
|
2017-10-18 10:44:32 +00:00
|
|
|
tags = "${merge(var.default_tags, map(
|
|
|
|
"Name", "kubernetes-${var.aws_cluster_name}-internetgw"
|
|
|
|
))}"
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_subnet" "cluster-vpc-subnets-public" {
|
2019-04-08 09:22:24 +00: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-01 17:25:58 +00:00
|
|
|
|
2019-04-08 09:22:24 +00:00
|
|
|
tags = "${merge(var.default_tags, map(
|
2020-03-27 12:46:24 +00:00
|
|
|
"Name", "kubernetes-${var.aws_cluster_name}-${element(var.aws_avail_zones, count.index)}-public",
|
|
|
|
"kubernetes.io/cluster/${var.aws_cluster_name}", "member"
|
|
|
|
))}"
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_nat_gateway" "cluster-nat-gateway" {
|
2019-04-08 09:22:24 +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" {
|
2019-04-08 09:22:24 +00: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-01 17:25:58 +00:00
|
|
|
|
2019-04-08 09:22:24 +00:00
|
|
|
tags = "${merge(var.default_tags, map(
|
2020-03-27 12:46:24 +00:00
|
|
|
"Name", "kubernetes-${var.aws_cluster_name}-${element(var.aws_avail_zones, count.index)}-private"
|
|
|
|
))}"
|
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" {
|
2019-04-08 09:22:24 +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"
|
|
|
|
gateway_id = "${aws_internet_gateway.cluster-vpc-internetgw.id}"
|
|
|
|
}
|
|
|
|
|
|
|
|
tags = "${merge(var.default_tags, map(
|
2020-03-27 12:46:24 +00:00
|
|
|
"Name", "kubernetes-${var.aws_cluster_name}-routetable-public"
|
|
|
|
))}"
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table" "kubernetes-private" {
|
2019-04-08 09:22:24 +00:00
|
|
|
count = "${length(var.aws_cidr_subnets_private)}"
|
|
|
|
vpc_id = "${aws_vpc.cluster-vpc.id}"
|
|
|
|
|
|
|
|
route {
|
|
|
|
cidr_block = "0.0.0.0/0"
|
|
|
|
nat_gateway_id = "${element(aws_nat_gateway.cluster-nat-gateway.*.id, count.index)}"
|
|
|
|
}
|
|
|
|
|
|
|
|
tags = "${merge(var.default_tags, map(
|
2020-03-27 12:46:24 +00:00
|
|
|
"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" {
|
2019-04-08 09:22:24 +00:00
|
|
|
count = "${length(var.aws_cidr_subnets_public)}"
|
2020-03-27 12:46:24 +00:00
|
|
|
subnet_id = "${element(aws_subnet.cluster-vpc-subnets-public.*.id, count.index)}"
|
2019-04-08 09:22:24 +00:00
|
|
|
route_table_id = "${aws_route_table.kubernetes-public.id}"
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_route_table_association" "kubernetes-private" {
|
2019-04-08 09:22:24 +00:00
|
|
|
count = "${length(var.aws_cidr_subnets_private)}"
|
2020-03-27 12:46:24 +00:00
|
|
|
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"
|
|
|
|
vpc_id = "${aws_vpc.cluster-vpc.id}"
|
2017-03-01 17:25:58 +00:00
|
|
|
|
2019-04-08 09:22:24 +00:00
|
|
|
tags = "${merge(var.default_tags, map(
|
2020-03-27 12:46:24 +00:00
|
|
|
"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"
|
|
|
|
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"]
|
|
|
|
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"]
|
|
|
|
security_group_id = "${aws_security_group.kubernetes.id}"
|
2017-03-01 17:25:58 +00:00
|
|
|
}
|