What is the purpose of the file provisioner in Terraform?
The file provisioner copies files or directories from the machine running Terraform to the target resource, such as a virtual machine, during provisioning.
Click to reveal answer
beginner
Which argument specifies the source file or directory in a Terraform file provisioner?
The source argument specifies the path to the file or directory on the local machine that you want to copy to the target resource.
Click to reveal answer
beginner
How do you specify the destination path on the target machine in a Terraform file provisioner?
Use the destination argument to set the path on the target machine where the file or directory will be copied.
Click to reveal answer
intermediate
When does the file provisioner run during Terraform execution?
The file provisioner runs after the resource is created but before Terraform marks the resource as fully created.
Click to reveal answer
intermediate
Why should you avoid using provisioners like file when possible in Terraform?
Provisioners can cause unpredictable behavior and make infrastructure less declarative. It's better to use native resource configurations or configuration management tools for setup.
Click to reveal answer
What does the file provisioner do in Terraform?
ARuns shell commands on the target resource
BDeletes files on the target resource
CCreates new resources in the cloud
DCopies files from local machine to the target resource
✗ Incorrect
The file provisioner copies files or directories from the local machine to the target resource during provisioning.
Which argument specifies the destination path in a file provisioner?
Adestination
Bpath
Ctarget
Dsource
✗ Incorrect
The destination argument sets where the file will be copied on the target machine.
When does the file provisioner run during Terraform apply?
ABefore resource creation
BAfter resource creation
CDuring resource destruction
DBefore Terraform plan
✗ Incorrect
The file provisioner runs after the resource is created but before Terraform finishes the apply.
Which of these is a best practice regarding provisioners like file?
AUse them to create cloud resources
BUse them for all configuration tasks
CAvoid them when possible
DUse them to delete resources
✗ Incorrect
Provisioners can cause unpredictable results; it's better to avoid them when possible.
What must be true about the source path in a file provisioner?
AIt must exist on the local machine running Terraform
BIt must be a remote URL
CIt must be inside the target resource
DIt must be a cloud storage path
✗ Incorrect
The source path must be a local file or directory accessible to Terraform.
Explain how the file provisioner works in Terraform and when it is used.
Think about copying files to a new virtual machine after it is created.
You got /3 concepts.
Describe the key arguments needed to configure a file provisioner in Terraform.
What do you need to tell Terraform where to copy from and to?
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the file provisioner in Terraform?
easy
A. To execute shell commands on a remote server
B. To destroy resources after use
C. To create new cloud resources like virtual machines
D. To copy files from your local machine to a remote server during resource creation
Solution
Step 1: Understand the role of the file provisioner
The file provisioner is designed to transfer files from your local computer to a remote machine during Terraform resource setup.
Step 2: Differentiate from other provisioners
Unlike remote-exec which runs commands, file provisioner only copies files, not executes commands or manages resource lifecycle.
Final Answer:
To copy files from your local machine to a remote server during resource creation -> Option D
Quick Check:
File provisioner copies files = B [OK]
Hint: File provisioner = copy files to server [OK]
Common Mistakes:
Confusing file provisioner with remote-exec
Thinking it creates or destroys resources
Assuming it runs commands on the server
2. Which of the following is the correct syntax to use the file provisioner in a Terraform resource block?
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]