-1

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.

0

2 Answers 2

0

Indeed, 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but I'm looking for a pulumi-native method, e.g. some post-create hook, and not bash-based solutions.
0

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.

3 Comments

Refer to this Stack link and Serverfault link may helpful to resolve your issue.
Could you provide a minimal example with pulumi and cloud init?
@dimid To use Cloud-init in pulumi. you need to provide a cloud-init script as metadata when creating a vm. which helps to run only once during the VM first boot. Refer to this Github link and Cloud-init documentation by Nathaniel Stickman for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.