Cloud VPS & Infrastructure Automation
Stop configuring servers one click at a time. Learn how modern sysadmins rent virtual servers in the cloud, lock them down with two layers of firewalls, bootstrap them automatically on first boot with cloud-init, and manage 100 servers at once with Ansible.
Start Module 17 โ01 On-Premises Servers vs Cloud VPS / EC2
Define: An on-premises server is physical hardware you buy, rack, power, and maintain inside your own building or colocation cage. A Cloud VPS (Virtual Private Server โ such as AWS EC2, Hetzner Cloud, DigitalOcean Droplets, or Linode) is a virtual machine running on someone elseโs giant data center hardware that you rent by the hour or month.
Buying an on-premises server is like buying a house: you pay a huge amount upfront, and if the roof leaks (a failed RAID drive) or the power goes out (a dead PSU), you fix it. Renting a Cloud VPS is like renting a furnished apartment: the landlord (AWS, Hetzner, DigitalOcean) maintains the building, electricity, cooling, and physical servers. You just move in with your suitcase (your OS, apps, and data) and pay monthly rent.
Understand & Know: Who Manages What?
Cloud computing runs on the Shared Responsibility Model. The provider guarantees that the physical server stays powered on, cooled, and connected to fiber internet. You are responsible for everything inside your virtual server: OS security patches, SSH keys, firewall rules, and backups of your application data.
| Feature | On-Premises Server | Cloud VPS / EC2 |
|---|---|---|
| Cost Model | CapEx (buy hardware upfront) | OpEx (pay per hour or month) |
| Provisioning Time | Days to weeks (shipping + racking) | 30โ60 seconds via web console or API |
| Hardware Failure | You swap failed disks/PSUs | Provider replaces host; you reboot/recreate |
| Best For | Strict local compliance, predictable 24/7 load | Startups, web apps, elastic scaling, global regions |
When & Execute: Ephemeral vs Persistent Storage
In the cloud, treat servers like cattle, not pets. If a VPS breaks, you can delete it and launch a fresh one in a minute. Just make sure your database or uploaded files live on persistent block storage (like AWS EBS or Hetzner Volumes) or object storage (S3), not on temporary instance scratch disks that vanish when the VM stops.
Checkpoint: On-Premises vs Cloud VPS
- Cloud providers manage physical hardware, power, cooling, and the hypervisor.
- You still manage OS updates, SSH hardening, firewalls, and application backups.
- Cloud VPS turns upfront CapEx into flexible monthly or hourly OpEx.
02 Cloud Security Groups vs Host Firewalls
Define: A Cloud Security Group (or Cloud Firewall) is a virtual
firewall managed at the cloud providerโs network edge before packets ever reach your VM.
A Host Firewall (such as ufw, nftables, or
firewalld) runs inside your serverโs Linux kernel.
Your Cloud Security Group is the security guard at the apartment buildingโs front gate: strangers without a pass are turned away outside, so they never even walk down your hallway. Your Host Firewall (
ufw) is the deadbolt on your own apartment door:
even if someone gets past the front gate (for example, a compromised neighbor server on the same
private VPC network), your deadbolt still stops them from walking into your room.
Understand & Know: Why Use Both Layers?
Beginners often ask: "If I already configured a Cloud Security Group, why bother with
ufw inside Ubuntu?" The answer is Defense in Depth:
- Cloud Security Group stops internet noise early: Blocked port scans never consume your VPS CPU or bandwidth.
- Host Firewall protects against misclicks & lateral movement: If a teammate
accidentally opens a Security Group rule to
0.0.0.0/0, or another VM in your private subnet gets hacked,ufwstill blocks unauthorized ports. - Stateful by default: Cloud Security Groups are stateful โ if you allow
inbound HTTPS (
443), the return response traffic back to the user is automatically allowed.
When & Execute: Minimum Required Rules
Never open SSH (22) or database ports (3306, 5432) to
0.0.0.0/0 unless you have a specific reason. Restrict SSH to your static office/VPN IP
(e.g., 203.0.113.10/32) in the Cloud Security Group, and enable ufw
inside the server:
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit 22/tcp
sudo ufw allow 80,443/tcp
sudo ufw enable
03 Bootstrapping New Servers with cloud-init
Define: cloud-init is the industry-standard tool pre-installed on
almost every cloud Linux image (Ubuntu, Debian, RHEL, AlmaLinux, Amazon Linux). On a serverโs very
first boot (Day 0), it reads a small YAML file called User Data
and automatically configures the server before you ever log in.
Imagine hiring a trusted assistant who arrives at your brand-new empty apartment 60 seconds before you do. By the time you walk up the stairs, the assistant has already changed the locks (SSH keys), set up your personal mailbox (non-root user), turned on the Wi-Fi (firewall), and stocked the fridge (installed Nginx). That assistant is
cloud-init.
Understand & Know: How cloud-init Works
When you click Launch Instance in AWS, Hetzner, or DigitalOcean, you can paste a
#cloud-config YAML snippet into the User Data box. During first
boot, cloud-init queries the cloud providerโs local metadata IP
(169.254.169.254), downloads your YAML, and executes it once.
When & Execute: A Real cloud-init Template
Notice the very first line must be #cloud-config (with no leading
spaces) so cloud-init knows it is a YAML configuration file rather than a raw bash
script:
#cloud-config
package_update: true
package_upgrade: true
packages:
- ufw
- fail2ban
- curl
users:
- name: deploy
groups: sudo
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
ssh_authorized_keys:
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKeyHere admin@laptop
runcmd:
- ufw allow 22/tcp
- ufw allow 80,443/tcp
- ufw --force enable
Memory Trick โ "cloud-init is Day 0, Ansible is Day 1+"
Use cloud-init once when the server is born (create user, inject SSH key, enable firewall). Use Ansible every day after that to deploy apps, update configs, and keep 10 or 100 servers identical.
04 Managing 100 Servers at Once with Ansible
Define: Ansible is an open-source, agentless configuration management tool. You write human-readable YAML Playbooks on one control machine (your laptop or a bastion server), and Ansible connects to all your target servers over standard SSH to apply the exact same configuration everywhere.
Logging into 50 servers one by one via SSH is like running from violinist to violinist whispering every musical note. Ansible is the orchestra conductor standing at the podium: it reads one sheet of music (your YAML Playbook) and directs all 50 musicians (Managed Nodes) at the same time. Even better, you donโt need to install any special software on the serversโjust SSH and Python.
Understand & Know: The 3 Core Concepts
Inventory (inventory.ini)
A plain text list of your server IPs or hostnames grouped by role, such as
[webservers] and [databases].
Playbook (site.yml)
A YAML file describing the desired end state (e.g., "Nginx is installed and running"), not fragile bash loops.
Idempotency
Running the same playbook 1 time or 100 times produces the exact same safe result. If Nginx is
already installed, Ansible reports ok and changes nothing.
Checkpoint: Ansible Core Ideas
- Ansible is agentless โ it only needs SSH access and Python on target Linux servers.
inventory.inilists which servers to manage; YAML playbooks declare the desired state.- Idempotency means re-running a playbook is safe and makes zero changes if nothing drifted.
05 Practical Cloud VPS Launch & First Ansible Playbook
Letโs walk through the exact end-to-end workflow used in production: launch a fresh Ubuntu 24.04 /
26.04 LTS Cloud VPS with cloud-init, lock down its firewalls, and deploy Nginx across
your fleet using your first Ansible playbook.
Step 1: Create Your Inventory File (inventory.ini)
[webservers]
198.51.100.11 ansible_user=deploy
198.51.100.12 ansible_user=deploy
[webservers:vars]
ansible_ssh_private_key_file=~/.ssh/id_ed25519
Step 2: Write Your First Playbook (site.yml)
---
- name: Configure Production Web Servers
hosts: webservers
become: true
tasks:
- name: Update apt cache and install Nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
- name: Ensure Nginx is started and enabled at boot
ansible.builtin.systemd_service:
name: nginx
state: started
enabled: true
Step 3: Dry-Run (--check) and Execute
# 1. Test SSH connectivity to all servers
ansible webservers -i inventory.ini -m ping
# 2. Dry-run without making changes
ansible-playbook -i inventory.ini site.yml --check
# 3. Apply configuration to all servers
ansible-playbook -i inventory.ini site.yml
Practice Problem 1: Diagnosing a Blocked Web Server
You launched a new Cloud VPS, installed Nginx with Ansible (state: started), and
verified inside the server with curl http://localhost that Nginx returns your
homepage. However, when you open http://<server-public-ip> in your browser,
it times out. What are the two firewall places you must check?
Show Solution
Check both layers of the firewall:
- 1. Cloud Security Group (Perimeter Gate): Verify an inbound rule
allows TCP port
80(and443for HTTPS) from0.0.0.0/0. - 2. Host Firewall (
ufw): Runsudo ufw statuson the VPS and ensure80/tcpand443/tcpare allowed.
Practice Problem 2: Day-0 vs Day-1+
You are launching 20 identical web servers. Should you put your entire 300-line application
deployment logic inside cloud-init User Data, or keep cloud-init tiny
and use Ansible? Why?
Show Solution
Keep cloud-init tiny (Day-0 essentials only: admin user, SSH key, basic
firewall) and put application deployment in Ansible. Why? Because
cloud-init only runs once on first boot, whereas Ansible is idempotent and
can be re-run any time to update configs, patch apps, or fix drift across all 20
servers.