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
Startup scripts for automation
📖 Scenario: You are setting up a Google Cloud virtual machine (VM) that needs to automatically install and start a web server when it boots up. This automation saves time and ensures the server is ready without manual setup.
🎯 Goal: Create a startup script that installs the Apache web server and starts it automatically on a Google Cloud VM instance.
📋 What You'll Learn
Create a startup script variable with the exact shell commands to install Apache
Add a configuration variable to specify the Apache service name
Write the core logic to include the startup script in the VM instance metadata
Complete the VM instance creation with the startup script attached
💡 Why This Matters
🌍 Real World
Automating VM setup with startup scripts is common in cloud environments to save time and reduce manual errors when deploying servers.
💼 Career
Cloud engineers and DevOps professionals use startup scripts to automate instance configuration and ensure consistent environments.
Progress0 / 4 steps
1
Create the startup script variable
Create a variable called startup_script that contains the exact shell commands to update package lists, install Apache2, and start the Apache2 service using systemctl. Use the following commands exactly: sudo apt-get update -y, sudo apt-get install apache2 -y, and sudo systemctl start apache2 separated by semicolons.
GCP
Hint
Use a single string with commands separated by semicolons.
2
Add the Apache service name configuration
Create a variable called apache_service and set it to the string "apache2" to specify the Apache service name.
GCP
Hint
Set the variable exactly as shown.
3
Add the startup script to VM instance metadata
Create a dictionary called instance_metadata with a key "startup-script" and set its value to the startup_script variable.
GCP
Hint
Use a dictionary with the key exactly as "startup-script".
4
Complete VM instance creation with startup script
Create a dictionary called vm_instance with keys "name" set to "web-server", "machine_type" set to "n1-standard-1", and "metadata" set to the instance_metadata variable.
GCP
Hint
Include all keys exactly as specified.
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
Step 1: Understand startup script role
Startup scripts run automatically when a VM starts to perform tasks without manual intervention.
Step 2: Identify correct purpose
Among the options, only automating tasks at boot matches the startup script function.
Final Answer:
To automate tasks when the VM boots -> Option A
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
Step 1: Recall correct flag for startup script
The correct metadata key to add a startup script is 'startup-script'.
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
Step 1: Check script commands and environment
The script uses apt-get which requires network access to update and install packages.
Step 2: Identify timing issue
Startup scripts may run before network is fully ready, causing apt-get to fail silently.
Final Answer:
The script runs before network is ready -> Option C
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?
For Debian-based VMs, apt-get is correct. Apache package is apache2. Creating /var/www/html is needed.
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.
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).