0
0
Terraformcloud~10 mins

Remote-exec provisioner in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Remote-exec provisioner
Terraform starts apply
Create resource (e.g., VM)
Wait for resource to be ready
Run remote-exec provisioner
Connect via SSH or WinRM
Execute commands on remote machine
Provisioning complete
Terraform apply finishes
Terraform creates the resource, then connects remotely to run commands on it using SSH or WinRM, completing provisioning.
Execution Sample
Terraform
resource "aws_instance" "example" {
  ami                         = "ami-12345678"
  instance_type               = "t2.micro"
  associate_public_ip_address = true
  key_name                    = "example-key"

  provisioner "remote-exec" {
    connection {
      type        = "ssh"
      user        = "ubuntu"
      host        = self.public_ip
      private_key = file("~/.ssh/mykey.pem")
    }
    inline = ["sudo apt-get update", "sudo apt-get install -y nginx"]
  }
}
This code creates an AWS instance and runs commands remotely to update packages and install nginx.
Process Table
StepActionResource StateProvisioner ConnectionCommands ExecutedResult
1Start terraform applyNo resourceNo connectionNo commandsBegin process
2Create aws_instanceInstance launchingNo connectionNo commandsInstance created but not ready
3Wait for instance readyInstance runningNo connectionNo commandsInstance ready for connection
4Connect via SSHInstance runningSSH connectedNo commandsConnection established
5Run command: sudo apt-get updateInstance runningSSH connectedsudo apt-get updateCommand success
6Run command: sudo apt-get install -y nginxInstance runningSSH connectedsudo apt-get install -y nginxCommand success
7Provisioning completeInstance runningSSH disconnectedAll commands doneProvisioning finished
8Terraform apply completeInstance runningNo connectionNo commandsApply finished successfully
💡 All commands executed successfully, provisioning finished, terraform apply completes.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 7Final
Resource StateNo resourceInstance launchingInstance runningInstance runningInstance runningInstance running
Provisioner ConnectionNo connectionNo connectionNo connectionSSH connectedSSH disconnectedNo connection
Commands ExecutedNoneNoneNoneNoneUpdate, Install nginxAll commands done
Key Moments - 3 Insights
Why does terraform wait before running remote-exec commands?
Terraform waits until the instance is fully running and accessible (see Step 3) before connecting via SSH (Step 4) to avoid connection failures.
What happens if the remote-exec commands fail?
If commands fail, terraform apply stops and reports an error at the provisioner step (between Steps 5-6), preventing incomplete provisioning.
Does remote-exec run before or after resource creation?
Remote-exec runs only after the resource is created and ready (after Step 3), as shown by connection established in Step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does terraform establish SSH connection?
AStep 4
BStep 2
CStep 6
DStep 8
💡 Hint
Check the 'Provisioner Connection' column for when it changes to 'SSH connected'.
At which step does terraform finish running all remote commands?
AStep 5
BStep 7
CStep 6
DStep 8
💡 Hint
Look for 'Commands Executed' column showing all commands done.
If the instance was not ready, what would happen to the connection step?
AConnection would succeed immediately
BTerraform would skip remote-exec
CTerraform would wait until instance is ready before connecting
DTerraform would fail apply instantly
💡 Hint
Refer to Step 3 and Step 4 showing waiting and then connection.
Concept Snapshot
Terraform Remote-exec provisioner:
- Runs commands on a remote resource after creation
- Connects via SSH (Linux) or WinRM (Windows)
- Waits for resource readiness before connecting
- Executes inline or script commands
- Fails apply if commands fail
- Used for initial setup/configuration
Full Transcript
Terraform Remote-exec provisioner works by first creating the resource, such as a virtual machine. Terraform waits until the resource is fully running and accessible. Then it connects remotely using SSH or WinRM. After connection, it runs the specified commands on the remote machine. These commands can update software or install packages. If any command fails, terraform stops and reports an error. Once all commands succeed, provisioning is complete and terraform finishes the apply process.