It seems like I can use either user_data
with a template file or a "remote-exec" provisioner
with inline commands to bootstrap. So which one is considered more idiomatic?
Provider development teams often prioritize features based on interest, so opening an issue is a way to record your interest in the feature. Provisioners are used to execute scripts on a local or remote machine as part of resource creation or destruction.
Destroy-time provisioners If when = destroy is specified, the provisioner will run when the resource it is defined within is destroyed. Destroy provisioners are run before the resource is destroyed. If they fail, Terraform will error and rerun the provisioners again on the next terraform apply.
The local-exec provisioner invokes a local executable after a resource is created. This invokes a process on the machine running Terraform, not on the resource.
User data is a helpful tool to get rid of routine operations after server provisioning. You can get a ready-to-use server with additional software installed and configured according to your specification. The feature is built upon the cloud-init package for Linux operating systems.
You should use user_data
. The user data field is idiomatic because it's native to AWS, whereas the remote-exec provisioner is specific to Terraform, which is just one of many ways to call the AWS API.
Also, the user-data is viewable in the AWS console, and often an important part of using Auto Scaling Groups in AWS, where you want each EC2 Instance to execute the same config code when it launches. It's not possible to do that with Terraform's remote-exec provisioner.
Though I do agree with Josh, if there are no run time changes to the instance you can use packer to build an ami and then use that in the launch config. That way you don't have to wait for user-data to run.
Packer is part of the Hashicorp family of tools
https://www.packer.io/docs/builders/amazon-ebs.html
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