0
0
Terraformcloud~30 mins

File provisioner in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a hint?

Use resource "local_file" "config_file" { filename = "config.txt" content = "environment=production" }.

3
Add a file provisioner to copy config.txt to the VM
Inside the example_vm resource, add a provisioner "file" block that copies the local file config.txt to the remote path /tmp/config.txt.
Terraform
Need a hint?

Use provisioner "file" { source = "config.txt" destination = "/tmp/config.txt" } inside example_vm.

4
Complete the Terraform configuration with proper resource structure
Ensure the example_vm resource includes the file provisioner block exactly as specified, and the local_file resource is defined after it.
Terraform
Need a hint?

Check that the provisioner is inside example_vm and local_file resource is present.