I'm using Pulumi on GCP, and I'd like to specify a bash script that would run just once after the VM is created. I saw that compute.Instance
has the parameter metadata_startup_script
, but IIUC it will run each time the machine boots.
I'm using Pulumi on GCP, and I'd like to specify a bash script that would run just once after the VM is created. I saw that compute.Instance
has the parameter metadata_startup_script
, but IIUC it will run each time the machine boots.
2 Answers
Reset to default 0Indeed, metadata_startup_script
runs every time the machine boots, but you can add set an environment variable (maybe something inside .bashrc
like $INIT_RUN=1) the first time you run it, and skip running it when the variable is already set.
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.