0
0
Terraformcloud~5 mins

Remote state data source for cross-project in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you manage infrastructure across multiple projects, you often need to share information between them. Remote state data source lets one Terraform project read the state data from another project safely and reliably.
When you want to use outputs from one Terraform project as inputs in another project.
When you manage infrastructure in separate projects but need to connect resources between them.
When you want to avoid duplicating resource definitions by referencing existing infrastructure state.
When you want to keep Terraform states isolated but still share data securely.
When you want to automate infrastructure deployment that depends on resources created elsewhere.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.3.0"
}

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

# Data source to read remote state from another project stored in S3

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

resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  # Use subnet ID from remote state output
  subnet_id = data.terraform_remote_state.network.outputs.public_subnet_id

  tags = {
    Name = "web-instance"
  }
}

This Terraform file shows how to use the terraform_remote_state data source to read outputs from a remote state stored in an S3 bucket.

The data "terraform_remote_state" "network" block configures where to find the remote state file.

The aws_instance resource uses the subnet ID output from the remote state to place the instance in the correct subnet.

Commands
Initializes the Terraform working directory and downloads necessary providers and modules.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.54.0... - Installed hashicorp/aws v4.54.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. All Terraform commands should now work.
Shows the planned actions Terraform will take, including reading remote state outputs to configure resources.
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_instance.web will be created + resource "aws_instance" "web" { + ami = "ami-0c55b159cbfafe1f0" + arn = (known after apply) + instance_type = "t2.micro" + subnet_id = "subnet-0abcd1234ef567890" + tags = { + "Name" = "web-instance" } } Plan: 1 to add, 0 to change, 0 to destroy.
Applies the planned changes to create the AWS instance using the subnet from the remote state.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.web: Creating... aws_instance.web: Still creating... [10s elapsed] aws_instance.web: Creation complete after 15s [id=i-0a1b2c3d4e5f67890] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approves the apply without prompting for confirmation.
Displays the outputs of the current Terraform state, useful to verify values after apply.
Terminal
terraform output
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: terraform_remote_state lets one project safely read outputs from another project's state to share data across projects.

Common Mistakes
Using incorrect backend configuration details like wrong bucket name or key path.
Terraform cannot find the remote state file and fails to read outputs, causing errors.
Double-check the backend config values exactly match where the remote state is stored.
Trying to access outputs that do not exist in the remote state.
Terraform throws an error because the output key is missing in the remote state file.
Verify the remote project exports the outputs you want to use and spell them correctly.
Not running terraform init after adding the terraform_remote_state data source.
Terraform does not download or configure the backend properly, causing failures.
Always run terraform init to initialize the working directory after changing backend or data sources.
Summary
Configure terraform_remote_state data source with correct backend details to read another project's state.
Use outputs from the remote state as inputs to resources in the current project.
Run terraform init, plan, and apply to deploy resources using shared state data.