Deploying GCP Virtual Machine Instance with a StartUp Script

This article guides us through deploying Virtual Machines (VM) Instances with a public accessible remote start-up script.

This would be helpful if you need to run a few commands on startup of your virtual machines. For example, update/install packages, perform a cron job etc... Also, you can always update the remote script contents whenever, without ever having to modify your instance’s configurations.

We’ll be hosting our startup script on Google Cloud Storage, feel free to use whatever you prefer, all we’ll need at the end is a publicly accessible URL to your startup script.

Uploading Startup Script to Google Cloud Storage Bucket

Login to Google Cloud Console , on the Navigation Menu, Click on Storage and then Browser. Let’s go ahead and create a bucket…

Enter a Unique Bucket Name and click on Create. Once done, we can now upload files to our bucket. Uploaded files are called Buckets.

I’ve successfully uploaded my startup script, however, I still need to make the script publicly accessible.

Making Google Cloud Storage Objects Publicly Available

Click on the Permissions tab and Enable Object-level permissions.

We’ll now be able to Edit Permissions on our objects.

Edit Permissions for Object

To make our Object public, Click on Edit Permissions and a New Item: User: Entity Name: allUsers Access: Reader

Copy Object Public URL


Setting up Virtual Machine Instance with StartUp Script

We’ll head over to Compute Engine > VM Instances and click on Create Instances Go ahead and leave all default for the VM Instance, Click on the dropdown for Management, security, disks, networking, sole tenancy. In the Metadata section, we’ll add our startup-script-url

Remember to change the value of startup-script-url to your startup script URL.

Go ahead and Create your virtual machine. 🕺🕺🕺

Another option to add startup scripts to VM instances is to add the contents of your script to Automation > StartUp Script (pasting the contents of your script works too).

If you’d like a faster way of doing this, the GCloud command-line tool does this in just one line:

gcloud compute instances create my-instance --scopes storage-ro --metadata startup-script-url=[https://storage.googleapis.com/my-buckets/install-web.sh](https://storage.googleapis.com/my-buckets/install-web.sh)

Thanks! Looking forward to writing more tips about Google Cloud Platform 😉😉

Comments (1)

Add a comment
EZEKIAS BOKOVE's photo

The article is very interesting. Cool