If you’ve ever had to set up more than one server, you know how tedious and error-prone running commands on several servers in a row can be. In this lab, we automate the basic set up of a new server in Google Compute Engine using bootstrap scripts. To start, we deploy a simple bootstrap script, then use the bootstrap script stored in Cloud Storage that references the Compute Engine metadata server.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Deploy Apache with a Startup Script
- Create a new VM instance with the following settings:
- Machine type: e2-small
- Firewall: Set to Allow HTTP traffic
- Add a startup script to the instance configuration using the provided code:
#! /bin/bash apt update apt -y install apache2 cat <<EOF > /var/www/html/index.html <html><body><h1>Hello Cloud Gurus</h1> <p>This page was created from a startup script.</p> </body></html> EOF
- Use the generated external IP to view the web page created by your startup script.
- Create a new VM instance with the following settings:
- Deploy a Startup Script from a Cloud Storage Bucket
- In the GCP console, create a unique bucket name to store a startup script. For example, use the last 8 digits of the project name to create a name like 836bd4db-scripts.
- Create the
startup.sh
file on your local computer using the provided code:#! /bin/bash apt update apt -y install apache2 ZONE=`curl -fs http://metadata/computeMetadata/v1/instance/zone -H "Metadata-Flavor: Google" | cut -d'/' -f4` cat <<EOF > /var/www/html/index.html <html><body><h1>Hello Cloud Gurus</h1> <p>This server is serving from ${ZONE}.</p> </body></html> EOF
- Upload the file to your storage bucket and copu the URI of the object location.
- Create a new VM with the following settings:
- Machine type: e2-small
- Firewall: Set to Allow HTTP traffic
- In the Metadata section, set the following as:
- Key: startup-script-url
- Value: Startup script URI
- Use the generated external IP to view the web page created by your startup script.