📖 Scenario: You are setting up a cloud server and want to run a simple command on it right after it is created. This helps you install software or configure the server automatically.
🎯 Goal: Build a Terraform configuration that creates a virtual machine and uses the remote-exec provisioner to run a command on the server after creation.
📋 What You'll Learn
Create an AWS EC2 instance resource named aws_instance.example
Add a remote-exec provisioner inside the instance resource
Use the provisioner to run the command echo Hello, Terraform! on the instance
Configure connection details for the provisioner using SSH
💡 Why This Matters
🌍 Real World
Automating server setup by running commands immediately after the server is created saves time and reduces manual errors.
💼 Career
Cloud engineers and DevOps professionals use remote-exec provisioners to automate configuration and deployment tasks on cloud instances.
Progress0 / 4 steps
1
Create AWS EC2 instance resource
Create a resource block named aws_instance.example with ami set to "ami-0c55b159cbfafe1f0" and instance_type set to "t2.micro".
Terraform
Hint
The resource block defines the EC2 instance with the given AMI and instance type.
2
Add SSH connection configuration
Add a connection block inside aws_instance.example with type set to "ssh", user set to "ec2-user", and private_key set to file("~/.ssh/id_rsa").
Terraform
Hint
The connection block tells Terraform how to connect to the instance via SSH.
3
Add remote-exec provisioner to run a command
Inside aws_instance.example, add a provisioner "remote-exec" block that runs the command echo Hello, Terraform! using the inline argument.
Terraform
Hint
The remote-exec provisioner runs commands on the remote instance after creation.
4
Add public IP to connect to the instance
Add associate_public_ip_address = true inside the aws_instance.example resource to ensure the instance gets a public IP for SSH connection.
Terraform
Hint
Setting associate_public_ip_address to true allows SSH access to the instance.
Practice
(1/5)
1. What is the main purpose of the remote-exec provisioner in Terraform?
easy
A. To run commands on a remote machine after it is created
B. To create a new virtual machine in the cloud
C. To store Terraform state files remotely
D. To manage user permissions on the local machine
Solution
Step 1: Understand the role of provisioners
Provisioners in Terraform run scripts or commands on resources after creation.
Step 2: Identify what remote-exec does
The remote-exec provisioner connects to the remote machine and runs commands there.
Final Answer:
To run commands on a remote machine after it is created -> Option A
Quick Check:
remote-exec runs remote commands = D [OK]
Hint: remote-exec runs commands remotely after resource creation [OK]
Common Mistakes:
Confusing remote-exec with resource creation
Thinking remote-exec manages local files
Mixing remote-exec with state management
2. Which of the following is the correct syntax to define a remote-exec provisioner inside a Terraform resource?
easy
A. remote_exec { commands = ["echo Hello"] }
B. provisioner remote-exec { script = "hello.sh" }
C. provisioner "remote-exec" { inline = ["echo Hello"] }
D. exec_remote { inline = ["echo Hello"] }
Solution
Step 1: Recall Terraform provisioner syntax
Provisioners are defined with the keyword provisioner followed by the provisioner type in quotes.
Step 2: Check the correct block structure for remote-exec
The remote-exec provisioner uses inline to run commands directly.
After running Terraform apply, you get a connection timeout error. What is the most likely cause?
medium
A. The EC2 instance's security group does not allow SSH (port 22) inbound
B. The password authentication is not supported by SSH
C. The inline commands have syntax errors
D. The host attribute should be a private IP, not public IP
Solution
Step 1: Understand connection timeout causes
Timeout usually means network connection to SSH port 22 is blocked or unreachable.
Step 2: Check common network issues
If the EC2 security group lacks inbound SSH rules, connection fails despite correct credentials.
Final Answer:
The EC2 instance's security group does not allow SSH (port 22) inbound -> Option A
Quick Check:
Timeout = network block on SSH port 22 [OK]
Hint: Timeout usually means SSH port 22 blocked in firewall/security group [OK]
Common Mistakes:
Blaming inline command syntax for connection errors
Assuming password auth is always unsupported
Confusing public and private IP for SSH host
5. You want to use remote-exec to run a script on multiple newly created instances in a loop. Which approach is best to ensure commands run on each instance correctly?
hard
A. Use local-exec provisioner to SSH manually to each instance
B. Define one resource and run remote-exec with a list of all instance IPs in one provisioner
C. Run remote-exec provisioner only once after all instances are created using a null_resource
D. Use a for_each on the resource and include a remote-exec provisioner inside each resource block
Solution
Step 1: Understand resource loops with provisioners
Using for_each creates multiple resource instances, each with its own provisioner execution.
Step 2: Why per-resource provisioner is best
Provisioners inside each resource run commands on that specific instance, ensuring correct targeting.
Final Answer:
Use a for_each on the resource and include a remote-exec provisioner inside each resource block -> Option D
Quick Check:
Provisioner per resource instance = A [OK]
Hint: Use for_each with provisioner inside resource for multiple instances [OK]
Common Mistakes:
Trying to run remote-exec on all IPs in one provisioner
Using null_resource without proper triggers
Using local-exec instead of remote-exec for remote commands
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.