Terraform Remote State Datasource
Step-01: Introduction
- c1-versions.tf
- c2-variables.tf
- c3-locals.tf
- c4-resource-group.tf
- c5-virtual-network.tf
- c6-outputs.tf
- terraform.tfvars
# Change Directory
cd project-1-network
# Terraform Initialize
terraform init
# Terraform Validate
terraform validate
# Terraform Plan
terraform plan
# Terraform Apply
terraform apply -auto-approve
# Observation
1. Verify Resource Group
2. Verify Virtual Network
3. Verify Virtual Network Subnet
4. Verify Public IP
5. Verify Network Interface
6. Verify Storage Account - TFState file
- c0-terraform-remote-state-datasource.tf
- c1-versions.tf
- c2-variables.tf
- c3-locals.tf
- c4-linux-virtual-machine.tf
- c5-outputs.tf
- terraform.tfvars
- Understand in depth about Terraform Remote State Datasource
# Terraform Remote State Datasource
data "terraform_remote_state" "project1" {
backend = "azurerm"
config = {
resource_group_name = "terraform-storage-rg"
storage_account_name = "terraformstate201"
container_name = "tfstatefiles"
key = "network-terraform.tfstate"
}
}
/*
1. Resource Group Name
data.terraform_remote_state.project1.outputs.resource_group_name
2. Resource Group Location
data.terraform_remote_state.project1.outputs.resource_group_location
3. Network Interface ID
data.terraform_remote_state.project1.outputs.network_interface_id
*/
Step-06: Project-2: c4-linux-virtual-machine.tf
- Understand the core changes in
Virtual Machine Resource
with Terraform Remote State Datasource
# Before (Using Single Project)
resource_group_name = azurerm_resource_group.myrg.name
location = azurerm_resource_group.myrg.location
network_interface_ids = [azurerm_network_interface.myvmnic.id]
# After (Using Two Projects and with Terraform Remote State Datasource)
# Getting Data using Terraform Remote State Datasource from Project-1
resource_group_name = data.terraform_remote_state.project1.outputs.resource_group_name
location = data.terraform_remote_state.project1.outputs.resource_group_location
network_interface_ids = [data.terraform_remote_state.project1.outputs.network_interface_id]
# Change Directory
cd project-2-app1
# Terraform Initialize
terraform init
# Terraform Validate
terraform validate
# Terraform Plan
terraform plan
# Terraform Apply
terraform apply -auto-approve
# Observation
1. Verify Resource Group
2. Verify Virtual Network
3. Verify Virtual Network Subnet
4. Verify Public IP
5. Verify Network Interface
6. Verify Virtual Machine Resource (Location it created, Network Interface it used)
7. Verify Storage Account - TFState file
Step-08: Project-2: Clean-Up
# Change Directory
cd project-2-app1
# Destroy Resources
terraform destroy -auto-approve
# Delete Files
rm -rf .terraform*
Step-09: Project-1: Clean-Up
# Change Directory
cd project-1-network
# Destroy Resources
terraform destroy -auto-approve
# Delete Files
rm -rf .terraform*