Of course! Setting up an AWS EKS cluster using a YAML configuration file is the recommended and most powerful way to use eksctl, the official CLI for EKS. This approach gives you a declarative, version-controllable definition of your cluster infrastructure.
Here is a complete step-by-step guide, from prerequisites to cleanup.
Why Use eksctl with a YAML file?
- Declarative: You define the desired state of your cluster, and
eksctl figures out how to make it happen.
- Repeatable: You can create identical clusters in different regions or AWS accounts using the same file.
- Version Controlled: You can check your cluster configuration into Git, track changes, and collaborate with your team.
- Simplicity: It automates the creation of all necessary underlying resources like VPCs, Subnets, IAM Roles, Security Groups, and Node Groups, which is incredibly complex to do manually.
Step 1: Prerequisites
Before you begin, you need to have the following tools installed and configured on your local machine.
- AWS Account: An active AWS account.
- IAM Permissions: An IAM user with sufficient permissions to create EKS clusters and related resources (VPC, EC2, IAM roles, etc.). Using the
AdministratorAccess policy is the easiest for a test setup, but for production, you should use more fine-grained permissions.
- AWS CLI: Install and configure the AWS CLI. Run
aws configure to set up your credentials.
aws configure
# AWS Access Key ID [None]: YOUR_ACCESS_KEY
# AWS Secret Access Key [None]: YOUR_SECRET_KEY
# Default region name [None]: us-west-2 (or your preferred region)
# Default output format [None]: json
- kubectl: Install the Kubernetes command-line tool.
eksctl will use it to interact with your cluster.
- eksctl: Install
eksctl. This is the main tool we will be using.
Step 2: Create the eksctl YAML Config File
This is the core of the process. Create a file named cluster.yaml. Below is a well-commented, production-ready example that you can adapt.
This example will create:
- A new VPC with public and private subnets across two Availability Zones.
- An EKS control plane.
- A managed node group of
t3.medium instances that can scale from 2 to 4 nodes.
- SSH access enabled for the nodes (you need an existing EC2 key pair for this).
# cluster.yaml
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
# === CLUSTER METADATA ===
metadata:
name: my-eks-cluster # The name of your EKS cluster
region: us-west-2 # The AWS region to create the cluster in
version: "1.29" # The Kubernetes version for your cluster
# === NETWORKING (VPC) ===
# eksctl will create a new VPC with public and private subnets by default.
# This configuration makes it explicit.
vpc:
cidr: "192.168.0.0/16" # CIDR for the new VPC
nat:
gateway: HighlyAvailable # Use a highly available NAT Gateway for private subnets
# === IAM & OIDC ===
# Enable OIDC provider, which is a best practice for allowing Kubernetes service
# accounts to assume IAM roles (known as IRSA - IAM Roles for Service Accounts).
iam:
withOIDC: true
# === NODE GROUPS ===
# These are the EC2 instances where your pods will run.
# Using 'managedNodeGroups' is recommended as they are managed by AWS.
managedNodeGroups:
- name: general-workers # Name of the node group
instanceType: t3.medium # EC2 instance type for your worker nodes
minSize: 2 # Minimum number of nodes in the group
maxSize: 4 # Maximum number of nodes for auto-scaling
desiredCapacity: 2 # The initial number of nodes
volumeSize: 80 # EBS volume size in GiB for each node
privateNetworking: true # Place nodes in private subnets for security
labels: { role: "worker" } # Kubernetes labels to apply to the nodes
tags:
"eksctl.cluster.k8s.io/v1alpha1/cluster-name": "my-eks-cluster" # Recommended tag
"project": "my-app" # Custom tag
# Enable SSH access to nodes. You must have an EC2 key pair named 'my-keypair'
# in the specified region. If you don't need SSH, you can remove this section.
ssh:
allow: true
publicKeyName: my-keypair # <-- IMPORTANT: Replace with your EC2 key pair name
Save this content to a file named cluster.yaml.
Step 3: (Optional but Recommended) Dry Run
Before creating the resources (and incurring costs), you can perform a dry run. eksctl will generate the CloudFormation template it would use without actually applying it. This is great for validation.
eksctl create cluster -f cluster.yaml --dry-run > dry-run-output.yaml
You can inspect the dry-run-output.yaml file to see exactly what AWS resources will be created.
Step 4: Create the EKS Cluster
Now, run the command to create the cluster. eksctl will read your cluster.yaml file and provision all the resources using AWS CloudFormation in the background.
This process will take about 15-20 minutes.
eksctl create cluster -f cluster.yaml
You will see a lot of output as eksctl reports its progress:
- Creating CloudFormation stack...
- Setting up the VPC and subnets...
- Creating the EKS Control Plane...
- Creating the managed node group...
- Configuring your local
kubeconfig file...
Once it's done, you'll see a message like [✔] EKS cluster "my-eks-cluster" in "us-west-2" is ready.
eksctl automatically updates your ~/.kube/config file, so you can immediately interact with your new cluster using kubectl.
Step 5: Verify Your Cluster
Let's run a few commands to confirm everything is working.
-
Check cluster status with eksctl:
eksctl get cluster
# NAME REGION EKSCTL CREATED
# my-eks-cluster us-west-2 True
-
Check nodes with kubectl: You should see the nodes from your general-workers node group.
kubectl get nodes -o wide
# NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
# ip-192-168-30-149.us-west-2.compute.internal Ready <none> 10m v1.29.0-eks-036c24b 192.168.30.149 <none> Amazon Linux 2 5.10.205-195.807.amzn2.x86_64 containerd://1.6.24
# ip-192-168-80-45.us-west-2.compute.internal Ready <none> 10m v1.29.0-eks-036c24b 192.168.80.45 <none> Amazon Linux 2 5.10.205-195.807.amzn2.x86_64 containerd://1.6.24
You now have a fully functional EKS cluster!
Step 6: Cleanup (Very Important!)
When you are finished with your cluster, you should delete it to avoid ongoing AWS charges. eksctl makes this just as easy as creation.
Use the same cluster.yaml file to delete all associated resources.
eksctl delete cluster -f cluster.yaml
This will trigger the deletion of the CloudFormation stacks, which will remove the Node Groups, the EKS Control Plane, the VPC, and all other resources that eksctl created. This process can also take 5-10 minutes.
Advanced YAML Configurations
Here are a few snippets for common, more advanced scenarios. You can add these to your cluster.yaml file.
Using an Existing VPC
If you already have a VPC, you can tell eksctl to use it instead of creating a new one.
vpc:
id: "vpc-0123456789abcdef0" # Your VPC ID
subnets:
private:
us-west-2a: { id: "subnet-0123" }
us-west-2b: { id: "subnet-4567" }
public:
us-west-2a: { id: "subnet-89ab" }
us-west-2b: { id: "subnet-cdef" }
Adding a Fargate Profile (Serverless Pods)
Fargate lets you run pods without managing EC2 nodes.
# Add this section at the top level of your cluster.yaml
fargateProfiles:
- name: fp-default
selectors:
# All pods in the 'default' and 'kube-system' namespaces will run on Fargate
- namespace: default
- namespace: kube-system
Adding an ARM-based (Graviton) Node Group
Graviton instances can offer better price-performance for many workloads.
managedNodeGroups:
- name: arm-workers
instanceType: t4g.medium # 'g' indicates a Graviton (ARM) instance
minSize: 1
maxSize: 3
# You must specify an ARM-compatible AMI family
amiFamily: AmazonLinux2_aarch64