Terraform Demos
Copy and modify these pre-built Terraform configurations for your infrastructure.
AWSEKS Cluster with Deployment
●AWSUploadedCreate an EKS cluster with a sample deployment and service.
By System · Containers
▾
EKS Cluster with Deployment
●AWSUploadedCreate an EKS cluster with a sample deployment and service.
By System · Containers
main.tf · variables.tf · outputs.tf · deployment.yaml
Downloadresource "aws_iam_role" "eks_cluster" {
name = "${var.cluster_name}-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = { Service = "eks.amazonaws.com" }
Action = "sts:AssumeRole"
}]
})
}
resource "aws_iam_role_policy_attachment" "eks_cluster_policy" {
role = aws_iam_role.eks_cluster.name
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
}
resource "aws_eks_cluster" "main" {
name = var.cluster_name
role_arn = aws_iam_role.eks_cluster.arn
version = var.kubernetes_version
vpc_config {
subnet_ids = var.subnet_ids
}
depends_on = [aws_iam_role_policy_attachment.eks_cluster_policy]
}
resource "aws_iam_role" "eks_nodes" {
name = "${var.cluster_name}-node-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = { Service = "ec2.amazonaws.com" }
Action = "sts:AssumeRole"
}]
})
}
resource "aws_iam_role_policy_attachment" "eks_worker_policy" {
role = aws_iam_role.eks_nodes.name
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
}
resource "aws_iam_role_policy_attachment" "eks_cni_policy" {
role = aws_iam_role.eks_nodes.name
policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
}
resource "aws_eks_node_group" "main" {
cluster_name = aws_eks_cluster.main.name
node_group_name = "${var.cluster_name}-nodes"
node_role_arn = aws_iam_role.eks_nodes.arn
subnet_ids = var.subnet_ids
scaling_config {
desired_size = var.desired_nodes
min_size = 1
max_size = var.max_nodes
}
}
variable "cluster_name" {
type = string
}
variable "kubernetes_version" {
type = string
default = "1.30"
}
variable "subnet_ids" {
type = list(string)
}
variable "desired_nodes" {
type = number
default = 2
}
variable "max_nodes" {
type = number
default = 4
}Troubleshooting & Solutions
Common Issues
- Nodes never join the cluster
- kubectl can't authenticate
- Cluster creation takes 15+ minutes
Solutions
- Confirm subnets are tagged correctly for EKS auto-discovery and span at least 2 AZs
- Run `aws eks update-kubeconfig --name <cluster>` to refresh local kubectl auth
- 10-15 minute creation time is normal for EKS — this isn't a hang
Troubleshooting Steps
- Check node group status and any scaling activity errors
- Verify the node IAM role has all three required policies attached
- Confirm security groups allow node-to-control-plane communication
- Check `kubectl get nodes` after update-kubeconfig to confirm nodes actually joined
Version compatibility: Terraform >= 1.0, AWS Provider >= 5.0, Kubernetes 1.28+