mirror of
https://github.com/ysoftdevs/terraform-aws-eks.git
synced 2026-01-16 08:37:18 +01:00
311 lines
8.9 KiB
HCL
311 lines
8.9 KiB
HCL
provider "aws" {
|
|
region = local.region
|
|
}
|
|
|
|
provider "aws" {
|
|
region = "us-east-1"
|
|
alias = "virginia"
|
|
}
|
|
|
|
provider "helm" {
|
|
kubernetes {
|
|
host = module.eks.cluster_endpoint
|
|
cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
|
|
|
|
exec {
|
|
api_version = "client.authentication.k8s.io/v1beta1"
|
|
command = "aws"
|
|
# This requires the awscli to be installed locally where Terraform is executed
|
|
args = ["eks", "get-token", "--cluster-name", module.eks.cluster_name]
|
|
}
|
|
}
|
|
}
|
|
|
|
provider "kubectl" {
|
|
apply_retry_count = 5
|
|
host = module.eks.cluster_endpoint
|
|
cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
|
|
load_config_file = false
|
|
|
|
exec {
|
|
api_version = "client.authentication.k8s.io/v1beta1"
|
|
command = "aws"
|
|
# This requires the awscli to be installed locally where Terraform is executed
|
|
args = ["eks", "get-token", "--cluster-name", module.eks.cluster_name]
|
|
}
|
|
}
|
|
|
|
data "aws_availability_zones" "available" {}
|
|
data "aws_ecrpublic_authorization_token" "token" {
|
|
provider = aws.virginia
|
|
}
|
|
|
|
locals {
|
|
name = "ex-${replace(basename(path.cwd), "_", "-")}"
|
|
cluster_version = "1.29"
|
|
region = "eu-west-1"
|
|
|
|
vpc_cidr = "10.0.0.0/16"
|
|
azs = slice(data.aws_availability_zones.available.names, 0, 3)
|
|
|
|
tags = {
|
|
Example = local.name
|
|
GithubRepo = "terraform-aws-eks"
|
|
GithubOrg = "terraform-aws-modules"
|
|
}
|
|
}
|
|
|
|
################################################################################
|
|
# EKS Module
|
|
################################################################################
|
|
|
|
module "eks" {
|
|
source = "../.."
|
|
|
|
cluster_name = local.name
|
|
cluster_version = local.cluster_version
|
|
cluster_endpoint_public_access = true
|
|
|
|
# Gives Terraform identity admin access to cluster which will
|
|
# allow deploying resources (Karpenter) into the cluster
|
|
enable_cluster_creator_admin_permissions = true
|
|
|
|
cluster_addons = {
|
|
coredns = {
|
|
configuration_values = jsonencode({
|
|
computeType = "Fargate"
|
|
# Ensure that 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 targeting 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 targeting 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 = {}
|
|
vpc-cni = {}
|
|
}
|
|
|
|
vpc_id = module.vpc.vpc_id
|
|
subnet_ids = module.vpc.private_subnets
|
|
control_plane_subnet_ids = module.vpc.intra_subnets
|
|
|
|
# Fargate profiles use the cluster primary security group so these are not utilized
|
|
create_cluster_security_group = false
|
|
create_node_security_group = false
|
|
|
|
fargate_profiles = {
|
|
karpenter = {
|
|
selectors = [
|
|
{ namespace = "karpenter" }
|
|
]
|
|
}
|
|
kube-system = {
|
|
selectors = [
|
|
{ namespace = "kube-system" }
|
|
]
|
|
}
|
|
}
|
|
|
|
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
|
|
})
|
|
}
|
|
|
|
################################################################################
|
|
# Karpenter
|
|
################################################################################
|
|
|
|
module "karpenter" {
|
|
source = "../../modules/karpenter"
|
|
|
|
cluster_name = module.eks.cluster_name
|
|
|
|
# EKS Fargate currently does not support Pod Identity
|
|
enable_irsa = true
|
|
irsa_oidc_provider_arn = module.eks.oidc_provider_arn
|
|
|
|
# Used to attach additional IAM policies to the Karpenter node IAM role
|
|
node_iam_role_additional_policies = {
|
|
AmazonSSMManagedInstanceCore = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
|
|
}
|
|
|
|
tags = local.tags
|
|
}
|
|
|
|
module "karpenter_disabled" {
|
|
source = "../../modules/karpenter"
|
|
|
|
create = false
|
|
}
|
|
|
|
################################################################################
|
|
# Karpenter Helm chart & manifests
|
|
# Not required; just to demonstrate functionality of the sub-module
|
|
################################################################################
|
|
|
|
resource "helm_release" "karpenter" {
|
|
namespace = "karpenter"
|
|
create_namespace = true
|
|
name = "karpenter"
|
|
repository = "oci://public.ecr.aws/karpenter"
|
|
repository_username = data.aws_ecrpublic_authorization_token.token.user_name
|
|
repository_password = data.aws_ecrpublic_authorization_token.token.password
|
|
chart = "karpenter"
|
|
version = "v0.34.0"
|
|
wait = false
|
|
|
|
values = [
|
|
<<-EOT
|
|
settings:
|
|
clusterName: ${module.eks.cluster_name}
|
|
clusterEndpoint: ${module.eks.cluster_endpoint}
|
|
interruptionQueue: ${module.karpenter.queue_name}
|
|
serviceAccount:
|
|
annotations:
|
|
eks.amazonaws.com/role-arn: ${module.karpenter.iam_role_arn}
|
|
EOT
|
|
]
|
|
}
|
|
|
|
resource "kubectl_manifest" "karpenter_node_class" {
|
|
yaml_body = <<-YAML
|
|
apiVersion: karpenter.k8s.aws/v1beta1
|
|
kind: EC2NodeClass
|
|
metadata:
|
|
name: default
|
|
spec:
|
|
amiFamily: AL2
|
|
role: ${module.karpenter.node_iam_role_name}
|
|
subnetSelectorTerms:
|
|
- tags:
|
|
karpenter.sh/discovery: ${module.eks.cluster_name}
|
|
securityGroupSelectorTerms:
|
|
- tags:
|
|
karpenter.sh/discovery: ${module.eks.cluster_name}
|
|
tags:
|
|
karpenter.sh/discovery: ${module.eks.cluster_name}
|
|
YAML
|
|
|
|
depends_on = [
|
|
helm_release.karpenter
|
|
]
|
|
}
|
|
|
|
resource "kubectl_manifest" "karpenter_node_pool" {
|
|
yaml_body = <<-YAML
|
|
apiVersion: karpenter.sh/v1beta1
|
|
kind: NodePool
|
|
metadata:
|
|
name: default
|
|
spec:
|
|
template:
|
|
spec:
|
|
nodeClassRef:
|
|
name: default
|
|
requirements:
|
|
- key: "karpenter.k8s.aws/instance-category"
|
|
operator: In
|
|
values: ["c", "m", "r"]
|
|
- key: "karpenter.k8s.aws/instance-cpu"
|
|
operator: In
|
|
values: ["4", "8", "16", "32"]
|
|
- key: "karpenter.k8s.aws/instance-hypervisor"
|
|
operator: In
|
|
values: ["nitro"]
|
|
- key: "karpenter.k8s.aws/instance-generation"
|
|
operator: Gt
|
|
values: ["2"]
|
|
limits:
|
|
cpu: 1000
|
|
disruption:
|
|
consolidationPolicy: WhenEmpty
|
|
consolidateAfter: 30s
|
|
YAML
|
|
|
|
depends_on = [
|
|
kubectl_manifest.karpenter_node_class
|
|
]
|
|
}
|
|
|
|
# Example deployment using the [pause image](https://www.ianlewis.org/en/almighty-pause-container)
|
|
# and starts with zero replicas
|
|
resource "kubectl_manifest" "karpenter_example_deployment" {
|
|
yaml_body = <<-YAML
|
|
apiVersion: apps/v1
|
|
kind: Deployment
|
|
metadata:
|
|
name: inflate
|
|
spec:
|
|
replicas: 0
|
|
selector:
|
|
matchLabels:
|
|
app: inflate
|
|
template:
|
|
metadata:
|
|
labels:
|
|
app: inflate
|
|
spec:
|
|
terminationGracePeriodSeconds: 0
|
|
containers:
|
|
- name: inflate
|
|
image: public.ecr.aws/eks-distro/kubernetes/pause:3.7
|
|
resources:
|
|
requests:
|
|
cpu: 1
|
|
YAML
|
|
|
|
depends_on = [
|
|
helm_release.karpenter
|
|
]
|
|
}
|
|
|
|
################################################################################
|
|
# Supporting Resources
|
|
################################################################################
|
|
|
|
module "vpc" {
|
|
source = "terraform-aws-modules/vpc/aws"
|
|
version = "~> 5.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)]
|
|
intra_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 52)]
|
|
|
|
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
|
|
}
|