Deploy a single-node k3s Kubernetes cluster on AWS EC2 using OpenTofu and Ansible.
# Initialize (creates AWS key pair, terraform.tfvars, and downloads providers)
make init
# Deploy the infrastructure and install k3s
make apply
# SSH into the instance
make ssh
# Fetch kubeconfig to use kubectl locally
make kubeconfig
export KUBECONFIG=./kubeconfig.yaml
kubectl get nodes| Target | Description |
|---|---|
make init |
Create key pair, copy tfvars, initialize OpenTofu |
make plan |
Preview infrastructure changes |
make apply |
Deploy infrastructure and install k3s |
make destroy |
Tear down all infrastructure |
make ssh |
SSH into the EC2 instance |
make kubeconfig |
Fetch kubeconfig and configure for remote access |
make ansible |
Re-run Ansible playbook manually |
make keypair |
Create AWS key pair only |
make clean |
Remove generated files (keeps key pair) |
make clean-all |
Remove everything including AWS key pair |
Edit terraform.tfvars after running make init:
aws_region = "us-east-1"
instance_type = "t3.medium"
key_name = "k3s-cluster-key"
private_key_path = "./k3s-cluster-key.pem"
allowed_ssh_cidr = "0.0.0.0/0" # Restrict to your IP for security
project_name = "k3s-cluster"make init KEY_NAME=my-custom-key- VPC with public subnet and internet gateway
- Security group (ports: 22, 80, 443, 6443)
- EC2 instance (Ubuntu 22.04, 30GB gp3 volume)
- k3s installed via Ansible
# Destroy infrastructure but keep key pair for reuse
make destroy
make clean
# Full cleanup including AWS key pair
make destroy
make clean-all