This project demonstrates Infrastructure as Code (IaC) to deploy Kubernetes worker nodes on AWS EC2 instances using Terraform, Ansible, Docker, and Kubernetes. It also deploys a sample FastAPI application inside Kubernetes automatically.
- AWS Account
- IAM User with EC2 permissions
- Terraform (>= 1.7.0)
- Ansible (9.3.0)
- Python3 environment
- SSH Key Pair for EC2 access
git clone https://github.com/Vrana710/k8s-ec2-autoscaling-demo.git
cd k8s-ec2-autoscaling
Install requirements:
pip install -r requirements.txt
cd infrastructure/terraform
terraform init
terraform apply -auto-approve
Get the Public IP from output.
Edit config/inventory.ini
:
[all]
<public_ip> ansible_user=ubuntu ansible_ssh_private_key_file=../your_key_name.pem
cd infrastructure/ansible
ansible-playbook playbook.yml -i ../../config/inventory.ini
This installs Docker, Kubernetes components, and CloudWatch agent.
SSH into EC2:
ssh -i ../your_key_name.pem ubuntu@<public_ip>
Initialize Kubernetes:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Install Flannel CNI:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
cd ~/app
sudo docker build -t fastapi-app:latest .
kubectl apply -f app/deployment.yaml
Check deployment:
kubectl get pods
kubectl get svc
Access FastAPI app via public IP.
Monitor CPU manually:
bash scripts/monitor_and_scale.sh
If CPU exceeds threshold, new nodes are provisioned.
- Fully provisioned EC2 instance
- Configured Kubernetes + CloudWatch
- FastAPI app deployed inside Kubernetes
- Ready for manual or automatic scaling
- GitHub Actions pipeline ready for full automation
k8s-ec2-autoscaling/
├── app/
│ ├── main.py
│ ├── Dockerfile
│ └── deployment.yaml
├── config/
│ ├── inventory.ini
│ └── terraform.tfvars (with updated values)
├── infrastructure/
│ ├── terraform/
│ └── ansible/
├── scripts/
│ └── monitor_and_scale.sh
├── .github/workflows/deploy.yml
├── requirements.txt
└── docs/
└── README.md
Edit config/terraform.tfvars
:
region = "your name of region"
ami_id = "your ami id"
key_name = "your key name"
instance_type = "your instance type"
Important: Key name is the AWS EC2 Key Pair name (without .pem
).
- SSH via key-pair only
- Minimum necessary ports open (22, 80)
- Terraform-managed security groups
- CloudWatch Agent installed for monitoring