@thomas.solbjor her er "import" av tofu fra ditt repo med justeringer for å tilpasse patterns her. Også minimalisert til å kun opprette cluster, ingen managed services som postgres etc. Ta en titt. Co-authored-by: Danijel Simeunovic <danijel.simeunovic@fortedigital.com> Reviewed-on: #15 Reviewed-by: Danijel Simeunovic <danijel.simeunovic@fortedigital.com> Co-authored-by: Ghost <> Co-committed-by: Ghost <>
208 lines
6.0 KiB
HCL
208 lines
6.0 KiB
HCL
# ─── VPC ──────────────────────────────────────────────────────────────
|
|
|
|
resource "aws_vpc" "main" {
|
|
cidr_block = var.vpc_cidr
|
|
enable_dns_hostnames = true
|
|
enable_dns_support = true
|
|
|
|
tags = merge(var.tags, { Name = "${var.prefix}-vpc" })
|
|
}
|
|
|
|
resource "aws_internet_gateway" "main" {
|
|
vpc_id = aws_vpc.main.id
|
|
tags = merge(var.tags, { Name = "${var.prefix}-igw" })
|
|
}
|
|
|
|
# Public subnets (one per AZ) — for NAT gateways and load balancers
|
|
resource "aws_subnet" "public" {
|
|
count = length(var.availability_zones)
|
|
vpc_id = aws_vpc.main.id
|
|
cidr_block = cidrsubnet(var.vpc_cidr, 4, count.index)
|
|
availability_zone = var.availability_zones[count.index]
|
|
|
|
map_public_ip_on_launch = true
|
|
|
|
tags = merge(var.tags, {
|
|
Name = "${var.prefix}-public-${count.index + 1}"
|
|
"kubernetes.io/cluster/${var.prefix}-eks" = "shared"
|
|
"kubernetes.io/role/elb" = "1"
|
|
})
|
|
}
|
|
|
|
# Private subnets (one per AZ) — for EKS nodes
|
|
resource "aws_subnet" "private" {
|
|
count = length(var.availability_zones)
|
|
vpc_id = aws_vpc.main.id
|
|
cidr_block = cidrsubnet(var.vpc_cidr, 4, count.index + length(var.availability_zones))
|
|
availability_zone = var.availability_zones[count.index]
|
|
|
|
tags = merge(var.tags, {
|
|
Name = "${var.prefix}-private-${count.index + 1}"
|
|
"kubernetes.io/cluster/${var.prefix}-eks" = "shared"
|
|
"kubernetes.io/role/internal-elb" = "1"
|
|
})
|
|
}
|
|
|
|
# NAT Gateway (single, in first public subnet — use one per AZ for prod HA)
|
|
resource "aws_eip" "nat" {
|
|
domain = "vpc"
|
|
tags = merge(var.tags, { Name = "${var.prefix}-nat-eip" })
|
|
}
|
|
|
|
resource "aws_nat_gateway" "main" {
|
|
allocation_id = aws_eip.nat.id
|
|
subnet_id = aws_subnet.public[0].id
|
|
tags = merge(var.tags, { Name = "${var.prefix}-nat" })
|
|
|
|
depends_on = [aws_internet_gateway.main]
|
|
}
|
|
|
|
resource "aws_route_table" "public" {
|
|
vpc_id = aws_vpc.main.id
|
|
|
|
route {
|
|
cidr_block = "0.0.0.0/0"
|
|
gateway_id = aws_internet_gateway.main.id
|
|
}
|
|
|
|
tags = merge(var.tags, { Name = "${var.prefix}-public-rt" })
|
|
}
|
|
|
|
resource "aws_route_table_association" "public" {
|
|
count = length(var.availability_zones)
|
|
subnet_id = aws_subnet.public[count.index].id
|
|
route_table_id = aws_route_table.public.id
|
|
}
|
|
|
|
resource "aws_route_table" "private" {
|
|
vpc_id = aws_vpc.main.id
|
|
|
|
route {
|
|
cidr_block = "0.0.0.0/0"
|
|
nat_gateway_id = aws_nat_gateway.main.id
|
|
}
|
|
|
|
tags = merge(var.tags, { Name = "${var.prefix}-private-rt" })
|
|
}
|
|
|
|
resource "aws_route_table_association" "private" {
|
|
count = length(var.availability_zones)
|
|
subnet_id = aws_subnet.private[count.index].id
|
|
route_table_id = aws_route_table.private.id
|
|
}
|
|
|
|
# ─── EKS Cluster ──────────────────────────────────────────────────────
|
|
|
|
resource "aws_iam_role" "eks_cluster" {
|
|
name_prefix = "${var.prefix}-eks-cluster-"
|
|
|
|
assume_role_policy = jsonencode({
|
|
Version = "2012-10-17"
|
|
Statement = [{
|
|
Action = "sts:AssumeRole"
|
|
Effect = "Allow"
|
|
Principal = { Service = "eks.amazonaws.com" }
|
|
}]
|
|
})
|
|
|
|
tags = var.tags
|
|
}
|
|
|
|
resource "aws_iam_role_policy_attachment" "eks_cluster_policy" {
|
|
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
|
|
role = aws_iam_role.eks_cluster.name
|
|
}
|
|
|
|
resource "aws_eks_cluster" "main" {
|
|
name = "${var.prefix}-eks"
|
|
role_arn = aws_iam_role.eks_cluster.arn
|
|
version = var.kubernetes_version
|
|
|
|
vpc_config {
|
|
subnet_ids = concat(aws_subnet.private[*].id, aws_subnet.public[*].id)
|
|
endpoint_private_access = true
|
|
endpoint_public_access = true
|
|
}
|
|
|
|
# Enable OIDC issuer for IRSA (IAM Roles for Service Accounts)
|
|
access_config {
|
|
authentication_mode = "API_AND_CONFIG_MAP"
|
|
}
|
|
|
|
tags = var.tags
|
|
|
|
depends_on = [aws_iam_role_policy_attachment.eks_cluster_policy]
|
|
}
|
|
|
|
# OIDC provider — required for IRSA (IAM Roles for Service Accounts)
|
|
data "tls_certificate" "eks" {
|
|
url = aws_eks_cluster.main.identity[0].oidc[0].issuer
|
|
}
|
|
|
|
resource "aws_iam_openid_connect_provider" "eks" {
|
|
client_id_list = ["sts.amazonaws.com"]
|
|
thumbprint_list = [data.tls_certificate.eks.certificates[0].sha1_fingerprint]
|
|
url = aws_eks_cluster.main.identity[0].oidc[0].issuer
|
|
|
|
tags = var.tags
|
|
}
|
|
|
|
# EKS Node Group
|
|
|
|
resource "aws_iam_role" "eks_nodes" {
|
|
name_prefix = "${var.prefix}-eks-nodes-"
|
|
|
|
assume_role_policy = jsonencode({
|
|
Version = "2012-10-17"
|
|
Statement = [{
|
|
Action = "sts:AssumeRole"
|
|
Effect = "Allow"
|
|
Principal = { Service = "ec2.amazonaws.com" }
|
|
}]
|
|
})
|
|
|
|
tags = var.tags
|
|
}
|
|
|
|
resource "aws_iam_role_policy_attachment" "eks_worker_node_policy" {
|
|
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
|
|
role = aws_iam_role.eks_nodes.name
|
|
}
|
|
|
|
resource "aws_iam_role_policy_attachment" "eks_cni_policy" {
|
|
policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
|
|
role = aws_iam_role.eks_nodes.name
|
|
}
|
|
|
|
resource "aws_iam_role_policy_attachment" "eks_ecr_readonly" {
|
|
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
|
|
role = aws_iam_role.eks_nodes.name
|
|
}
|
|
|
|
resource "aws_eks_node_group" "main" {
|
|
cluster_name = aws_eks_cluster.main.name
|
|
node_group_name = "${var.prefix}-nodes"
|
|
node_role_arn = aws_iam_role.eks_nodes.arn
|
|
subnet_ids = aws_subnet.private[*].id
|
|
|
|
instance_types = [var.node_instance_type]
|
|
|
|
scaling_config {
|
|
desired_size = var.node_count
|
|
max_size = var.node_max_count
|
|
min_size = var.node_min_count
|
|
}
|
|
|
|
update_config {
|
|
max_unavailable = 1
|
|
}
|
|
|
|
tags = var.tags
|
|
|
|
depends_on = [
|
|
aws_iam_role_policy_attachment.eks_worker_node_policy,
|
|
aws_iam_role_policy_attachment.eks_cni_policy,
|
|
aws_iam_role_policy_attachment.eks_ecr_readonly,
|
|
]
|
|
}
|