0
0
Terraformcloud~5 mins

Terraform_remote_state usage - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you work on infrastructure with Terraform, you often need to share information between different parts of your setup. Terraform remote state lets you save and access this shared information safely and reliably.
When you have multiple Terraform projects that need to share outputs like IP addresses or resource IDs.
When you want to keep your Terraform state files in a central place to avoid conflicts.
When you work in a team and want everyone to use the same infrastructure data.
When you want to separate infrastructure layers, like networking and applications, but connect them.
When you want to back up your Terraform state securely in cloud storage.
Config File - main.tf
main.tf
terraform {
  backend "s3" {
    bucket = "example-terraform-state"
    key    = "network/terraform.tfstate"
    region = "us-east-1"
  }
}

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

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
  tags = {
    Name = "example-vpc"
  }
}

output "vpc_id" {
  value = aws_vpc.main.id
}


# Another Terraform configuration to use remote state

terraform {
  required_version = ">= 1.0"
}

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

data "terraform_remote_state" "network" {
  backend = "s3"
  config = {
    bucket = "example-terraform-state"
    key    = "network/terraform.tfstate"
    region = "us-east-1"
  }
}

resource "aws_subnet" "app_subnet" {
  vpc_id     = data.terraform_remote_state.network.outputs.vpc_id
  cidr_block = "10.0.1.0/24"
  tags = {
    Name = "app-subnet"
  }
}

The first part configures Terraform to store its state file in an S3 bucket under the path network/terraform.tfstate. It creates a VPC and outputs its ID.

The second part shows how another Terraform project can read that VPC ID from the remote state using terraform_remote_state data source, then create a subnet inside that VPC.

Commands
This command initializes Terraform, setting up the backend to store state remotely in S3 and downloading necessary plugins.
Terminal
terraform init
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. All Terraform commands should now work.
This command applies the Terraform configuration, creating the VPC and saving the state remotely in S3.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_vpc.main: Creating... aws_vpc.main: Creation complete after 3s [id=vpc-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skip manual approval to apply changes immediately
Initialize the second Terraform project that will use the remote state to read the VPC ID.
Terminal
terraform init
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. All Terraform commands should now work.
Apply the second Terraform configuration, which creates a subnet inside the VPC using the remote state output.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_subnet.app_subnet: Creating... aws_subnet.app_subnet: Creation complete after 2s [id=subnet-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skip manual approval to apply changes immediately
Key Concept

If you remember nothing else from this pattern, remember: Terraform remote state lets separate projects share infrastructure data safely by storing state files in a central place.

Common Mistakes
Not initializing Terraform before applying changes
Terraform needs to set up the backend and download plugins before it can work properly.
Always run 'terraform init' before 'terraform apply' or 'terraform plan'.
Using different backend configurations between projects
If backend settings like bucket or key differ, Terraform cannot find the remote state and will fail.
Ensure all projects accessing the remote state use the exact same backend configuration.
Trying to access outputs from remote state before applying the first project
The remote state file does not exist yet, so Terraform cannot read outputs.
Apply the first Terraform project to create resources and save outputs before referencing them remotely.
Summary
Configure Terraform backend to store state remotely in S3 for safe sharing.
Use 'terraform_remote_state' data source to read outputs from another Terraform project.
Run 'terraform init' before applying to set up backend and plugins.
Apply the first project to create resources and save state remotely.
Apply the second project to use remote outputs and create dependent resources.