Which provision is best to use to run a script on a virtual machine created by Terraform?

Prepare for the HashiCorp Terraform Associate Exam with quizzes, flashcards, and multiple-choice questions. Each question includes hints and explanations. Boost your confidence and ace your exam!

Multiple Choice

Which provision is best to use to run a script on a virtual machine created by Terraform?

Explanation:
When you want to run commands on a VM right after it’s created, you need a provisioner that executes on the remote resource itself. The remote-exec provisioner connects to the new VM (typically via SSH for Linux or WinRM for Windows) and runs the given script or inline commands, making it ideal for bootstrapping software, applying initial configuration, or running setup steps inside the VM. Local-exec runs commands on the machine running Terraform, not on the VM, so it wouldn’t execute your script inside the VM. The file provisioner is used to copy files to the remote resource but doesn’t run them by itself. The null_resource approach can orchestrate actions, but it’s not specifically about executing a script on the created VM. So the remote-exec option is the best fit for running a script on the VM you just created.

When you want to run commands on a VM right after it’s created, you need a provisioner that executes on the remote resource itself. The remote-exec provisioner connects to the new VM (typically via SSH for Linux or WinRM for Windows) and runs the given script or inline commands, making it ideal for bootstrapping software, applying initial configuration, or running setup steps inside the VM.

Local-exec runs commands on the machine running Terraform, not on the VM, so it wouldn’t execute your script inside the VM. The file provisioner is used to copy files to the remote resource but doesn’t run them by itself. The null_resource approach can orchestrate actions, but it’s not specifically about executing a script on the created VM. So the remote-exec option is the best fit for running a script on the VM you just created.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy