Startup Script for Compute Engine

The startup script is a file that performs various required tasks when your Virtual machine startup. You can have requirements where every machine that will run inside your project to have some preinstalled tools or any job you want your machine to perform when it startup. This post will discuss multiple ways to utilize the startup script for your Compute Engine using GCP UI.

Startup script using Automation section

  1. While creating a new compute Engine, expand “Networking, Disks, Security, Management, Sole-Tenancy.
  2. Under the “Management” section, you can see box named “Automation” where you can provide your startup script.

I am using a startup script to install Apache Webserver on my machine in my screenshot.

3. After machine creation, I can see Apache webserver is installed by accessing the public IP of a virtual machine from my browser. If you are testing, please make sure you pass HTTP and not HTTPS unless you have installed certificates. Please also make sure your firewall also allows traffic on port 80.

http://<Public IP>/

Want to know what information was passed in the “Automation” tab after machine creation?

After creating this machine, you can find your startup script under the “Custom metadata” section in Compute instance details.

Startup Script using Metadata field

  1. While creating a new compute Engine, expand “Networking, Disks, Security, Management, Sole-Tenancy”
  2. Under the “Management” section, you can see “Metadata.” Metadata is stored as Key: Value pair. For startup script Metadata key can be “startup-script” or “startup-script-url”.

You can use “startup-script-url” when you are creating the machine or after machine creation and “startup-script” to already running machines, where you might have forgotten to pass any startup script in the “Automation” section.

Now you might be thinking about why you want to use “startup-script-url” if you can pass the script directly. Passing bash or non-bash startup script has a limitation of 256 KB size. Using “startup-script-url, you can give a script above 256KB size as well. For “startup-script-url,” you can pass URL for a script that is stored in Cloud Storage.

If I remove Apache and reboot the machine, it will automatically reinstall the apache web as mentioned in my startup script.

Please also be aware that you can configure Project level Metadata to add startup script as per your need. In Compute Engine Tab, you can find a setting option where you can configure Project Metadata.

Here we can configure the startup script as well with the same key name “startup-script” or “startup-script-url” or both.

I hope you know you have a good understanding of how to use the Startup script with Compute Engine.

One Reply to “Startup Script for Compute Engine”

  1. I抦 not that much of a online reader to be honest but your blogs really nice, keep it up! I’ll go ahead and bookmark your site to come back in the future. Cheers

Leave a Reply

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