DevOps Hive

Patterns / Deploy an app

EKS cluster with a managed node group in private subnets

A Kubernetes cluster whose worker nodes have no public addresses and whose API only answers the address ranges you list, with the two IAM roles it needs and nothing wider.

  1. kubectl
  2. Cluster API
  3. Managed node group
  4. Pods in private subnets

How it works

  1. What the quick-start cluster leaves open

    A cluster made with default settings has an API endpoint that anyone on the internet can reach and try to log in to. Worker nodes often end up in public subnets too. This pattern puts the nodes in private subnets and limits the API to the address ranges you choose.

  2. Bring a VPC with private subnets

    Pass the IDs of private subnets in at least two availability zones. They need a route to a NAT gateway, because nodes download container images and reach AWS services when they start. Without that route the nodes never join the cluster, and the node group fails after about 20 minutes.

  3. Create the two IAM roles

    The cluster role lets the EKS service manage networking for the cluster. The node role lets each worker join the cluster and pull images. The mistake people make is adding application permissions, such as S3 access, to the node role: every pod on every node then has them. Give a pod its own role with EKS Pod Identity.

  4. Create the cluster and limit its API

    Set api_allowed_cidrs to the addresses you work from, such as your office or VPN address. The configuration refuses 0.0.0.0/0. Pass an empty list to switch the public endpoint off, and then run kubectl from inside the VPC or over a VPN. Kubernetes secrets are encrypted with a KMS key before the cluster stores them.

  5. Add the node group

    A managed node group is a set of EC2 instances that EKS creates, replaces and upgrades for you. Nodes use encrypted disks and require IMDSv2, with a hop limit that stops pods from reading the node's credentials. The default is two t3.medium nodes, which is enough to try things and too small for most production loads.

  6. Connect and give others access

    Run the command in the kubeconfig_command output, then kubectl get nodes. At first only the identity that created the cluster can use it. Add an EKS access entry for each person or deploy role that needs access, before the creator is on holiday.

What you set

cluster_name
Name of the cluster For example acme-prod
kubernetes_version
A Kubernetes version that EKS lists under standard support For example 1.33
private_subnet_ids
Private subnets in at least two availability zones For example ["subnet-0123456789abcdef0", "subnet-0fedcba9876543210"]
api_allowed_cidrs
Address ranges allowed to reach the cluster API For example ["203.0.113.10/32"]

What it costs to run

About 140 to 160 dollars a month for the defaults: roughly 73 dollars for the cluster itself and the rest for two t3.medium nodes and their disks. This does not include the NAT gateway, load balancers or data transfer. Estimate.

The configuration

# An EKS cluster with one managed node group in private subnets.
#
# What this creates:
#   - an IAM role for the cluster and one for the worker nodes
#   - a KMS key that encrypts Kubernetes secrets stored by the cluster
#   - the EKS cluster, with its API endpoint limited to the address ranges you list
#   - a managed node group in your private subnets
#
# It expects a VPC with private subnets in at least two availability zones
# that can reach the internet through a NAT gateway.

terraform {
  required_version = ">= 1.6.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = var.region
}

variable "region" {
  description = "AWS region to deploy into."
  type        = string
  default     = "us-east-1"
}

variable "cluster_name" {
  description = "Name of the EKS cluster."
  type        = string
}

variable "kubernetes_version" {
  description = "Kubernetes version, for example 1.33. Pick one that EKS lists under standard support: versions in extended support are billed at a higher hourly rate."
  type        = string
}

variable "private_subnet_ids" {
  description = "IDs of private subnets in at least two availability zones. The cluster and the nodes are placed here."
  type        = list(string)

  validation {
    condition     = length(var.private_subnet_ids) >= 2
    error_message = "Pass at least two private subnets, in different availability zones."
  }
}

variable "api_allowed_cidrs" {
  description = "Address ranges allowed to reach the cluster API over the internet, such as your office or VPN address. Pass an empty list to switch the public endpoint off and reach the API from inside the VPC only."
  type        = list(string)

  validation {
    condition     = !contains(var.api_allowed_cidrs, "0.0.0.0/0")
    error_message = "0.0.0.0/0 opens the cluster API to the whole internet. List specific address ranges."
  }
}

variable "node_instance_types" {
  description = "Instance types for the worker nodes."
  type        = list(string)
  default     = ["t3.medium"]
}

variable "node_desired_size" {
  description = "Number of worker nodes to run."
  type        = number
  default     = 2
}

variable "node_min_size" {
  description = "Smallest number of worker nodes."
  type        = number
  default     = 2
}

variable "node_max_size" {
  description = "Largest number of worker nodes."
  type        = number
  default     = 4
}

variable "node_disk_size_gb" {
  description = "Size of each node's root volume in GB."
  type        = number
  default     = 30
}

# Role for the cluster itself (the control plane that AWS runs for you).

data "aws_iam_policy_document" "cluster_assume" {
  statement {
    effect  = "Allow"
    actions = ["sts:AssumeRole"]

    principals {
      type        = "Service"
      identifiers = ["eks.amazonaws.com"]
    }
  }
}

resource "aws_iam_role" "cluster" {
  name               = "${var.cluster_name}-cluster"
  assume_role_policy = data.aws_iam_policy_document.cluster_assume.json
}

resource "aws_iam_role_policy_attachment" "cluster" {
  role       = aws_iam_role.cluster.name
  policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
}

# Role for the worker nodes. These three policies are what a node needs to
# join the cluster, set up pod networking and pull images. Do not add
# application permissions here: every pod on the node would share them.

data "aws_iam_policy_document" "node_assume" {
  statement {
    effect  = "Allow"
    actions = ["sts:AssumeRole"]

    principals {
      type        = "Service"
      identifiers = ["ec2.amazonaws.com"]
    }
  }
}

resource "aws_iam_role" "node" {
  name               = "${var.cluster_name}-node"
  assume_role_policy = data.aws_iam_policy_document.node_assume.json
}

resource "aws_iam_role_policy_attachment" "node_worker" {
  role       = aws_iam_role.node.name
  policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
}

resource "aws_iam_role_policy_attachment" "node_cni" {
  role       = aws_iam_role.node.name
  policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
}

resource "aws_iam_role_policy_attachment" "node_ecr" {
  role       = aws_iam_role.node.name
  policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
}

# Kubernetes secrets are only base64 encoded by default. With this key, EKS
# encrypts them before they are written to the cluster's database.
resource "aws_kms_key" "secrets" {
  description             = "Encrypts Kubernetes secrets for the ${var.cluster_name} cluster"
  enable_key_rotation     = true
  deletion_window_in_days = 30
}

resource "aws_kms_alias" "secrets" {
  name          = "alias/${var.cluster_name}-eks-secrets"
  target_key_id = aws_kms_key.secrets.key_id
}

resource "aws_eks_cluster" "this" {
  name     = var.cluster_name
  role_arn = aws_iam_role.cluster.arn
  version  = var.kubernetes_version

  vpc_config {
    subnet_ids = var.private_subnet_ids

    # Nodes talk to the API over the private endpoint, inside the VPC.
    endpoint_private_access = true

    # The public endpoint answers only the listed address ranges. With an
    # empty list it is switched off.
    endpoint_public_access = length(var.api_allowed_cidrs) > 0
    public_access_cidrs    = length(var.api_allowed_cidrs) > 0 ? var.api_allowed_cidrs : null
  }

  encryption_config {
    resources = ["secrets"]

    provider {
      key_arn = aws_kms_key.secrets.arn
    }
  }

  # Lets you grant other people and roles access with EKS access entries. The
  # identity that creates the cluster starts as its administrator.
  access_config {
    authentication_mode                         = "API_AND_CONFIG_MAP"
    bootstrap_cluster_creator_admin_permissions = true
  }

  # The role needs its policy before the cluster is created, and must keep it
  # until the cluster is deleted.
  depends_on = [aws_iam_role_policy_attachment.cluster]
}

resource "aws_launch_template" "node" {
  name_prefix = "${var.cluster_name}-node-"

  # IMDSv2 only, with a hop limit of 1: pods cannot read the node's IAM
  # credentials from the instance metadata service. Give pods AWS access with
  # EKS Pod Identity or IAM roles for service accounts.
  metadata_options {
    http_endpoint               = "enabled"
    http_tokens                 = "required"
    http_put_response_hop_limit = 1
  }

  block_device_mappings {
    device_name = "/dev/xvda"

    ebs {
      volume_size           = var.node_disk_size_gb
      volume_type           = "gp3"
      encrypted             = true
      delete_on_termination = true
    }
  }

  tag_specifications {
    resource_type = "instance"

    tags = {
      Name = "${var.cluster_name}-node"
    }
  }
}

resource "aws_eks_node_group" "this" {
  cluster_name    = aws_eks_cluster.this.name
  node_group_name = "default"
  node_role_arn   = aws_iam_role.node.arn
  subnet_ids      = var.private_subnet_ids
  instance_types  = var.node_instance_types
  ami_type        = "AL2023_x86_64_STANDARD"

  scaling_config {
    desired_size = var.node_desired_size
    min_size     = var.node_min_size
    max_size     = var.node_max_size
  }

  # Replace one node at a time during an upgrade, so the rest keep serving.
  update_config {
    max_unavailable = 1
  }

  launch_template {
    id      = aws_launch_template.node.id
    version = aws_launch_template.node.latest_version
  }

  # Nodes cannot join without these, and removing them first on destroy leaves
  # network interfaces behind.
  depends_on = [
    aws_iam_role_policy_attachment.node_worker,
    aws_iam_role_policy_attachment.node_cni,
    aws_iam_role_policy_attachment.node_ecr,
  ]
}

output "cluster_name" {
  description = "Name of the cluster."
  value       = aws_eks_cluster.this.name
}

output "cluster_endpoint" {
  description = "URL of the cluster API."
  value       = aws_eks_cluster.this.endpoint
}

output "cluster_oidc_issuer" {
  description = "OIDC issuer URL, needed if you set up IAM roles for service accounts."
  value       = aws_eks_cluster.this.identity[0].oidc[0].issuer
}

output "node_role_arn" {
  description = "IAM role the worker nodes run as."
  value       = aws_iam_role.node.arn
}

output "kubeconfig_command" {
  description = "Run this to point kubectl at the cluster."
  value       = "aws eks update-kubeconfig --region ${var.region} --name ${aws_eks_cluster.this.name}"
}

Works with OpenTofu 1.6 or later, and with Terraform. Read it before you apply it: it creates real resources in your account.

Written by DevOps Hive. Last updated 2026-11-27.

More in deploy an app