locals { name = var.name region = var.aws_region vpc_cidr = var.vpc_cidr azs = slice(data.aws_availability_zones.available.names, 0, 3) tags = { Blueprint = var.name GithubRepo = "github.com/aws-ia/terraform-aws-eks-blueprints" } } ################################################################################ # Supporting Resources ################################################################################ module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "~> 4.0" name = local.name cidr = local.vpc_cidr azs = local.azs private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 4, k)] public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 48)] enable_nat_gateway = true single_nat_gateway = true public_subnet_tags = { "kubernetes.io/role/elb" = 1 } private_subnet_tags = { "kubernetes.io/role/internal-elb" = 1 # Tags subnets for Karpenter auto-discovery "karpenter.sh/discovery" = local.name } tags = local.tags } ################################################################################ # Cluster ################################################################################ module "eks" { source = "terraform-aws-modules/eks/aws" version = "~> 19.12" cluster_name = local.name cluster_version = var.cluster_version cluster_endpoint_public_access = true cluster_addons = { aws-ebs-csi-driver = { most_recent = true } coredns = { most_recent = true configuration_values = jsonencode({ computeType = "Fargate" # Ensure that the we fully utilize the minimum amount of resources that are supplied by # Fargate https://docs.aws.amazon.com/eks/latest/userguide/fargate-pod-configuration.html # Fargate adds 256 MB to each pod's memory reservation for the required Kubernetes # components (kubelet, kube-proxy, and containerd). Fargate rounds up to the following # compute configuration that most closely matches the sum of vCPU and memory requests in # order to ensure pods always have the resources that they need to run. resources = { limits = { cpu = "0.25" # We are targetting the smallest Task size of 512Mb, so we subtract 256Mb from the # request/limit to ensure we can fit within that task memory = "256M" } requests = { cpu = "0.25" # We are targetting the smallest Task size of 512Mb, so we subtract 256Mb from the # request/limit to ensure we can fit within that task memory = "256M" } } }) } kube-proxy = { most_recent = true } vpc-cni = { most_recent = true before_compute = true configuration_values = jsonencode({ env = { # Reference docs https://docs.aws.amazon.com/eks/latest/userguide/cni-increase-ip-addresses.html ENABLE_PREFIX_DELEGATION = "true" WARM_PREFIX_TARGET = "1" } }) } } vpc_id = module.vpc.vpc_id subnet_ids = module.vpc.private_subnets # Fargate profiles use the cluster primary security group so these are not utilized create_cluster_security_group = false create_node_security_group = false manage_aws_auth_configmap = true aws_auth_roles = [ # We need to add in the Karpenter node IAM role for nodes launched by Karpenter { rolearn = aws_iam_role.karpenter_node_role.arn username = "system:node:{{EC2PrivateDNSName}}" groups = [ "system:bootstrappers", "system:nodes", ] }, ] fargate_profiles = { karpenter = { selectors = [ { namespace = "karpenter" } ] } kube_system = { name = "kube-system" selectors = [ { namespace = "kube-system" } ] } flux-system = { selectors = [ { namespace = "flux-system" } ] } argo-workflows = { selectors = [ { namespace = "argo-workflows" } ] } } tags = merge(local.tags, { # NOTE - if creating multiple security groups with this module, only tag the # security group that Karpenter should utilize with the following tag # (i.e. - at most, only one security group should have this tag in your account) "karpenter.sh/discovery" = local.name }) } ################################################################################ # Flux ################################################################################ module "flux_v2" { source = "../modules/flux_cd" cluster_endpoint = module.eks.cluster_endpoint ca = module.eks.cluster_certificate_authority_data token = data.aws_eks_cluster_auth.this.token git_branch = var.git_branch git_username = var.git_username git_password = var.git_password git_url = var.git_url }