0
0
Terraformcloud~5 mins

Remote execution model in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Terraform's remote execution model lets you run your infrastructure commands on a remote server instead of your local computer. This helps teams work together safely and keeps your infrastructure state secure and consistent.
When multiple team members need to work on the same infrastructure without conflicts
When you want to keep your infrastructure state file safe and backed up remotely
When you want to run Terraform commands from a central server or CI/CD pipeline
When you want to avoid installing Terraform and dependencies on every developer's machine
When you want to enforce consistent Terraform versions and plugins across your team
Config File - main.tf
main.tf
terraform {
  backend "remote" {
    organization = "example-org"

    workspaces {
      name = "example-workspace"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example" {
  bucket = "example-terraform-remote-execution"
  acl    = "private"
}

The terraform block configures the remote backend, specifying the organization and workspace to store the state remotely.

The provider block sets the AWS region.

The resource block creates an S3 bucket as an example resource.

Commands
Initializes Terraform and configures the remote backend to store state remotely.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Successfully configured the backend "remote"! Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, you must run this command again to reinitialize your working directory.
Shows the changes Terraform will make to your infrastructure using the remote execution model.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_s3_bucket.example will be created + resource "aws_s3_bucket" "example" { + acl = "private" + bucket = "example-terraform-remote-execution" + id = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy. ───────────────────────────────────────────────────────────────────────────── Note: You didn't specify an "-out" parameter to save this plan, so when "terraform apply" is run, Terraform can't guarantee this is what will execute.
Applies the planned changes remotely without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.example: Creating... aws_s3_bucket.example: Creation complete after 2s [id=example-terraform-remote-execution] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skips interactive approval before applying changes
Displays the current state of your infrastructure stored remotely.
Terminal
terraform show
Expected OutputExpected
aws_s3_bucket.example: id = example-terraform-remote-execution acl = private bucket = example-terraform-remote-execution
Key Concept

If you remember nothing else from this pattern, remember: remote execution runs Terraform commands on a shared server to keep your infrastructure state safe and consistent across your team.

Common Mistakes
Not running 'terraform init' after configuring the remote backend
Terraform won't connect to the remote backend and will use local state instead, causing conflicts.
Always run 'terraform init' after changing backend settings to initialize remote state.
Sharing the local state file instead of using remote backend
Local state files can get out of sync and cause infrastructure conflicts or data loss.
Use remote backend to store state centrally and avoid sharing local files.
Running Terraform commands without proper permissions on the remote backend
Terraform commands will fail due to lack of access to the remote workspace or state.
Ensure your user has correct permissions in the remote backend organization and workspace.
Summary
Configure the remote backend in the Terraform configuration file to store state remotely.
Run 'terraform init' to initialize the remote backend connection.
Use 'terraform plan' and 'terraform apply' to manage infrastructure remotely and safely.
Check the current infrastructure state with 'terraform show' to verify remote execution results.