← Back to Course Index

Week 11 Lab: Automating Cloud Deployment

Module: Operating Systems 3 (Virtualisation & Cloud Technologies)

Estimated Time: 60 Minutes
Lab Type: Scripting / CLI


Lab Overview

By the end of this lab, you will have moved beyond "ClickOps". You will write code that deploys a web server automatically.

Objectives: 1. Write "User Data": Create a Cloud-Init YAML file to install software. 2. Script the CLI: Write a Bash script to launch an instance. 3. Parse Output: Use jq to extract the Floating IP automatically.

Prerequisites:


Part 1: The Cloud-Init Configuration (User Data)

Cloud-Init runs on the first boot. We tell it what to do using YAML.

  1. Create the Config File: On your Ubuntu VM (running MicroStack), create a file named web-setup.yaml. bash nano web-setup.yaml

  2. Add the Configuration:

#cloud-config
package_update: true
packages:
  - apache2

runcmd:
  - echo "<h1>Hello from Automation!</h1>" > /var/www/html/index.html
  - systemctl enable apache2
  - systemctl start apache2
  1. Field Report:

    • Filename created: [ _____________________________ ]
    • What software are we installing?: [ _____________________________ ]

Part 2: The Deployment Script (Bash + OpenStack CLI)

We will write a script that does everything: Launch -> Wait -> Assign IP.

  1. Create the Script: bash nano deploy.sh

  2. Add the Logic (Type this carefully):

#!/bin/bash

# Variables
VM_NAME="auto-web-01" # Hint: Use "auto-web-$(date +%s)" to avoid name collisions!
IMAGE="ubuntu"  # Or 'cirros' if RAM is tight
FLAVOR="m1.tiny"
NET="private-net"
KEY="lab-key"
SEC_GROUP="default" 

echo "--- Starting Deployment of $VM_NAME ---"

# 1. Launch Instance
echo "1. Launching Server..."
openstack server create \
  --flavor $FLAVOR \
  --image $IMAGE \
  --network $NET \
  --key-name $KEY \
  --security-group $SEC_GROUP \
  --user-data web-setup.yaml \
  --wait \
  $VM_NAME

# 2. Get Floating IP
echo "2. Requesting Floating IP..."
fip=$(openstack floating ip create external -f json | jq -r .floating_ip_address)
echo "   Got IP: $fip"

# 3. Assign IP
echo "3. Assigning IP to Server..."
openstack server add floating ip $VM_NAME $fip

echo "--- Deployment Complete! ---"
echo "Access your site at: http://$fip"
  1. Make executable:
chmod +x deploy.sh

Part 3: Execution and Testing

  1. Run the script: bash ./deploy.sh
  2. Wait:

    • The script will finish in ~30 seconds.
    • BUT: The VM needs time to boot and run the Cloud-Init script (installing Apache takes ~2 minutes).
    • Verify:

    • Pro Tip: Run ssh ubuntu@<IP> sudo cloud-init status --wait to see when it finishes!

    • Open http://<THE_FLOATING_IP> in your browser.
    • Success Condition: You see "Hello from Automation!".

    Important Observation: This script is not idempotent. If you run it a second time, it will fail because the server auto-web-01 already exists. This is why we need tools like Heat and Ansible.

  3. Field Report:

    • Floating IP Assigned: [ _____________________________ ]
    • Did the Apache Page load? [ Yes / No ]

Lab Checkpoint

Reflection:

Looking Ahead: In the next lab, we will replace this Bash script with a Heat Template, making our deployment declarative and repeatable.


Instructor Signature: ___ Date: ___

← Back to Course Index