Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform - should I use user_data or provisioner to bootstrap a resource?

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?

like image 342
Chris Avatar asked Jun 05 '17 21:06

Chris


People also ask

What is difference between Provisioner and provider in Terraform?

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.

Why Provisioners are not recommended in Terraform?

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.

What is the purpose of using the local-exec Provisioner?

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.

What is User_data in Terraform?

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.


2 Answers

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.

like image 193
Josh Padnick Avatar answered Sep 19 '22 19:09

Josh Padnick


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

like image 20
strongjz Avatar answered Sep 21 '22 19:09

strongjz