0
0
Terraformcloud~30 mins

Why provisioners are a last resort in Terraform - See It in Action

Choose your learning style9 modes available
Why Provisioners Are a Last Resort in Terraform
📖 Scenario: You are managing cloud infrastructure using Terraform. You want to understand how to handle tasks that Terraform does not natively support, like running scripts on a virtual machine after it is created.
🎯 Goal: Build a simple Terraform configuration that creates a virtual machine and uses a provisioner as a last resort to run a script on it.
📋 What You'll Learn
Create a Terraform resource for a virtual machine
Add a connection block to enable communication with the VM
Add a provisioner block to run a script on the VM
Understand why provisioners should be avoided when possible
💡 Why This Matters
🌍 Real World
Provisioners are sometimes needed to perform tasks that Terraform cannot do natively, like installing software or configuring a VM after creation.
💼 Career
Cloud engineers and DevOps professionals often need to understand when and how to use provisioners safely to avoid unstable infrastructure deployments.
Progress0 / 4 steps
1
Create a Terraform resource for a virtual machine
Write a Terraform resource block called aws_instance named example with the ami set to "ami-0c55b159cbfafe1f0" and instance_type set to "t2.micro".
Terraform
Need a hint?

Use the resource keyword with aws_instance and specify the ami and instance_type exactly as given.

2
Add a connection block to enable communication with the VM
Add a connection block inside the aws_instance.example resource with type set to "ssh", user set to "ec2-user", and private_key set to file("~/.ssh/id_rsa").
Terraform
Need a hint?

The connection block tells Terraform how to connect to the VM for running commands.

3
Add a provisioner block to run a script on the VM
Inside the aws_instance.example resource, add a provisioner block of type remote-exec that runs the command "echo Hello, Terraform! > /tmp/hello.txt".
Terraform
Need a hint?

The provisioner runs commands on the VM after it is created. Use remote-exec with an inline command.

4
Add a comment explaining why provisioners are a last resort
Add a comment at the top of the Terraform file explaining: "Provisioners are a last resort because they can cause unpredictable failures and make infrastructure harder to manage."
Terraform
Need a hint?

Write a clear comment at the top of the file explaining the risks of using provisioners.