0
0
GCPcloud~5 mins

Startup scripts for automation in GCP - Commands & Configuration

Choose your learning style9 modes available
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.