0
0
Terraformcloud~5 mins

Why remote state matters for teams in Terraform - Why It Works

Choose your learning style9 modes available
Introduction
When multiple people work on the same infrastructure, they need a shared place to keep the current setup details. Remote state in Terraform helps teams avoid conflicts and keeps everyone updated on the infrastructure changes.
When several team members update cloud resources and need to see the latest changes.
When you want to prevent two people from changing the same resource at the same time.
When you want to keep a backup of your infrastructure setup safely outside your computer.
When you want to track infrastructure changes over time in a shared place.
When you want to use Terraform Cloud or other services to manage your infrastructure state.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
  backend "local" {}
}

resource "null_resource" "example" {
  provisioner "local-exec" {
    command = "echo Hello, Terraform!"
  }
}

This simple Terraform configuration uses a local backend by default. It defines a resource that runs a local command. To switch to remote state, you replace the backend block with a remote backend configuration like Terraform Cloud or S3.

The backend block tells Terraform where to store the state file, which keeps track of your infrastructure.

Commands
This command initializes the Terraform working directory. It downloads necessary plugins and sets up the backend to store state. Run this first to prepare Terraform for use.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/null... - Installing hashicorp/null v3.1.0... - Installed hashicorp/null v3.1.0 (signed by HashiCorp) 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. If you ever set or change modules or backend configuration, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
This command applies the Terraform configuration to create or update infrastructure. The -auto-approve flag skips the confirmation step to speed up the process.
Terminal
terraform apply -auto-approve
Expected OutputExpected
null_resource.example: Creating... null_resource.example: Provisioning with 'local-exec'... null_resource.example (local-exec): Executing: ["/bin/sh" "-c" "echo Hello, Terraform!"] null_resource.example (local-exec): Hello, Terraform! null_resource.example: Creation complete after 0s [id=1234567890] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply without asking for confirmation
This command lists all resources tracked in the current Terraform state. It helps verify what Terraform knows about your infrastructure.
Terminal
terraform state list
Expected OutputExpected
null_resource.example
This command reinitializes Terraform to use a remote backend stored in an S3 bucket. It configures where the state file is saved remotely so the team can share it.
Terminal
terraform init -backend-config="bucket=my-terraform-state" -backend-config="key=example/terraform.tfstate" -backend-config="region=us-east-1"
Expected OutputExpected
Initializing the backend... Successfully configured the backend "s3"! 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. If you ever set or change modules or backend configuration, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
-backend-config - Specify backend configuration options like bucket name and region
Key Concept

If you remember nothing else from this pattern, remember: remote state lets your team share and protect the current infrastructure setup so everyone works together smoothly.

Common Mistakes
Not configuring remote state and letting each team member use local state files.
This causes conflicts and overwrites because each person has their own copy of the state, leading to errors and lost changes.
Set up a remote backend like S3 or Terraform Cloud so all team members use the same shared state file.
Changing the backend configuration without running 'terraform init' again.
Terraform will not apply the new backend settings and may continue using the old state location, causing confusion.
Always run 'terraform init' after changing backend settings to reinitialize Terraform properly.
Summary
Run 'terraform init' to prepare Terraform and set up the backend for state storage.
Use 'terraform apply' to create or update infrastructure and save the state.
Switch to a remote backend to share the state file safely among team members.
Use 'terraform state list' to see what resources Terraform manages in the current state.