Bird
Raised Fist0
GCPcloud~5 mins

Startup scripts for automation in GCP - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Startup scripts run commands automatically when a virtual machine starts. They help set up software or configurations without manual work every time the machine boots.
When you want to install software automatically on a new VM instance.
When you need to configure system settings each time a VM starts.
When you want to start background services automatically after boot.
When you want to update packages or pull code from a repository on startup.
When you want to automate repetitive setup tasks for multiple VM instances.
Config File - startup-script.sh
startup-script.sh
#!/bin/bash
sudo apt-get update
sudo apt-get install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx

This script updates the package list, installs the Nginx web server, starts it immediately, and sets it to start automatically on every boot.

Commands
This command creates a new VM named 'example-vm' in the specified zone and attaches the startup script that installs and starts Nginx automatically when the VM boots.
Terminal
gcloud compute instances create example-vm --zone=us-central1-a --metadata startup-script='#!/bin/bash
sudo apt-get update
sudo apt-get install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx'
Expected OutputExpected
Created [https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instances/example-vm]. NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS example-vm us-central1-a e2-medium 10.128.0.2 34.68.123.45 RUNNING
--metadata - Attaches the startup script to the VM instance metadata
--zone - Specifies the zone where the VM will be created
This command connects to the VM and checks if the Nginx service is running, verifying that the startup script worked.
Terminal
gcloud compute ssh example-vm --zone=us-central1-a --command='systemctl status nginx'
Expected OutputExpected
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2024-06-14 12:00:00 UTC; 2min ago Docs: man:nginx(8) Main PID: 1234 (nginx) Tasks: 3 (limit: 1152) Memory: 5.0M CGroup: /system.slice/nginx.service ├─1234 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─1235 nginx: worker process
--command - Runs a command on the VM without opening an interactive shell
Key Concept

If you remember nothing else from this pattern, remember: startup scripts automate VM setup by running commands automatically at boot time.

Common Mistakes
Not making the startup script executable or missing the shebang line (#!/bin/bash).
The script won't run properly without the correct interpreter or execution permission.
Always start scripts with #!/bin/bash and ensure they have executable permissions if used from a file.
Forgetting to include the startup script in the VM metadata during creation.
Without attaching the script, the VM will not run any automation on startup.
Use the --metadata flag with the startup-script key when creating the VM.
Assuming the startup script runs as the logged-in user instead of root.
Some commands require root privileges; without sudo, they may fail.
Use sudo for commands that need elevated permissions inside the startup script.
Summary
Create a VM with a startup script using the --metadata flag to automate setup tasks.
Use startup scripts to install software and start services automatically on boot.
Verify the script worked by checking service status via SSH commands.

Practice

(1/5)
1. What is the main purpose of a startup script in a Google Cloud VM instance?
easy
A. To automate tasks when the VM boots
B. To manually start the VM
C. To create a new VM instance
D. To delete files from the VM

Solution

  1. Step 1: Understand startup script role

    Startup scripts run automatically when a VM starts to perform tasks without manual intervention.
  2. Step 2: Identify correct purpose

    Among the options, only automating tasks at boot matches the startup script function.
  3. Final Answer:

    To automate tasks when the VM boots -> Option A
  4. Quick Check:

    Startup script = automate tasks at boot [OK]
Hint: Startup scripts run automatically at VM boot time [OK]
Common Mistakes:
  • Confusing startup scripts with manual commands
  • Thinking startup scripts create or delete VMs
  • Assuming startup scripts run after user login
2. Which command correctly adds a startup script to a new VM instance using gcloud CLI?
easy
A. gcloud compute instances create my-vm --metadata startup='echo Hello'
B. gcloud compute instances create my-vm --script-startup='echo Hello'
C. gcloud compute instances create my-vm --startup='echo Hello'
D. gcloud compute instances create my-vm --metadata startup-script='echo Hello'

Solution

  1. Step 1: Recall correct flag for startup script

    The correct metadata key to add a startup script is 'startup-script'.
  2. Step 2: Match command syntax

    gcloud compute instances create my-vm --metadata startup-script='echo Hello' uses '--metadata startup-script' correctly; others use invalid flags.
  3. Final Answer:

    gcloud compute instances create my-vm --metadata startup-script='echo Hello' -> Option D
  4. Quick Check:

    Use --metadata startup-script to add scripts [OK]
Hint: Use --metadata startup-script flag with gcloud create [OK]
Common Mistakes:
  • Using incorrect flag names like --script-startup
  • Confusing metadata keys with other flags
  • Missing quotes around the script content
3. Given this startup script added to a VM:
#!/bin/bash
echo "Hello World" > /var/log/startup.log

What will happen when the VM boots?
medium
A. The VM will fail to boot due to script error
B. The file /var/log/startup.log will contain 'Hello World'
C. Nothing happens because echo is not allowed
D. The file /var/log/startup.log will be deleted

Solution

  1. Step 1: Analyze the script commands

    The script writes the text 'Hello World' into the file /var/log/startup.log using echo and redirection.
  2. Step 2: Understand script effect on boot

    Since startup scripts run as root, the file will be created or overwritten with the text.
  3. Final Answer:

    The file /var/log/startup.log will contain 'Hello World' -> Option B
  4. Quick Check:

    Startup script writes text to log file [OK]
Hint: Startup scripts run as root and can write files [OK]
Common Mistakes:
  • Assuming echo command is blocked
  • Thinking the file will be deleted instead of created
  • Believing the VM will fail due to simple echo
4. You wrote this startup script:
#!/bin/bash
apt-get update
apt-get install nginx -y

But nginx is not installed after VM boots. What is the likely problem?
medium
A. The script lacks 'sudo' before commands
B. The script is missing the shebang line
C. The script runs before network is ready
D. The script should use 'yum' instead of 'apt-get'

Solution

  1. Step 1: Check script commands and environment

    The script uses apt-get which requires network access to update and install packages.
  2. Step 2: Identify timing issue

    Startup scripts may run before network is fully ready, causing apt-get to fail silently.
  3. Final Answer:

    The script runs before network is ready -> Option C
  4. Quick Check:

    Network must be ready before package install [OK]
Hint: Ensure network is ready before package installs in startup scripts [OK]
Common Mistakes:
  • Adding sudo unnecessarily (scripts run as root)
  • Ignoring network readiness in startup timing
  • Using wrong package manager for Debian-based VM
5. You want to automate VM setup to install Apache, create a website folder, and start the service on boot. Which startup script snippet correctly achieves this?
hard
A. #!/bin/bash apt-get update apt-get install apache2 -y mkdir /var/www/html systemctl enable apache2 systemctl start apache2
B. #!/bin/bash apt-get install apache2 mkdir /var/www/html service apache2 stop
C. #!/bin/bash apt-get update apt-get install apache2 -y mkdir -p /var/www/html systemctl start apache2
D. #!/bin/bash yum update -y yum install apache2 -y mkdir /var/www systemctl restart apache2

Solution

  1. Step 1: Verify package manager and commands

    For Debian-based VMs, apt-get is correct. Apache package is apache2. Creating /var/www/html is needed.
  2. Step 2: Check service management commands

    #!/bin/bash apt-get update apt-get install apache2 -y mkdir /var/www/html systemctl enable apache2 systemctl start apache2 uses 'systemctl enable' to start Apache on boot and 'systemctl start' to start immediately, which is best practice.
  3. Step 3: Compare other options

    #!/bin/bash apt-get update apt-get install apache2 -y mkdir -p /var/www/html systemctl start apache2 misses enabling service on boot. #!/bin/bash apt-get install apache2 mkdir /var/www/html service apache2 stop stops service instead of starting. #!/bin/bash yum update -y yum install apache2 -y mkdir /var/www systemctl restart apache2 uses yum (wrong for Debian).
  4. Final Answer:

    #!/bin/bash apt-get update apt-get install apache2 -y mkdir /var/www/html systemctl enable apache2 systemctl start apache2 -> Option A
  5. Quick Check:

    Enable and start service for automation [OK]
Hint: Enable and start services to run on boot in startup scripts [OK]
Common Mistakes:
  • Forgetting to enable service to start on boot
  • Using wrong package manager for the OS
  • Stopping service instead of starting it