

Sudipta Deb
Founder of Technical Potpourri, Co-Founder of Shrey Tech, Enterprise Cloud Architect
In Google Cloud Platform (GCP), a startup script is a script that runs automatically when a virtual machine (VM) instance starts up. It's a powerful tool for automating tasks like:
- Software Installation: Install necessary software, packages, or dependencies.
- System Configuration: Configure system settings, environment variables, and services.
- Service Startup: Start required services or applications.
- Updates and Patches: Apply system updates and security patches.
Watch The Video Or Read The Article (Or Do Both
)
The form you have selected does not exist.

What Are The Benefits?
There are multiple benefits, like:
- Time-saving: Automates repetitive tasks.
- Consistency: Ensures consistent VM configurations.
- Scalability: Easily deploy and manage multiple VMs with the same configuration.
Ways To Use Startup Scripts
There are literally two ways you can use the Startup Scripts while creating Virtual Machines on the GCP platform
- Write the Startup Scripts specific for that Virtual Machine
- Write startup scripts, store them in Google Cloud Storage Bucket, and refer to them while creating Virtual Machine.
In this blog post, I will explain both ways.
# 1: Create Startup Scripts For Virtual Machine
In this approach, I will create the Startup Script for the Virtual Machine and add that while configuring it.
While doing the configuration of the Virtual Machine, I will click on Advanced and then under Automation section, I will include my Startup Script (mentioned below).
#!/bin/bash
echo "*** Startup Script From Automation Section ***"
apt-get update
apt-get install -y apache2
echo "<h1>Hello World from technical potpourri !!</h1>" > /var/www/html/index.htmlNow to check whether the Startup Script has executed successfully or not, I will check the log. To do that, I will go inside the virtual machine by clicking on the name from the console. Once there, I will click on the "Serial port 1 (console)" link to open the logs in a different tab. This is the place where I can find out the entire log and search to make sure my startup script has executed successfully.
Finally, when I open the external IP in a different tab, I will see that new content in the window, which indicates that my startup script has executed successfully.
So that's awesome. But if you think deeply, this way is good for one or a few virtual machines where I can add the startup scripts for each of the virtual machines while doing the configuration for them. But now if you think from a bigger team's standpoint, where multiple developers are working on creating multiple virtual machines, this approach is not efficient. Because it can introduce errors, developers can change the startup script. Instead we should have one startup script which can be shared by all the virtual machines.
And that is what the second approach is all about.
# 2: Share Startup Scripts For All Virtual Machines
In this approach, I will first create and save the startup script in my local desktop with a name "startup.sh" (You can use any name).
After that I will create the Google Cloud Storage Bucket and upload the startup.sh file inside the bucket. Screenshot given below.

Once the startup script is inside the bucket, I will refer to it while configuring the virtual machine. Inside virtual machine configuration section, under Metadata, I will add the key value to refer to the startup script
Key: startup-script-url
Value: gs://my-startup-script-bucket/startup.sh

Once the virtual machine is ready, I will see the details while opening up the External IP in a new tab.
So now you can understand that with this approach, all of your team members can use the same startup script while creating new virtual machines. This is definitely the preferred approach.
Access Metadata Using Startup Scripts
Using Startup Script, it is possible to access the metadata that is being configured in Virtual Machine. For that this is going to be my startup script where I am fetching the metadata data with key name as Foo and displaying them inside index.html file.
#!/bin/bash
METADATA_VALUE=$(curl http://metadata.google.internal/computeMetadata/v1/instance/attributes/foo -H "Metadata-Flavor: Google")
echo "*** Startup Script From Automation Section ***"
apt-get update
apt-get install -y apache2
cat <<EOF > /var/www/html/index.html
<h1>Hello World from technical potpourri !!</h1>
<html><body><p>Accessing metadata value of foo: $METADATA_VALUE</p></body></html>
EOFOnce the starup script is ready, I will upload them inside the same bucket with a different name let's say - new-startup.sh. With the file available inside bucket, I will configure the virtual machine. Everything will be same, just two differences as shown in the screenshot below

With this configuration, when I will open the external IP in a separate tab, I will see the below details where the metadata is being fetched in Starup Scripts and then dipslayed.

Final Thoughts
That's all you need to know about startup scripts in Google Cloud Platform and it's usage. Please provide me your comments on how you use this in your project today. Thanks !!
Disclaimer
This article is not endorsed by Salesforce, Google, or any other company in any way. I shared my knowledge on this topic in this blog post. Please always refer to Official Documentation for the latest information.



0 Comments