Re-use provisioner script?

Hi,

I’m very new to terraform. Right now I use “remote-exec” to setup some tiny scripts/programs everytime I create new instance (Vultr provider).

I would like to check if there is a recommended way to re-use provision script? For example on every types of instance I need to setup a logger, it’s difficult to maintain when I have to copy and paste for each resource definition that I need to use that.

I looked into modules but I’m quite unsure how that can help in this situation.

Hi @yellow1912!

The provisioners feature is provided as a last resort for situations where no other option is available, and so the feature is not designed for broad use.

In your case, it seems like Vultr has the concept of “startup scripts” which are intended to help you run shell commands on system boot, without the need to connect to the system over SSH and run the commands directly. I don’t have personal experience with that feature (or indeed, with Vultr at all) but I would encourage you to consider using the startup scripts feature instead of using provisioner "remote-exec".

From reviewing the provider docs it seems like startup scripts are a separate entity from servers and so you can set up a single startup script using vultr_startup_script and associate it with many servers using the script_id argument to the various server resource types. That means you only need to find a solution to make the appropriate script_id to all of your modules that create servers, instead of sharing the content of the script itself.