Skip to content

eks

Basic, production-oriented Amazon EKS cluster:

  • EKS control plane with Kubernetes secrets envelope-encrypted by a dedicated KMS key (rotation enabled).
  • All control plane log types sent to a KMS-encrypted CloudWatch log group (365 days retention by default).
  • Private API endpoint always on; public endpoint disabled by default (endpoint_public_access = false).
  • One managed node group (IMDSv2 only with hop limit 1, encrypted gp3 root volumes) with its IAM role. Pods use IRSA, not the node role.
  • IAM OIDC provider for IRSA, plus an IRSA role for cluster-autoscaler scoped to node groups tagged as owned by this cluster.
  • Optional Helm add-ons, each behind a flag: ingress-nginx (behind an NLB), cluster-autoscaler and cert-manager (with CRDs).

The module does not create the network: pass an existing vpc_id and subnet_ids (for example from the vpc module).

Usage

module "eks" {
  source = "github.com/rafaelcarvajal05/terraform-stacks//modules/eks?ref=v0.1.0"

  name       = "my-app"
  vpc_id     = "vpc-0123456789abcdef0"
  subnet_ids = ["subnet-aaaa", "subnet-bbbb", "subnet-cccc"]

  node_instance_types = ["t3.large"]
  node_min_size       = 2
  node_max_size       = 5
  node_desired_size   = 2

  tags = {
    Environment = "prod"
  }
}

Configuring the helm provider

The module installs add-ons with helm_release, but, as a reusable module, it does not configure any provider. Configure aws and helm in the root module and point helm at the cluster using the module outputs. Using exec avoids storing a short-lived token in the state:

provider "aws" {
  region = "us-east-1"
}

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"
      args        = ["eks", "get-token", "--cluster-name", module.eks.cluster_name]
    }
  }
}

With hashicorp/helm >= 3.0, kubernetes is an attribute instead of a block (kubernetes = { host = ..., exec = { ... } }).

If you also use the kubernetes provider in the root module, configure it with the same host, cluster_ca_certificate and exec arguments.

Notes:

  • Terraform must reach the API endpoint to install the add-ons. With the default private-only endpoint, run Terraform from inside the VPC (CI runner, bastion, VPN), or set endpoint_public_access = true with a restricted endpoint_public_access_cidrs. Alternatively set all enable_* flags to false and install the add-ons separately.
  • The identity that creates the cluster gets cluster-admin access. Grant access to other principals with EKS access entries or the aws-auth ConfigMap.
  • Node group desired_size changes are ignored after creation so cluster-autoscaler can manage it.

Requirements

Name Version
terraform >= 1.6.0
aws >= 5.0
helm >= 2.12
tls >= 4.0

Providers

Name Version
aws >= 5.0
helm >= 2.12
tls >= 4.0

Modules

No modules.

Resources

Name Type
aws_cloudwatch_log_group.this resource
aws_eks_cluster.this resource
aws_eks_node_group.default resource
aws_iam_openid_connect_provider.this resource
aws_iam_role.cluster resource
aws_iam_role.cluster_autoscaler resource
aws_iam_role.node resource
aws_iam_role_policy.cluster_autoscaler resource
aws_iam_role_policy.cluster_kms resource
aws_iam_role_policy_attachment.cluster resource
aws_iam_role_policy_attachment.node resource
aws_kms_alias.this resource
aws_kms_key.this resource
aws_launch_template.node resource
helm_release.cert_manager resource
helm_release.cluster_autoscaler resource
helm_release.ingress_nginx resource
aws_caller_identity.current data source
aws_iam_policy_document.cluster_assume_role data source
aws_iam_policy_document.cluster_autoscaler data source
aws_iam_policy_document.cluster_autoscaler_assume_role data source
aws_iam_policy_document.cluster_kms data source
aws_iam_policy_document.kms data source
aws_iam_policy_document.node_assume_role data source
aws_partition.current data source
aws_region.current data source
aws_subnets.vpc data source
tls_certificate.oidc data source

Inputs

Name Description Type Default Required
name Name of the EKS cluster. Also used as prefix for related resources (IAM roles, KMS alias, log group). string n/a yes
subnet_ids Subnet IDs (ideally private, in at least two AZs) for the control plane ENIs and the managed node group. list(string) n/a yes
vpc_id ID of the VPC where the cluster is created. string n/a yes
cert_manager_chart_version Version of the cert-manager Helm chart. string "v1.17.2" no
cluster_autoscaler_chart_version Version of the cluster-autoscaler Helm chart. string "9.46.6" no
cluster_log_retention_days Retention (days) of the control plane CloudWatch log group. number 365 no
cluster_log_types Control plane log types sent to CloudWatch Logs. list(string)
[
"api",
"audit",
"authenticator",
"controllerManager",
"scheduler"
]
no
enable_cert_manager Install cert-manager (including its CRDs). bool true no
enable_cluster_autoscaler Install cluster-autoscaler with an IRSA role scoped to this cluster's node groups. bool true no
enable_ingress_nginx Install the ingress-nginx controller (exposed through an AWS Network Load Balancer). bool true no
endpoint_public_access Whether the Kubernetes API endpoint is reachable from the internet. The private endpoint is always enabled. bool false no
endpoint_public_access_cidrs CIDR blocks allowed to reach the public API endpoint when endpoint_public_access is true. Restrict it; do not use 0.0.0.0/0 in production. list(string) [] no
ingress_nginx_chart_version Version of the ingress-nginx Helm chart. string "4.12.1" no
kms_key_deletion_window_in_days Waiting period (days) before the KMS key used for secrets and logs encryption is deleted. number 30 no
kubernetes_version Kubernetes version of the EKS control plane (for example "1.33"). string "1.33" no
node_desired_size Initial desired number of nodes. Changes after creation are ignored so cluster-autoscaler can manage it. number 2 no
node_disk_size Root EBS volume size (GiB) of each node. The volume is always encrypted. number 50 no
node_instance_types EC2 instance types for the managed node group. list(string)
[
"t3.medium"
]
no
node_max_size Maximum number of nodes in the managed node group. number 3 no
node_min_size Minimum number of nodes in the managed node group. number 1 no
tags Tags applied to all AWS resources created by the module. map(string) {} no

Outputs

Name Description
cluster_arn ARN of the EKS cluster.
cluster_autoscaler_role_arn ARN of the cluster-autoscaler IRSA role (null when enable_cluster_autoscaler is false).
cluster_certificate_authority_data Base64-encoded certificate authority data of the cluster.
cluster_endpoint Endpoint of the Kubernetes API server.
cluster_name Name of the EKS cluster.
cluster_security_group_id ID of the cluster security group created by EKS.
kms_key_arn ARN of the KMS key used for secrets and log encryption.
node_group_role_arn ARN of the IAM role used by the managed node group.
oidc_issuer_url OIDC issuer URL of the cluster.
oidc_provider_arn ARN of the IAM OIDC provider used for IRSA.