AWS Builder Center
EKS Infrastructure with Terraform

EKS Infrastructure with Terraform

Production-ready Amazon EKS cluster with complete infrastructure automation

πŸš€ EKS Infrastructure with Terraform

Build and deploy a production-ready Amazon EKS environment using Terraform, with a modular architecture that includes a multi-AZ VPC, private worker nodes, IAM integration, managed EKS add-ons, Cluster Autoscaler, and a secure bastion host for cluster management.

πŸ—οΈ Architecture Overview

This project provides a modular Terraform setup for deploying an Amazon EKS cluster together with the supporting AWS infrastructure required to operate it securely.
The architecture includes:
  • Multi-AZ VPC
  • Public and private subnets
  • NAT Gateway per Availability Zone
  • Amazon EKS cluster
  • EKS Managed Node Groups
  • IAM roles and policies
  • EKS managed add-ons
  • Secure bastion host
  • Cluster Autoscaler
  • Restricted access to the EKS API endpoint

Key Features

Feature
Description
🌐 Multi-AZ VPC
High availability across multiple Availability Zones
πŸ” Secure Access
Bastion host with restricted SSH access
πŸ“ˆ Auto Scaling
Cluster Autoscaler integration
βš™οΈ Managed Add-ons
CoreDNS, VPC CNI, kube-proxy, and EKS Pod Identity Agent
πŸ”‘ IAM Integration
AWS IAM-based authentication and authorization
πŸ›‘οΈ Network Isolation
EKS worker nodes deployed in private subnets

ο»Ώ

🧩 Components

The infrastructure is divided into three main Terraform modules:
modules/
β”œβ”€β”€ vpc/
β”œβ”€β”€ eks/
└── bastion/
ο»Ώ
This modular approach makes the infrastructure easier to maintain, reuse, and extend.

ο»Ώ

1. 🌐 VPC Module

The VPC module is responsible for creating the networking infrastructure required by EKS.
It creates:
  • VPC
  • Public subnets
  • Private subnets
  • Internet Gateway
  • NAT Gateways
  • Route tables
  • Security groups
  • Network ACLs
  • EKS-specific subnet tags

Network Layout

The VPC uses:
VPC CIDR: 10.0.0.0/16
ο»Ώ

Public Subnets

Public subnets are used for resources that require internet connectivity, such as the bastion host and AWS Load Balancers.
10.0.101.0/24
10.0.102.0/24
10.0.103.0/24
ο»Ώ

Private Subnets

EKS worker nodes are deployed into private subnets:
10.0.1.0/24
10.0.2.0/24
10.0.3.0/24
ο»Ώ
The worker nodes do not require public IP addresses. Outbound internet connectivity is provided through NAT Gateways.

NAT Gateways

A NAT Gateway is deployed in each Availability Zone.
This allows resources in private subnets to access external services while preventing direct inbound internet access to the worker nodes.
The simplified architecture looks like this:
Internet
|
Internet Gateway
|
+-----------------+-----------------+
| |
Public Subnet Public Subnet
| |
NAT Gateway NAT Gateway
| |
+-----------------+-----------------+
|
Private Subnets
|
+-------------+-------------+
| | |
AZ-a AZ-b AZ-c
| | |
EKS Nodes EKS Nodes EKS Nodes
ο»Ώ

ο»Ώ

2. ☸️ EKS Module

The EKS module provisions the Kubernetes control plane and managed worker nodes.
The cluster uses Amazon EKS Managed Node Groups, allowing AWS to manage the underlying EC2 worker-node lifecycle.

EKS Managed Add-ons

The cluster includes the following essential add-ons:
  • CoreDNS
  • Amazon VPC CNI
  • kube-proxy
  • EKS Pod Identity Agent
These components provide core Kubernetes DNS, networking, service networking, and AWS workload identity functionality.

ο»Ώ

πŸ”‘ IAM Roles and Users

IAM is an important part of the EKS architecture because AWS IAM controls access to AWS resources while Kubernetes RBAC controls access to Kubernetes resources.
The project includes the following IAM components.

EKS Manager User

An IAM user is used for programmatic EKS management.

EKS Manager Role

An IAM role can be assumed by the bastion host, allowing administrators to manage the EKS cluster without embedding long-lived credentials directly into the EC2 instance.

Cluster Autoscaler IAM Role

The Cluster Autoscaler requires AWS permissions to modify EKS node groups.
A dedicated IAM role is provided for the Kubernetes service account used by Cluster Autoscaler.
This follows the principle of least privilege by giving the workload only the AWS permissions it requires.

ο»Ώ

3. πŸ–₯️ Bastion Host Module

The bastion host provides a controlled management point for accessing the EKS environment.
The EC2 instance is deployed into a public subnet and is preconfigured with:
  • AWS CLI
  • kubectl
  • Helm
  • EKS management configuration
  • EKS manager user
The worker nodes remain inside private subnets.
The management flow is:
Administrator
|
| SSH
v
Bastion Host
|
| AWS IAM authentication
v
Amazon EKS API
|
v
Private EKS Worker Nodes
ο»Ώ

ο»Ώ

⚑ Prerequisites

Before deploying the infrastructure, make sure the following requirements are available:
Requirement
Version / Configuration
AWS CLI
Latest version recommended
Terraform
>= 1.0
AWS Account
Required
AWS Credentials
Configured locally
EC2 Key Pair
Must exist in the target AWS Region
Public IP
Required for restricting access
You can check your public IP using:
curl ifconfig.me
ο»Ώ

ο»Ώ

πŸš€ Deployment

1. Clone the Repository

Clone the Terraform project:
git clone https://github.com/khaledhawil/Terraform-Eks-Infrastructure.git
ο»Ώ
cd Terraform-Eks-Infrastructure
ο»Ώ

ο»Ώ

2. Configure Terraform Variables

Create or update Β terraform.tfvarsΒ :
# Region and Cluster Configuration
aws_region = "eu-north-1"
cluster_name = "myapp-eks-cluster"
ο»Ώ
# Security Configuration
endpoint_public_access_cidrs = ["YOUR_PUBLIC_IP/32"]
bastion_allowed_cidrs = ["YOUR_PUBLIC_IP/32"]
ο»Ώ
# SSH Key Configuration
key_pair_name = "your-existing-key-pair"
ο»Ώ
# Network Configuration
vpc_cidr_block = "10.0.0.0/16"
ο»Ώ
private_subnet_cidr_blocks = [
"10.0.1.0/24",
"10.0.2.0/24",
"10.0.3.0/24"
]
ο»Ώ
public_subnet_cidr_blocks = [
"10.0.101.0/24",
"10.0.102.0/24",
"10.0.103.0/24"
]
ο»Ώ
# Resource Tags
common_tags = {
Environment = "development"
Project = "myapp"
ManagedBy = "terraform"
Owner = "your-name"
}
ο»Ώ
Replace:
YOUR_PUBLIC_IP
ο»Ώ
with your actual public IP address.
Using Β /32Β  restricts access to a single IPv4 address.

ο»Ώ

3. Create an SSH Key Pair

If you do not already have an EC2 key pair in the target AWS Region, you can create one using the AWS CLI:
aws ec2 create-key-pair \
--key-name myapp-bastion-key \
--query 'KeyMaterial' \
--output text > ~/.ssh/myapp-bastion-key.pem
ο»Ώ
Set the correct file permissions:
chmod 400 ~/.ssh/myapp-bastion-key.pem
ο»Ώ
Then update:
key_pair_name = "myapp-bastion-key"
ο»Ώ

ο»Ώ

4. Initialize Terraform

Initialize the Terraform working directory:
terraform init
ο»Ώ
Terraform will download the required providers and initialize the modules.

ο»Ώ

5. Review the Terraform Plan

Before creating any AWS resources, review the execution plan:
terraform plan
ο»Ώ
Review the resources carefully before proceeding.

ο»Ώ

6. Deploy the Infrastructure

Apply the configuration:
terraform apply
ο»Ώ
Terraform will provision the VPC, networking components, EKS cluster, managed node groups, IAM resources, and bastion host.
The deployment can take approximately 15–20 minutes, depending on AWS resource provisioning.

ο»Ώ

🎯 Access the EKS Cluster

After Terraform finishes, retrieve the bastion public IP:
terraform output bastion_public_ip
ο»Ώ
Connect to the bastion host:
ssh -i ~/.ssh/your-key.pem \
ec2-user@$(terraform output -raw bastion_public_ip)
ο»Ώ
Switch to the EKS manager user:
sudo su - eks-manager
ο»Ώ
Verify access to the Kubernetes cluster:
kubectl get nodes
ο»Ώ
You can also check all Kubernetes workloads:
kubectl get pods --all-namespaces
ο»Ώ
If the deployment was successful, the worker nodes should appear with a Β ReadyΒ  status.

ο»Ώ

πŸ€– Deploy Cluster Autoscaler

The project generates a Cluster Autoscaler deployment configuration.
From the bastion host, switch to the Β eks-managerΒ  user and deploy it:
kubectl apply \
-f /home/eks-manager/cluster-autoscaler.yaml
ο»Ώ
Verify the deployment:
kubectl get deployment cluster-autoscaler \
-n kube-system
ο»Ώ
Check the logs:
kubectl logs \
deployment/cluster-autoscaler \
-n kube-system
ο»Ώ

ο»Ώ

πŸ’‘ Usage Examples

Managing the Cluster from the Bastion Host

After connecting to the bastion:
sudo su - eks-manager
ο»Ώ
Check the cluster:
kubectl get nodes
kubectl get pods
kubectl get services
ο»Ώ
Verify the AWS identity:
aws sts get-caller-identity
ο»Ώ

ο»Ώ

Deploy a Test Application

Create an NGINX deployment:
kubectl create deployment nginx \
--image=nginx \
--replicas=3
ο»Ώ
Expose the deployment using a LoadBalancer:
kubectl expose deployment nginx \
--port=80 \
--type=LoadBalancer
ο»Ώ
Check the service:
kubectl get service nginx
ο»Ώ
AWS will provision the corresponding Load Balancer.

ο»Ώ

Test Cluster Scaling

Increase the number of replicas:
kubectl scale deployment nginx \
--replicas=10
ο»Ώ
Check the workload:
kubectl get pods
ο»Ώ
If the existing nodes do not have enough capacity to schedule the additional pods, Cluster Autoscaler can increase the size of the EKS node group.

ο»Ώ

πŸ’» Managing the Cluster from Your Local Machine

If your current public IP is allowed by the EKS API endpoint configuration, you can configure Β kubectlΒ  locally:
aws eks update-kubeconfig \
--region eu-north-1 \
--name myapp-eks-cluster
ο»Ώ
Then verify connectivity:
kubectl get nodes
ο»Ώ
This allows Kubernetes administration without connecting through the bastion host, provided the EKS API access policy permits your source IP.

ο»Ώ

πŸ”’ Security Features

Security is implemented at multiple layers of the infrastructure.
Security Layer
Implementation
🌐 Network Isolation
EKS worker nodes run in private subnets
🚧 EKS API Protection
API access restricted to specified CIDRs
πŸ” IAM Integration
AWS IAM used for authentication and authorization
πŸ›‘οΈ Bastion Security
SSH access restricted to allowed source IPs
🎭 Workload Identity
IAM roles are used for Kubernetes workloads
πŸ”‘ Least Privilege
Dedicated IAM roles for specific workloads
The worker nodes are not directly exposed to the public internet.
The bastion host provides a controlled administrative entry point, while the EKS API endpoint can be restricted to trusted source IP addresses.

ο»Ώ

πŸ“Š Monitoring and Troubleshooting

Check Cluster Status

Retrieve detailed information about the EKS cluster:
aws eks describe-cluster \
--name myapp-eks-cluster
ο»Ώ

ο»Ώ

Check Node Group Status

aws eks describe-nodegroup \
--cluster-name myapp-eks-cluster \
--nodegroup-name main
ο»Ώ

ο»Ώ

Check Kubernetes System Pods

kubectl get pods \
-n kube-system
ο»Ώ
This is useful for verifying components such as CoreDNS, kube-proxy, VPC CNI, and Cluster Autoscaler.

ο»Ώ

Check Cluster Autoscaler Logs

kubectl logs \
deployment/cluster-autoscaler \
-n kube-system
ο»Ώ

ο»Ώ

Describe a Problematic Pod

kubectl describe pod \
<pod-name> \
-n <namespace>
ο»Ώ
This can reveal scheduling errors, image-pull failures, resource constraints, volume problems, and other pod-level issues.

ο»Ώ

Check Kubernetes Events

kubectl get events \
--sort-by=.metadata.creationTimestamp
ο»Ώ
Cluster events are particularly useful when troubleshooting scheduling, node provisioning, and workload deployment problems.

ο»Ώ

🧠 Key Takeaways

This project demonstrates how Terraform can be used to automate the deployment of an Amazon EKS environment while keeping infrastructure components modular and manageable.
The main concepts covered are:
  • Building a multi-AZ AWS VPC for EKS
  • Using public and private subnets
  • Deploying EKS worker nodes in private subnets
  • Using NAT Gateways for outbound connectivity
  • Deploying EKS Managed Node Groups
  • Configuring EKS managed add-ons
  • Integrating AWS IAM with Kubernetes access
  • Restricting access to the EKS API
  • Using a bastion host for cluster administration
  • Configuring Cluster Autoscaler
  • Managing the complete infrastructure using Terraform
The main lesson is that an EKS environment is more than the Kubernetes control plane itself. A production-oriented deployment requires networking, IAM, security, compute, scalability, and operational access to be designed together.

πŸ”— Project Repository

The complete Terraform implementation is available on GitHub:
Feel free to explore the repository, review the Terraform modules, and use the architecture as a starting point for your own EKS infrastructure projects.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article