google cloud platform – Run script after VM creation just once

The metadata_startup_script in your Compute Engine runs every time the instance boots. But if you want to specify a bash script that would run just once after the VM is created. For this you can use startup scripts with a flag file or cloud -init.

You can use the metadata_startup_script but add logic in the script to check the existence of a flag file. If the flag file exists, the script does nothing immediately. If the flag file doesn’t exist, execute the script and then create the flag file.

Cloud-init is also a very helpful tool for this purpose. Which provides more advanced configuration options for your instances, including running scripts only once.

Read more here: Source link