0
0
Terraformcloud~10 mins

Remote state data source for cross-project in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Remote state data source for cross-project
Start Terraform config
Define remote state backend
Terraform stores state remotely
In another project, define data source
Terraform reads remote state data
Use remote state outputs in current config
Apply configuration with cross-project data
Terraform stores state remotely in one project and reads it from another using a remote state data source to share outputs.
Execution Sample
Terraform
data "terraform_remote_state" "network" {
  backend = "gcs"
  config = {
    bucket = "network-state-bucket"
    prefix = "terraform/state"
  }
}

output "network_vpc_id" {
  value = data.terraform_remote_state.network.outputs.vpc_id
}
This code reads the remote state from a Google Cloud Storage bucket in another project and outputs the VPC ID.
Process Table
StepActionEvaluationResult
1Terraform reads local configDetects remote state data sourcePrepares to fetch remote state
2Connect to GCS bucket 'network-state-bucket'Access remote state file at 'terraform/state'Remote state file found and read
3Parse remote state JSONExtract outputs.vpc_idvpc_id value obtained (e.g., 'vpc-12345')
4Assign output 'network_vpc_id'Set to remote vpc_id valueOutput ready for use in current project
5Apply configurationUse output value in resourcesResources configured with remote VPC ID
6EndNo errorsCross-project remote state data successfully used
💡 Remote state data source successfully fetched and used for cross-project reference
Status Tracker
VariableStartAfter Step 3Final
data.terraform_remote_state.network.outputs.vpc_idundefinedvpc-12345vpc-12345
output.network_vpc_idundefinedundefinedvpc-12345
Key Moments - 3 Insights
Why do we need to specify the backend and config in the remote state data source?
Because Terraform needs to know where and how to access the remote state file. The backend and config tell it the storage location and path, as shown in execution_table step 2.
What happens if the remote state file is missing or inaccessible?
Terraform will fail to read the remote state and throw an error during planning or applying, stopping the process before step 4 in the execution_table.
Can outputs from the remote state be used directly in resource definitions?
Yes, once Terraform reads the remote outputs (step 3), they can be referenced in resource configurations as shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does Terraform obtain the remote output value?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Evaluation' column for when outputs.vpc_id is extracted.
According to the variable tracker, what is the value of 'output.network_vpc_id' after step 3?
Avpc-12345
Bnull
Cundefined
Dempty string
💡 Hint
Look at the 'After Step 3' column for 'output.network_vpc_id' in variable_tracker.
If the GCS bucket name in the config is changed, which step in the execution table would fail?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Step 2 involves connecting to the GCS bucket; changing the bucket name affects this step.
Concept Snapshot
Terraform remote state data source:
- Use 'terraform_remote_state' data block
- Specify backend and config to locate remote state
- Access outputs from remote state
- Enables sharing data across projects
- Must have read access to remote state storage
Full Transcript
This visual execution shows how Terraform uses a remote state data source to read outputs from another project's state file stored remotely, such as in a Google Cloud Storage bucket. The flow starts with Terraform reading the local config, then connecting to the remote backend to fetch the state file. It parses the state to extract outputs like VPC ID, assigns these to local outputs, and uses them in resource definitions. Variables track the output value as it becomes available. Key moments clarify why backend config is needed, what happens if remote state is missing, and how outputs are used. The quiz tests understanding of when values are obtained and what happens if config changes.