0
0
Terraformcloud~30 mins

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

Choose your learning style9 modes available
Using Remote State Data Source in Terraform
📖 Scenario: You are managing infrastructure with Terraform. You have one Terraform configuration that creates a virtual network. Another Terraform configuration needs to use the network's ID from the first configuration. To do this, you will use Terraform's remote state data source to access the network ID.
🎯 Goal: Build a Terraform configuration that reads the virtual network ID from a remote state stored in an S3 bucket and uses it to create a subnet.
📋 What You'll Learn
Create a Terraform backend configuration for remote state stored in an S3 bucket
Define a data source to read the remote state
Extract the virtual network ID from the remote state
Use the extracted network ID to create a subnet resource
💡 Why This Matters
🌍 Real World
In real projects, infrastructure is often split into multiple Terraform configurations. Using remote state data sources allows sharing information like network IDs between these configurations safely and cleanly.
💼 Career
Understanding remote state data sources is essential for cloud engineers and DevOps professionals to manage complex infrastructure deployments across teams and environments.
Progress0 / 4 steps
1
Configure Terraform backend for remote state
Write a terraform block to configure the backend to use an S3 bucket named my-terraform-state in region us-west-2 with key subnet/terraform.tfstate.
Terraform
Need a hint?

The terraform block configures where Terraform stores its state remotely. Use the backend "s3" block with the exact bucket, key, and region values.

2
Define remote state data source
Add a data block named remote_network of type terraform_remote_state that points to the same S3 bucket my-terraform-state, key network/terraform.tfstate, and region us-west-2.
Terraform
Need a hint?

The terraform_remote_state data source reads state from a remote backend. Use the same bucket, key, and region as the backend configuration.

3
Extract virtual network ID from remote state
Create a locals block with a value called vnet_id assigned the value of data.terraform_remote_state.remote_network.outputs.vnet_id to get the virtual network ID from the remote state outputs.
Terraform
Need a hint?

Use a locals block with the exact name vnet_id and assign the remote state output value to it.

4
Create subnet resource using remote network ID
Add a resource block named azurerm_subnet called example_subnet that uses local.vnet_id as the virtual_network_id and has the name example-subnet with address prefix 10.0.1.0/24.
Terraform
Need a hint?

Use the azurerm_subnet resource with the exact name example_subnet. Set virtual_network_id to local.vnet_id and provide the required name and address prefix.