In this lab, you'll configure and deploy VM instances with web apps on them, by Cloud Console (to see all options) and gcloud (to script commands). Then, you'll create a disk snapshot and an instance image.

These tasks are especially useful for lift-and-shift operations that may require specific hardware configurations.

What you will build

What you'll learn

What you'll need

Go to console.cloud.google.com.

Sign in as the owner of a free-trial GCP account or as a user with project owner access to a billing-enabled project.

Select a project with billing enabled.

Go to Compute Engine > VM Instances and click Create Instance.

Name your instance apache-1.

Select a zone near you.

Set Machine type to micro.

Keep Docker containers disabled.

Under Boot disk, click Change:

Leave Access as the default.

Under Firewall access, check allow HTTP traffic for your web server. You could set a firewall rule, but this is a quick way to open port 80 for just this instance.

Under Management, disk, networking, SSH keys > Management copy the following lines to the startup script to install and configure an Apache web server with a custom welcome page.

#! /bin/bash
VALUE_OF_GREETING=$(curl http://metadata.google.internal/computeMetadata/v1/instance/attributes/greeting -H "Metadata-Flavor: Google")
apt-get update
apt-get install -y apache2
cat <<EOF > /var/www/html/index.html
<html><body><h1>Hello from $VALUE_OF_GREETING</h1>
<p>This page was created from a simple startup script!</p>
</body></html>
EOF

Under Metadata, enter the key/value pair as shown so it can be fetched by the startup script and placed in the home page (key is greeting, value is Apache One.

Leave defaults for Availability.

Click the Disks tab

Uncheck Deletion rule to keep your boot disk when done (you'll use this later to create a disk image).

Click Add Item under Additional disks to see if there are any disks you can add.

Click the Name drop-down.

Currently, there are no unattached disks created by you in this zone.

Click Create disk in the drop-down to see what you could do.

Select a type and standard image and notice Estimated performances populate:

Change the size and notice estimates change.

Click Cancel to not add an additional disk.

Click the icon to the right of the unnamed, empty disk so you don't get an error later.

Click the SSH keys tab.

To use project keys (instead of instance-specific keys), leave the Block project-wide keys... box unchecked.

You're done defining the instance, so click Create.

When creation ends, note the instance's external IP address.

Open Cloud Shell and run a curl command to fetch the web server's welcome page using the instance's external IP address. For example:

curl http://<instance-external-IP>

You'll get an HTML page (shown below) with the greeting 'Hello from Apache One':

Now you've explored instance creation options from the console and deployed an app.

Next, you'll do the same thing from command-line. This is where tasks become scriptable and repeatable.

Return to Cloud Shell where Cloud SDK is automatically installed for you.

Cloud SDK has defaults set that can be used by commands. To see them, enter:

gcloud config list

To set a default zone, run the following command to see the list of available zone names.

gcloud compute zones list

Then run a command like the following:

gcloud config set compute/zone us-west1-b

Rerun gcloud config list to see your default zone.

Run the following command to get the list of available OS images:

gcloud compute images list

Look for the current name and project of the latest Debian image (the name will vary over time and you'll need the project because the image isn't coming from your project):

Run the following command to get the list of disk types:

gcloud compute disk-types list

Run the following command to store the startup script as a local file that installs the Apache web server so you can pass it as a parameter to the gcloud command.

cat > startup.txt <<EOF
#!/bin/bash
apt-get update
apt-get install -y apache2
EOF

Run the following command to create the instance. The startup script is provided from the local file you just created.

gcloud compute instances create apache-2 \
    --machine-type f1-micro \
    --image <latest-debian-image-name> \
    --image-project debian-cloud \
    --boot-disk-type pd-ssd \
    --boot-disk-size 30GB \
    --metadata-from-file startup-script=startup.txt

Review and adjust the instance:

gcloud compute firewall-rules create port80 --allow tcp:80

Test the instance

To test the web server, you could look up the instance's public IP and enter it manually to fetch the web server's home page, or, more usefully, you can fetch it programmatically with gcloud using the following command (do it now).

export PUB_IP=$(gcloud compute instances describe apache-2 --format 'value(networkInterfaces[0].accessConfigs[0].natIP)')

Now you can fetch the home page using the system variable PUB_IP where you stored the instance's external address.

curl http://${PUB_IP}

By using gcloud in this way, you can further script higher level testing and configuration tasks.

To see, this, click one of the instances with HTTP allowed and go to the Network tags field.

Then go to Networking > Firewall rules and look for the rule with the tag.

By knowing this, you can allow HTTP access when creating instances using gcloud by setting the http-server tag and making sure a firewall rule is set for the tag.

Snapshots are used for regularly backing up data from disks. Images are used primarily to create instances or instance templates from custom boot images.

Snapshots are differential, and can be created from disks on running instances, so you can regularly create them faster and cheaper than full images.

To create a snapshot:

To create a custom image, you must first delete your instance to detach the boot disk from the instance and preserve it.

To do this, you set the auto-delete state of the apache-1 boot disk to false so it's not deleted when you delete the instance.

To create the image:

Note the image added, its size (it's a full image), and its project (Created by):

To create an instance from your new image:

In Cloud Shell, run the curl command to fetch the web server's welcome page using apache-3's external address.

Go to the VM Instances page and delete your instances.

Go to the Snapshots page, select your snapshot, and click Delete.

Go to the Images page, select your custom image, and click Delete.

Congratulations! Now you know how to start and delete instances and create snapshots and images.