0
0
Terraformcloud~30 mins

Why provisioners run scripts on resources in Terraform - See It in Action

Choose your learning style9 modes available
Why provisioners run scripts on resources
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Check that both provisioners are inside the resource with correct commands and connection details.