2022-10-21 06:24:18 -04:00
|
|
|
terraform {
|
|
|
|
required_providers {
|
|
|
|
aws = {
|
|
|
|
source = "hashicorp/aws"
|
2023-11-21 05:24:31 -05:00
|
|
|
version = "5.26.0"
|
2022-10-21 06:24:18 -04:00
|
|
|
}
|
|
|
|
random = {
|
|
|
|
source = "hashicorp/random"
|
2023-05-16 10:01:47 -04:00
|
|
|
version = "3.5.1"
|
2022-10-21 06:24:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Configure the AWS Provider
|
|
|
|
provider "aws" {
|
|
|
|
region = var.region
|
|
|
|
}
|
|
|
|
|
|
|
|
locals {
|
2023-10-23 09:06:48 -04:00
|
|
|
uid = random_id.uid.hex
|
|
|
|
name = "${var.name}-${local.uid}"
|
|
|
|
initSecretHash = random_password.initSecret.bcrypt_hash
|
|
|
|
cidr_vpc_subnet_nodes = "192.168.176.0/20"
|
|
|
|
ports_node_range = "30000-32767"
|
2023-10-05 06:34:02 -04:00
|
|
|
load_balancer_ports = flatten([
|
|
|
|
{ name = "kubernetes", port = "6443", health_check = "HTTPS" },
|
|
|
|
{ name = "bootstrapper", port = "9000", health_check = "TCP" },
|
|
|
|
{ name = "verify", port = "30081", health_check = "TCP" },
|
|
|
|
{ name = "konnectivity", port = "8132", health_check = "TCP" },
|
|
|
|
{ name = "recovery", port = "9999", health_check = "TCP" },
|
|
|
|
{ name = "join", port = "30090", health_check = "TCP" },
|
|
|
|
var.debug ? [{ name = "debugd", port = "4000", health_check = "TCP" }] : [],
|
|
|
|
])
|
2023-06-23 11:19:43 -04:00
|
|
|
target_group_arns = {
|
2023-10-05 06:34:02 -04:00
|
|
|
control-plane : [
|
|
|
|
for port in local.load_balancer_ports : module.load_balancer_targets[port.name].target_group_arn
|
|
|
|
]
|
2023-06-23 11:19:43 -04:00
|
|
|
worker : []
|
|
|
|
}
|
|
|
|
iam_instance_profile = {
|
|
|
|
control-plane : var.iam_instance_profile_control_plane
|
|
|
|
worker : var.iam_instance_profile_worker_nodes
|
|
|
|
}
|
|
|
|
# zones are all availability zones that are used by the node groups
|
|
|
|
zones = distinct(sort([
|
|
|
|
for node_group in var.node_groups : node_group.zone
|
|
|
|
]))
|
2023-07-21 10:43:51 -04:00
|
|
|
// wildcard_lb_dns_name is the DNS name of the load balancer with a wildcard for the name.
|
|
|
|
// example: given "name-1234567890.region.elb.amazonaws.com" it will return "*.region.elb.amazonaws.com"
|
|
|
|
wildcard_lb_dns_name = replace(aws_lb.front_end.dns_name, "/^[^.]*\\./", "*.")
|
2022-10-21 06:24:18 -04:00
|
|
|
|
2023-07-21 10:43:51 -04:00
|
|
|
tags = {
|
|
|
|
constellation-uid = local.uid,
|
|
|
|
}
|
2023-10-17 09:46:15 -04:00
|
|
|
|
|
|
|
in_cluster_endpoint = aws_lb.front_end.dns_name
|
|
|
|
out_of_cluster_endpoint = var.internal_load_balancer && var.debug ? module.jump_host[0].ip : local.in_cluster_endpoint
|
2022-10-21 06:24:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "random_id" "uid" {
|
|
|
|
byte_length = 4
|
|
|
|
}
|
|
|
|
|
2022-11-26 13:44:34 -05:00
|
|
|
resource "random_password" "initSecret" {
|
|
|
|
length = 32
|
|
|
|
special = true
|
|
|
|
override_special = "_%@"
|
|
|
|
}
|
|
|
|
|
2022-10-21 06:24:18 -04:00
|
|
|
resource "aws_vpc" "vpc" {
|
|
|
|
cidr_block = "192.168.0.0/16"
|
|
|
|
tags = merge(local.tags, { Name = "${local.name}-vpc" })
|
|
|
|
}
|
|
|
|
|
|
|
|
module "public_private_subnet" {
|
|
|
|
source = "./modules/public_private_subnet"
|
|
|
|
name = local.name
|
|
|
|
vpc_id = aws_vpc.vpc.id
|
2023-10-23 09:06:48 -04:00
|
|
|
cidr_vpc_subnet_nodes = local.cidr_vpc_subnet_nodes
|
2023-06-23 11:19:43 -04:00
|
|
|
cidr_vpc_subnet_internet = "192.168.0.0/20"
|
2022-10-21 06:24:18 -04:00
|
|
|
zone = var.zone
|
2023-06-23 11:19:43 -04:00
|
|
|
zones = local.zones
|
2022-10-21 06:24:18 -04:00
|
|
|
tags = local.tags
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_eip" "lb" {
|
2023-06-30 10:56:31 -04:00
|
|
|
# TODO(malt3): use for_each = toset(module.public_private_subnet.all_zones)
|
|
|
|
# in a future version to support all availability zones in the chosen region
|
|
|
|
# This should only be done after we migrated to DNS-based addressing for the
|
|
|
|
# control-plane.
|
2023-10-17 09:46:15 -04:00
|
|
|
for_each = var.internal_load_balancer ? [] : toset([var.zone])
|
2023-06-23 11:19:43 -04:00
|
|
|
domain = "vpc"
|
2023-07-21 10:43:51 -04:00
|
|
|
tags = merge(local.tags, { "constellation-ip-endpoint" = each.key == var.zone ? "legacy-primary-zone" : "additional-zone" })
|
2022-10-21 06:24:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_lb" "front_end" {
|
|
|
|
name = "${local.name}-loadbalancer"
|
2023-10-17 09:46:15 -04:00
|
|
|
internal = var.internal_load_balancer
|
2022-10-21 06:24:18 -04:00
|
|
|
load_balancer_type = "network"
|
|
|
|
tags = local.tags
|
2023-09-25 04:23:35 -04:00
|
|
|
security_groups = [aws_security_group.security_group.id]
|
2022-10-21 06:24:18 -04:00
|
|
|
|
2023-06-23 11:19:43 -04:00
|
|
|
dynamic "subnet_mapping" {
|
2023-06-28 07:22:28 -04:00
|
|
|
# TODO(malt3): use for_each = toset(module.public_private_subnet.all_zones)
|
|
|
|
# in a future version to support all availability zones in the chosen region
|
|
|
|
# without needing to constantly replace the loadbalancer.
|
2023-06-30 10:56:31 -04:00
|
|
|
# This has to wait until the bootstrapper that we upgrade from (source version) use
|
|
|
|
# DNS-based addressing for the control-plane.
|
2023-06-28 07:22:28 -04:00
|
|
|
# for_each = toset(module.public_private_subnet.all_zones)
|
2023-06-30 10:56:31 -04:00
|
|
|
for_each = toset([var.zone])
|
2023-06-23 11:19:43 -04:00
|
|
|
content {
|
|
|
|
subnet_id = module.public_private_subnet.public_subnet_id[subnet_mapping.key]
|
2023-10-17 09:46:15 -04:00
|
|
|
allocation_id = var.internal_load_balancer ? "" : aws_eip.lb[subnet_mapping.key].id
|
2023-06-23 11:19:43 -04:00
|
|
|
}
|
2022-10-21 06:24:18 -04:00
|
|
|
}
|
|
|
|
enable_cross_zone_load_balancing = true
|
2023-09-25 04:23:35 -04:00
|
|
|
|
|
|
|
lifecycle {
|
|
|
|
ignore_changes = [security_groups]
|
|
|
|
}
|
2022-10-21 06:24:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_security_group" "security_group" {
|
|
|
|
name = local.name
|
|
|
|
vpc_id = aws_vpc.vpc.id
|
|
|
|
description = "Security group for ${local.name}"
|
|
|
|
tags = local.tags
|
|
|
|
|
|
|
|
egress {
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "-1"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
description = "Allow all outbound traffic"
|
|
|
|
}
|
|
|
|
|
|
|
|
ingress {
|
|
|
|
from_port = split("-", local.ports_node_range)[0]
|
|
|
|
to_port = split("-", local.ports_node_range)[1]
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
description = "K8s node ports"
|
|
|
|
}
|
|
|
|
|
2023-10-05 06:34:02 -04:00
|
|
|
dynamic "ingress" {
|
|
|
|
for_each = local.load_balancer_ports
|
|
|
|
content {
|
|
|
|
description = ingress.value.name
|
|
|
|
from_port = ingress.value.port
|
|
|
|
to_port = ingress.value.port
|
|
|
|
protocol = "tcp"
|
|
|
|
cidr_blocks = ["0.0.0.0/0"]
|
|
|
|
}
|
2022-11-07 17:04:50 -05:00
|
|
|
}
|
|
|
|
|
2022-11-02 06:46:52 -04:00
|
|
|
ingress {
|
|
|
|
from_port = 0
|
|
|
|
to_port = 0
|
|
|
|
protocol = "-1"
|
|
|
|
cidr_blocks = [aws_vpc.vpc.cidr_block]
|
|
|
|
description = "allow all internal"
|
|
|
|
}
|
|
|
|
|
2022-10-21 06:24:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_cloudwatch_log_group" "log_group" {
|
|
|
|
name = local.name
|
|
|
|
retention_in_days = 30
|
|
|
|
tags = local.tags
|
|
|
|
}
|
|
|
|
|
2023-10-05 06:34:02 -04:00
|
|
|
module "load_balancer_targets" {
|
|
|
|
for_each = { for port in local.load_balancer_ports : port.name => port }
|
2022-11-02 06:46:52 -04:00
|
|
|
source = "./modules/load_balancer_target"
|
2023-10-05 06:34:02 -04:00
|
|
|
name = "${local.name}-${each.value.name}"
|
|
|
|
port = each.value.port
|
|
|
|
healthcheck_protocol = each.value.health_check
|
|
|
|
healthcheck_path = each.value.name == "kubernetes" ? "/readyz" : ""
|
2022-11-02 06:46:52 -04:00
|
|
|
vpc_id = aws_vpc.vpc.id
|
|
|
|
lb_arn = aws_lb.front_end.arn
|
|
|
|
tags = local.tags
|
2023-09-25 04:23:35 -04:00
|
|
|
}
|
|
|
|
|
2023-06-23 11:19:43 -04:00
|
|
|
module "instance_group" {
|
2022-10-21 06:24:18 -04:00
|
|
|
source = "./modules/instance_group"
|
2023-06-23 11:19:43 -04:00
|
|
|
for_each = var.node_groups
|
|
|
|
base_name = local.name
|
|
|
|
node_group_name = each.key
|
|
|
|
role = each.value.role
|
|
|
|
zone = each.value.zone
|
2022-10-21 06:24:18 -04:00
|
|
|
uid = local.uid
|
2023-06-23 11:19:43 -04:00
|
|
|
instance_type = each.value.instance_type
|
2023-06-30 04:53:00 -04:00
|
|
|
initial_count = each.value.initial_count
|
2022-10-21 06:24:18 -04:00
|
|
|
image_id = var.ami
|
2023-06-23 11:19:43 -04:00
|
|
|
state_disk_type = each.value.disk_type
|
|
|
|
state_disk_size = each.value.disk_size
|
|
|
|
target_group_arns = local.target_group_arns[each.value.role]
|
2022-11-08 04:08:11 -05:00
|
|
|
security_groups = [aws_security_group.security_group.id]
|
2023-06-23 11:19:43 -04:00
|
|
|
subnetwork = module.public_private_subnet.private_subnet_id[each.value.zone]
|
|
|
|
iam_instance_profile = local.iam_instance_profile[each.value.role]
|
2023-06-09 09:41:02 -04:00
|
|
|
enable_snp = var.enable_snp
|
2022-11-26 13:44:34 -05:00
|
|
|
tags = merge(
|
|
|
|
local.tags,
|
2023-08-14 07:42:20 -04:00
|
|
|
{ Name = "${local.name}-${each.value.role}" },
|
2023-06-23 11:19:43 -04:00
|
|
|
{ constellation-role = each.value.role },
|
2023-06-30 10:56:31 -04:00
|
|
|
{ constellation-node-group = each.key },
|
2022-11-26 13:44:34 -05:00
|
|
|
{ constellation-uid = local.uid },
|
2023-06-13 03:58:39 -04:00
|
|
|
{ constellation-init-secret-hash = local.initSecretHash },
|
|
|
|
{ "kubernetes.io/cluster/${local.name}" = "owned" }
|
2022-11-26 13:44:34 -05:00
|
|
|
)
|
2022-10-21 06:24:18 -04:00
|
|
|
}
|
2023-10-05 06:34:02 -04:00
|
|
|
|
2023-10-17 09:46:15 -04:00
|
|
|
module "jump_host" {
|
|
|
|
count = var.internal_load_balancer && var.debug ? 1 : 0
|
|
|
|
source = "./modules/jump_host"
|
|
|
|
base_name = local.name
|
|
|
|
subnet_id = module.public_private_subnet.public_subnet_id[var.zone]
|
|
|
|
lb_internal_ip = aws_lb.front_end.dns_name
|
|
|
|
ports = [for port in local.load_balancer_ports : port.port]
|
|
|
|
iam_instance_profile = var.iam_instance_profile_worker_nodes
|
|
|
|
security_group_id = aws_security_group.security_group.id
|
|
|
|
}
|
|
|
|
|
2023-10-05 06:34:02 -04:00
|
|
|
# TODO(31u3r): Remove once 2.12 is released
|
|
|
|
moved {
|
|
|
|
from = module.load_balancer_target_konnectivity
|
|
|
|
to = module.load_balancer_targets["konnectivity"]
|
|
|
|
}
|
|
|
|
|
|
|
|
moved {
|
|
|
|
from = module.load_balancer_target_verify
|
|
|
|
to = module.load_balancer_targets["verify"]
|
|
|
|
}
|
|
|
|
|
|
|
|
moved {
|
|
|
|
from = module.load_balancer_target_recovery
|
|
|
|
to = module.load_balancer_targets["recovery"]
|
|
|
|
}
|
|
|
|
|
|
|
|
moved {
|
|
|
|
from = module.load_balancer_target_join
|
|
|
|
to = module.load_balancer_targets["join"]
|
|
|
|
}
|
|
|
|
|
|
|
|
moved {
|
|
|
|
from = module.load_balancer_target_debugd[0]
|
|
|
|
to = module.load_balancer_targets["debugd"]
|
|
|
|
}
|
|
|
|
|
|
|
|
moved {
|
|
|
|
from = module.load_balancer_target_kubernetes
|
|
|
|
to = module.load_balancer_targets["kubernetes"]
|
|
|
|
}
|
|
|
|
|
|
|
|
moved {
|
|
|
|
from = module.load_balancer_target_bootstrapper
|
|
|
|
to = module.load_balancer_targets["bootstrapper"]
|
|
|
|
}
|