|
| 1 | +################################################################################ |
| 2 | +# EKS Cluster |
| 3 | +################################################################################ |
| 4 | + |
| 5 | +module "eks" { |
| 6 | + source = "terraform-aws-modules/eks/aws" |
| 7 | + version = "~> 19.15" |
| 8 | + |
| 9 | + cluster_name = local.name |
| 10 | + cluster_version = "1.27" |
| 11 | + |
| 12 | + cluster_addons = { |
| 13 | + coredns = {} |
| 14 | + kube-proxy = {} |
| 15 | + vpc-cni = {} |
| 16 | + } |
| 17 | + |
| 18 | + cluster_security_group_additional_rules = { |
| 19 | + # Allow tcp/443 from the NLB IP addresses |
| 20 | + for ip_addr in data.dns_a_record_set.nlb.addrs : "nlb_ingress_${replace(ip_addr, ".", "")}" => { |
| 21 | + description = "Allow ingress from NLB" |
| 22 | + type = "ingress" |
| 23 | + from_port = 443 |
| 24 | + to_port = 443 |
| 25 | + protocol = "tcp" |
| 26 | + cidr_blocks = ["${ip_addr}/32"] |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + vpc_id = module.vpc.vpc_id |
| 31 | + subnet_ids = module.vpc.private_subnets |
| 32 | + |
| 33 | + eks_managed_node_groups = { |
| 34 | + default = { |
| 35 | + instance_types = ["c5.large"] |
| 36 | + |
| 37 | + min_size = 1 |
| 38 | + max_size = 3 |
| 39 | + desired_size = 1 |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + tags = local.tags |
| 44 | +} |
| 45 | + |
| 46 | +################################################################################ |
| 47 | +# VPC |
| 48 | +################################################################################ |
| 49 | + |
| 50 | +module "vpc" { |
| 51 | + source = "terraform-aws-modules/vpc/aws" |
| 52 | + version = "~> 5.0" |
| 53 | + |
| 54 | + name = local.name |
| 55 | + cidr = local.vpc_cidr |
| 56 | + |
| 57 | + azs = local.azs |
| 58 | + private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 4, k)] |
| 59 | + |
| 60 | + # Disable NAT gateway for fully private networking |
| 61 | + enable_nat_gateway = false |
| 62 | + |
| 63 | + private_subnet_tags = { |
| 64 | + "kubernetes.io/role/internal-elb" = 1 |
| 65 | + } |
| 66 | + |
| 67 | + tags = local.tags |
| 68 | +} |
| 69 | + |
| 70 | +# Explicitly create a Internet Gateway here in the Private EKS VPC as without an |
| 71 | +# internet gateway, a NLB cannot be created. Config option of create_igw = true |
| 72 | +# (default) did not work during the VPC creation as it requires public subnets |
| 73 | +# and the related routes that connect them to IGW |
| 74 | +resource "aws_internet_gateway" "igw" { |
| 75 | + vpc_id = module.vpc.vpc_id |
| 76 | + tags = local.tags |
| 77 | +} |
| 78 | + |
| 79 | +################################################################################ |
| 80 | +# VPC Endpoints |
| 81 | +################################################################################ |
| 82 | + |
| 83 | +module "vpc_endpoints" { |
| 84 | + source = "terraform-aws-modules/vpc/aws//modules/vpc-endpoints" |
| 85 | + version = "~> 5.1" |
| 86 | + |
| 87 | + vpc_id = module.vpc.vpc_id |
| 88 | + |
| 89 | + # Security group |
| 90 | + create_security_group = true |
| 91 | + security_group_name_prefix = "${local.name}-vpc-endpoints-" |
| 92 | + security_group_description = "VPC endpoint security group" |
| 93 | + security_group_rules = { |
| 94 | + ingress_https = { |
| 95 | + description = "HTTPS from VPC" |
| 96 | + cidr_blocks = [module.vpc.vpc_cidr_block] |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + endpoints = merge({ |
| 101 | + s3 = { |
| 102 | + service = "s3" |
| 103 | + service_type = "Gateway" |
| 104 | + route_table_ids = module.vpc.private_route_table_ids |
| 105 | + tags = { |
| 106 | + Name = "${local.name}-s3" |
| 107 | + } |
| 108 | + } |
| 109 | + }, |
| 110 | + { for service in toset(["autoscaling", "ecr.api", "ecr.dkr", "ec2", "ec2messages", "elasticloadbalancing", "sts", "kms", "logs", "ssm", "ssmmessages"]) : |
| 111 | + replace(service, ".", "_") => |
| 112 | + { |
| 113 | + service = service |
| 114 | + subnet_ids = module.vpc.private_subnets |
| 115 | + private_dns_enabled = true |
| 116 | + tags = { Name = "${local.name}-${service}" } |
| 117 | + } |
| 118 | + }) |
| 119 | + |
| 120 | + tags = local.tags |
| 121 | +} |
0 commit comments