Provision a File on a Virtual Machine Using Terraform File Provisioner
📖 Scenario: You are setting up a virtual machine (VM) in the cloud. You want to place a configuration file on this VM automatically during its creation.This helps you prepare the VM with necessary settings without manual copying.
🎯 Goal: Create a Terraform configuration that provisions a file named config.txt with specific content onto the VM using the file provisioner.
📋 What You'll Learn
Define a resource for a virtual machine named example_vm.
Create a local file named config.txt with the exact content environment=production.
Use the file provisioner to copy config.txt to the VM's /tmp/config.txt path.
Ensure the provisioner is correctly attached to the example_vm resource.
💡 Why This Matters
🌍 Real World
Automating file placement on virtual machines during provisioning saves time and reduces manual errors in cloud infrastructure setup.
💼 Career
Cloud engineers and DevOps professionals often use file provisioners in Terraform to prepare VMs with necessary configuration files automatically.
Progress0 / 4 steps
1
Create a virtual machine resource named example_vm
Write a Terraform resource block for a virtual machine named example_vm using the null_resource provider as a placeholder.
Terraform
Hint
Use resource "null_resource" "example_vm" { } to create a simple VM placeholder.
2
Create a local file named config.txt with content environment=production
Add a Terraform local_file resource named config_file that creates a file config.txt with the content environment=production.
Terraform apply fails with a connection error. What is the most likely cause?
medium
A. The destination path must be a directory, not a file
B. The password authentication is not supported; use private_key instead
C. The connection block is inside the provisioner block, but it should be at the resource level
D. The source file path is incorrect
Solution
Step 1: Check connection block placement
In Terraform, the connection block must be defined at the resource level, not inside the provisioner block.
Step 2: Understand connection block scope
Placing connection inside provisioner causes Terraform to not find connection details, leading to connection errors.
Final Answer:
The connection block is inside the provisioner block, but it should be at the resource level -> Option C
Quick Check:
Connection block at resource level, not inside provisioner = D [OK]
Hint: Place connection block outside provisioner, inside resource [OK]
Common Mistakes:
Putting connection block inside provisioner
Assuming password auth is always unsupported
Mistaking file path or destination format as cause
5. You want to copy multiple files from your local ./scripts folder to the remote server's /usr/local/bin directory using the file provisioner. Which approach is best to achieve this efficiently?
hard
A. Use a single file provisioner with source = "./scripts" and destination = "/usr/local/bin" to copy the whole folder
B. Write multiple file provisioner blocks, one for each file inside ./scripts
C. Compress the folder into a zip file, copy it with file provisioner, then unzip remotely with remote-exec
D. Use a remote-exec provisioner to download files from a public URL instead
The file provisioner natively supports copying entire folders recursively by specifying a folder as source and a directory as destination.
Step 2: Differentiate efficiency of approaches
Use a single file provisioner with source = "./scripts" and destination = "/usr/local/bin" to copy the whole folder directly copies the directory contents to /usr/local/bin efficiently without extra compression or multiple blocks.
Final Answer:
Use a single file provisioner with source = "./scripts" and destination = "/usr/local/bin" to copy the whole folder -> Option A
Quick Check:
File provisioner copies dir recursively = A [OK]
Hint: File provisioner copies dir recursively with source=dir destination=dir [OK]