Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcloud: running a script only on first boot

What is the best way to run a script using gcloud compute instances create only on first boot. I have looked at startup-script, but they are run each time the instance reboots or restarts.

Alternatively what is the best practice on setting up an instance, besides using own image?

like image 492
jethar Avatar asked Apr 09 '15 15:04

jethar


People also ask

What is Startup script in GCP?

A startup script is a file that contains commands that run when a virtual machine (VM) instance boots. Compute Engine provides support for running startup scripts on Linux VMs and Windows VMs. The following table contains links to documentation that describes how to use startup scripts. Startup script task.

How do I create a shutdown script in GCP?

Apply a shutdown script to running instances Using the Google Cloud CLI, you can provide the path to a shutdown script file, using the --metadata-from-file flag and the shutdown-script metadata key. shutdown-script-url : Supply a Cloud Storage URL to the shutdown script file with this key.


1 Answers

Use images for heavy configuration stuff: Packages, and image basic configuration. Due to the life cycle of instances in Cloud, you should find the right balance between stateless and stateful configuration for your solution.

The best way to configure an instance, IMHO, is using the startup scripts as much as you can, as they can be changed and updated without risk of loosing access to the instances due to a wrong persistent configuration. As for your question, I can think of several different options:

  1. Checking for a flag file created from the startup script when the first boot is finished and terminating the startup script if the file exists.
  2. Modifying the startup script for the instance at initial startup script to something different at the end of first time execution.
  3. Setting up an automated configuration software ready for action at your image as cfengine, puppet or chef...

As for myself, I rather use the first option as I find it clearer and easier to implement and understand.

like image 143
Antxon Avatar answered Jan 01 '23 23:01

Antxon