
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/2410.0.102.0/2410.0.103.0/24ο»Ώ
Private Subnets
EKS worker nodes are deployed into private subnets:
10.0.1.0/2410.0.2.0/2410.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 vBastion Host | | AWS IAM authentication vAmazon EKS API | vPrivate 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 Configurationaws_region = "eu-north-1"cluster_name = "myapp-eks-cluster"ο»Ώ
# Security Configurationendpoint_public_access_cidrs = ["YOUR_PUBLIC_IP/32"]bastion_allowed_cidrs = ["YOUR_PUBLIC_IP/32"]ο»Ώ
# SSH Key Configurationkey_pair_name = "your-existing-key-pair"ο»Ώ
# Network Configurationvpc_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 Tagscommon_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 nodeskubectl get podskubectl 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.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article