Solution to GSP301 – Deploy a Compute Instance with a Remote Startup Script Answers

Solution to GSP301 – Deploy a Compute Instance with a Remote Startup Script Answers

Challenge scenario

You have been given the responsibility of managing the configuration of your organization’s Google Cloud virtual machines. You have decided to make some changes to the framework used for managing the deployment and configuration machines – you want to make it easier to modify the startup scripts used to initialize a number of the compute instances. Instead of storing startup scripts directly in the instances’ metadata, you have decided to store the scripts in a Cloud Storage bucket and then configure the virtual machines to point to the relevant script file in the bucket.

Topics covered

  • Create a storage bucket for startup scripts.
  • Create a virtual machine that runs a startup script from Cloud Storage.
  • Configure HTTP access for the virtual machine.
  • Deploy an application on an instance.

Your challenge

A basic bash script that installs the Apache web server software called install-web.sh has been provided for you as a sample startup script. You can download this from the Student Resources links on the left side of the page.

Solution

Copy the code below 👇 and save it in a file labeled install-web.sh Then run in on the terminal.

# Task 1: Confirm that a Google Cloud Storage bucket exists that contains a file
gsutil mb gs://$DEVSHELL_PROJECT_ID
gsutil cp gs://sureskills-ql/challenge-labs/ch01-startup-script/install-web.sh gs://$DEVSHELL_PROJECT_ID
# Task 2: Confirm that a compute instance has been created that has a remote startup script called install-web.sh configured
gcloud compute instances create example-instance --zone=us-central1-a --tags=http-server --metadata startup-script-url=gs://$DEVSHELL_PROJECT_ID/install-web.sh
# Task 3: Confirm that a HTTP access firewall rule exists with tag that applies to that virtual machine
gcloud compute firewall-rules create allow-http --target-tags http-server --source-ranges 0.0.0.0/0 --allow tcp:80
# Task 4: Connect to the server ip-address using HTTP and get a non-error response

Congratulations!

You have successfully accomplished your challenge.

Leave a Reply

Your email address will not be published. Required fields are marked *