๐Ÿ“˜ Visual Posts About Contact
Explore ๐Ÿ“‹ Cheat Sheets โœ๏ธ Practice ๐ŸŽฎ Games ๐ŸŽฌ Simulations ๐Ÿƒ Flashcards ๐ŸŽฏ Interactive Layers ๐Ÿ“– Storytelling
๐Ÿ’œ Support my site
Module 17 โ€” Final Capstone Cloud & DevOps 2026 Updated

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 โ†’
CLOUD REGION / VPC ๐Ÿ›ก Security Group (Allow :22, :443) VPS-01 (Web) cloud-init โœ“ | nginx โœ“ VPS-02 (API) cloud-init โœ“ | ufw โœ“ VPS-03 (DB) cloud-init โœ“ | postgres โœ“ CONTROL NODE playbook.yml inventory.ini SSH

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.

Universal Analogy โ€” Owning a House vs Renting a Furnished Apartment:
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.

Owning a House (On-Prem) vs Renting an Apartment (Cloud VPS) ON-PREMISES (You Manage 100%) Apps, Data & Backups OS, Patches & Firewall Hypervisor (KVM / VMware) Physical CPU, RAM, RAID Disks Data Center Power, Cooling & Fiber CapEx: High upfront cost, slow to scale CLOUD VPS / EC2 (Shared Responsibility) YOU: Apps, Data & Backups YOU: OS, Patches & Firewall PROVIDER: Hypervisor & Virtualization PROVIDER: Physical CPU, RAM & NVMe PROVIDER: Power, Cooling & Physical Security OpEx: Pay-as-you-go, ready in 60 seconds
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.

Universal Analogy โ€” The Perimeter Gate vs Your Apartment Deadbolt:
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, ufw still 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.
Defense in Depth: Perimeter Gate + Apartment Deadbolt INTERNET HTTPS :443 SSH :22 (Admin) MySQL :3306 LAYER 1: CLOUD SG (Perimeter Gate) โœ“ Allow 443 (All) โœ“ Allow 22 (My IP) โœ• DROP 3306 Blocks noise before VM LAYER 2: UFW (OS Deadbolt) โœ“ allow 443/tcp โœ“ limit 22/tcp โœ• deny incoming Stops VPC lateral scans YOUR VPS nginx :443 sshd :22 db: localhost

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.

Universal Analogy โ€” The Move-In Checklist Bot:
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.

Day-0 Server Bootstrapping with cloud-init 1. USER DATA #cloud-config โ€ข Admin user โ€ข SSH public key โ€ข Packages list โ€ข First-boot cmds Pasted at launch 2. FIRST BOOT systemd starts cloud-init.service Fetches metadata at 169.254.169.254 Runs once on Day 0 3. AUTO-SETUP โœ“ Create user "deploy" โœ“ Inject SSH key โœ“ apt update & upgrade โœ“ Install nginx + ufw Zero manual clicks 4. READY ๐Ÿš€ SSH in as deploy@ip ~45 seconds Debug Tip: Check /var/log/cloud-init-output.log if a first-boot task fails.

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.

Universal Analogy โ€” The Orchestra Conductor:
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

01

Inventory (inventory.ini)

A plain text list of your server IPs or hostnames grouped by role, such as [webservers] and [databases].

02

Playbook (site.yml)

A YAML file describing the desired end state (e.g., "Nginx is installed and running"), not fragile bash loops.

03

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.

Ansible: Agentless SSH Push + Idempotency CONTROL NODE (Your Laptop / CI Runner) inventory.ini (100 IPs) playbook.yml (YAML) ansible-playbook -i ... SSH (Port 22) No agent needed! web-01 (198.51.100.11) nginx: present | state: started web-02 (198.51.100.12) nginx: present | state: started ... web-100 All servers match desired state IDEMPOTENCY IN ACTION 1st Run: ok=3 changed=2 (installs & starts nginx) | 2nd Run: ok=3 changed=0 (already matches!)

Checkpoint: Ansible Core Ideas

  • Ansible is agentless โ€” it only needs SSH access and Python on target Linux servers.
  • inventory.ini lists 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 (and 443 for HTTPS) from 0.0.0.0/0.
  • 2. Host Firewall (ufw): Run sudo ufw status on the VPS and ensure 80/tcp and 443/tcp are 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.

๐ŸŽฏ Knowledge Check

Test your understanding of Module 17: Cloud VPS, Security Groups, cloud-init & Ansible. Score: 0 / 4

1. In the Shared Responsibility Model for a Cloud VPS (like AWS EC2 or Hetzner Cloud), which task is STILL your responsibility?

Replacing failed physical power supplies in the data center rackโŒ
Applying OS security patches, configuring firewalls, and backing up your dataโœ…
Upgrading the cloud providerโ€™s physical hypervisor hardwareโŒ

2. Why do experienced sysadmins use BOTH a Cloud Security Group AND a Host Firewall (ufw)?

Defense in depth โ€” Security Groups block internet noise early, while UFW protects inside the VPCโœ…
Because Cloud Security Groups cannot block TCP ports like 3306โŒ
Because SSH keys only work when UFW is installedโŒ

3. What is the main purpose of cloud-init when launching a new cloud server?

To act as a public DNS load balancer for web trafficโŒ
To automate Day-0 setup (users, SSH keys, packages) on the serverโ€™s very first bootโœ…
To continuously monitor CPU usage and send email alerts every minuteโŒ

4. What does it mean when we say an Ansible Playbook is idempotent?

It requires installing a heavy background agent on every serverโŒ
It can only be executed once before the server must be deletedโŒ
Running it 1 time or 100 times produces the exact same end state without breaking anythingโœ