📖 Scenario: You are setting up a virtual machine in the cloud. After creating the machine, you want to install software automatically on it. This helps save time and ensures the machine is ready to use right after creation.
🎯 Goal: Learn how to use Terraform provisioners to run scripts on cloud resources after they are created.
📋 What You'll Learn
Create a virtual machine resource
Add a provisioner to run a script on the virtual machine
Use a local-exec provisioner to run a simple command
Use a remote-exec provisioner to run commands on the machine
💡 Why This Matters
🌍 Real World
Automating software installation and configuration on cloud servers right after they are created saves time and reduces manual errors.
💼 Career
Cloud engineers and DevOps professionals use provisioners to automate resource setup, making deployments faster and more reliable.
Progress0 / 4 steps
1
Create a virtual machine resource
Write Terraform code to create an aws_instance resource named example with the AMI ID ami-0c55b159cbfafe1f0 and instance type t2.micro.
Terraform
Hint
Use the resource block with type aws_instance and name example. Set ami and instance_type exactly as given.
2
Add a local-exec provisioner to run a script locally
Add a provisioner block inside the aws_instance.example resource that uses local-exec to run the command echo 'Instance created'.
Terraform
Hint
Inside the resource, add provisioner "local-exec" with a command that echoes the text.
3
Add a remote-exec provisioner to run commands on the instance
Add a provisioner block inside aws_instance.example that uses remote-exec to run the command sudo apt-get update. Use connection block with type = "ssh", user = "ubuntu", and private_key = file("~/.ssh/id_rsa").
Terraform
Hint
Use provisioner "remote-exec" with inline commands and a connection block for SSH details.
4
Complete the resource with both provisioners
Ensure the aws_instance.example resource includes both the local-exec provisioner that runs echo 'Instance created' and the remote-exec provisioner that runs sudo apt-get update with the correct SSH connection settings.
Terraform
Hint
Check that both provisioners are inside the resource with correct commands and connection details.
Practice
(1/5)
1. Why do Terraform provisioners run scripts on resources?
easy
A. To backup resource data before creation
B. To delete resources automatically
C. To configure resources after they are created
D. To monitor resource usage continuously
Solution
Step 1: Understand provisioners' purpose
Provisioners run scripts to configure resources after Terraform creates them.
Step 2: Identify correct use case
They help install software or run commands Terraform cannot manage directly.
Final Answer:
To configure resources after they are created -> Option C
Quick Check:
Provisioners configure resources post-creation = D [OK]
Hint: Provisioners run scripts after resource creation [OK]
Common Mistakes:
Thinking provisioners delete resources
Assuming provisioners monitor resources
Believing provisioners backup data
2. Which of the following is the correct syntax to use a local-exec provisioner in Terraform?
easy
A. provisioner "local-exec" { command = "echo Hello" }
B. provisioner local_exec { run = "echo Hello" }
C. provisioner "local-exec" (command: "echo Hello")
D. provisioner local-exec { execute = "echo Hello" }
Solution
Step 1: Recall correct provisioner block syntax
Terraform uses provisioner blocks with type in quotes and a command attribute.
B. The command attribute is invalid for remote-exec
C. remote-exec provisioner requires a local script file
D. Missing connection block to specify how to connect to the resource
Solution
Step 1: Check remote-exec requirements
remote-exec provisioner needs a connection block to connect to the resource (SSH or WinRM).
Step 2: Identify missing connection block
The snippet lacks a connection block, causing Terraform to error because it cannot connect to run the command.
Final Answer:
Missing connection block to specify how to connect to the resource -> Option D
Quick Check:
remote-exec needs connection block = C [OK]
Hint: Always add connection block for remote-exec provisioners [OK]
Common Mistakes:
Assuming command attribute is invalid
Thinking sudo commands are disallowed
Believing remote-exec runs local scripts only
5. You want to install software on a new VM after Terraform creates it. Which approach best uses provisioners to do this reliably?
hard
A. Use a remote-exec provisioner with a connection block to run installation commands after creation
B. Use a local-exec provisioner to run installation commands on your local machine
C. Run installation commands manually after Terraform finishes
D. Use a provisioner without connection block to run commands remotely
Solution
Step 1: Identify provisioner type for remote VM configuration
remote-exec provisioner runs commands on the remote VM after creation using a connection block.
Step 2: Evaluate options for reliability
Using remote-exec with connection block automates installation reliably; local-exec runs locally, manual runs are error-prone, and missing connection causes failure.
Final Answer:
Use a remote-exec provisioner with a connection block to run installation commands after creation -> Option A