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?
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.
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.
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:
As for myself, I rather use the first option as I find it clearer and easier to implement and understand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With