Skip to content

Instantly share code, notes, and snippets.

@ilumin
Last active June 8, 2023 02:57
Show Gist options
  • Save ilumin/406fd0a5af3e40c09110385d30c056dc to your computer and use it in GitHub Desktop.
Save ilumin/406fd0a5af3e40c09110385d30c056dc to your computer and use it in GitHub Desktop.
Cloudskillsboost: Deploy a Compute Instance with a Remote Startup Script

[!todo] Configure a Linux Compute Engine instance that installs the Apache web server software using a remote startup script. In order to confirm that a compute instance Apache has successfully installed, the Compute Engine instance must be accessible via HTTP from the internet.

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==.

Note

Intermediate level not show tutorial content

projectID: qwiklabs-gcp-00-5891a493aa66

Normally we cloud add startup script directly

# on new instance 
gcloud compute instances create VM_NAME \
  --image-project=debian-cloud \
  --image-family=debian-10 \
  --metadata=startup-script='#! /bin/bash
  apt update
  apt -y install apache2
  cat <<EOF > /var/www/html/index.html
  <html><body><p>Linux startup script added directly.</p></body></html>
  EOF'

# on created instance 
gcloud compute instances add-metadata VM_NAME \
  --zone=ZONE \
  --metadata=startup-script='#! /bin/bash
  apt update
  apt -y install apache2
  cat <<EOF > /var/www/html/index.html
  <html><body><p>Linux startup script added directly.</p></body></html>
  EOF'

But this workshop we're going to add with startup script from Cloud Bucket

Configure Instance Metadata with startup script

Create Cloud Storage Bucket with projectId as name

BUCKET_NAME="qwiklabs-gcp-00-5891a493aa66"
gcloud storage buckets create gs://$BUCKET_NAME

create a startup script

vim install-web.sh
# with content 
# #!/bin/bash
# apt-get update 
# apt-get install -y apache2

Add file to storage[^1]

gcloud storage cp install-web.sh gs://$BUCKET_NAME

Pass the startup script into VM

VM_NAME="lab-monitor"
ZONE="us-central1-a"
gcloud compute instances add-metadata $VM_NAME \
  --zone=$ZONE \
  --metadata startup-script-url=gs://$BUCKET_NAME/install-web.sh

Verify by shelling into VM and run

sudo google_metadata_script_runner startup

Check firewalls

# attach tag into VM
gcloud compute instance add-tags $VM_NAME --zone=$ZONE --tags=lab-monitor

# create firewall for http
gcloud compute firewall-rules create allow-http --target-tags lab-monitor --source-ranges 0.0.0.0/0 --allow tcp:80

Check the URL and address

EXTERNAL_IP="34.172.130.186"

curl -X GET http://$EXTERNAL_IP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment