0
0
Terraformcloud~30 mins

Remote state data source for cross-project in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Remote state data source for cross-project
📖 Scenario: You are managing infrastructure for two separate projects in Terraform. Project A creates a network, and Project B needs to use the network's ID from Project A to create a virtual machine inside that network.
🎯 Goal: Learn how to use Terraform's remote state data source to access outputs from one project in another project.
📋 What You'll Learn
Create a Terraform backend configuration for Project A's state
Output the network ID from Project A
Configure Project B to read Project A's remote state
Use the network ID from Project A in Project B's resource
💡 Why This Matters
🌍 Real World
Many organizations split infrastructure into multiple Terraform projects for separation of concerns. Sharing outputs via remote state allows projects to connect resources safely.
💼 Career
Understanding remote state data sources is essential for Terraform users working in teams or managing complex infrastructure across multiple projects.
Progress0 / 4 steps
1
Create Project A's network and output
In Project A's Terraform configuration, create a resource called aws_vpc named main_network with cidr_block set to "10.0.0.0/16". Then create an output called network_id that outputs aws_vpc.main_network.id.
Terraform
Need a hint?

Use resource "aws_vpc" "main_network" to create the network and output "network_id" to expose its ID.

2
Configure backend for Project A state storage
Add a terraform block with a backend configuration using s3 backend. Set bucket to "project-a-terraform-state", key to "network/terraform.tfstate", and region to "us-east-1".
Terraform
Need a hint?

Use the terraform block with backend "s3" and set the bucket, key, and region exactly as given.

3
Configure Project B to read Project A's remote state
In Project B's Terraform configuration, create a data block of type terraform_remote_state named project_a. Set the backend to s3 with bucket as "project-a-terraform-state", key as "network/terraform.tfstate", and region as "us-east-1".
Terraform
Need a hint?

Use data "terraform_remote_state" "project_a" with backend = "s3" and the correct config values.

4
Use Project A's network ID in Project B resource
Create a resource aws_instance named vm in Project B. Set ami to "ami-12345678", instance_type to "t2.micro", and subnet_id to data.terraform_remote_state.project_a.outputs.network_id.
Terraform
Need a hint?

Create resource "aws_instance" "vm" and set subnet_id to the remote state output.