Forum Discussion

MohamedT_Trabelsi's avatar
MohamedT_Trabelsi
Brass Contributor
Jun 10, 2022

Retrieving resource ids with Terraform remote state

So, if you want to retreive a rersource id of an Azure resource and share it between configurations when working with Terraform. 

 

To explain that, we can refer to this example : 

 

 

Here, we will deploy via two pipeline to simplify this example  : 

 

-Infrastructure

---Networking

##Create the resource group
##Create Virtual Network
##Create Subnet Workload
##Create "AzureFirewallSubnet"
---Firewall
##Create the Firewall  Policy

##Create Firewall

 

NOTE THAT, we have two different state file : 

 

When you’re using multiple folders, you simply cannot have a single state file. 
You can have a single location where you store the different state files.

 

-- One for the networking deployment 

 

 

-- Second for the firewall deployment 

 

So, the first step is to deploy the network part :  ( state file is "networking.tfstate" )

 

and outputs ,  that we will need it later 

 

 

Here is the network deployment 

 

 

 

After that, we need to create the firewall and here we need to use the RG, location and the "AzureFirewallSubnet" of the network folder. 

 

How ? 

 

NOTE :   To get the outputs of the first folder and make them available for the second folder, you can use terraform_remote_state (https://www.terraform.io/language/state/remote). The best practice is to use a data source to get the data you need, but that does require you to provide some info in the second folder. 

 

We need to add the configuration of the terraform_remote_state in the firewall.tf who is located at the folder Firewall 

 

 

Then, we can refer for the resources from the folder Netwoking with the outputs of the networking part  : 

 

- RG :  

Output from netwokring 

firewall.tf file 

- location : 

Output from netwokring 

firewall.tf file 

- Subnet Firewall : 

Output from netwokring 

firewall.tf file 

-------------*********-------------

ALL the file firewall.tf : 

 

We can see the deployment now : 

 

 

 

I tried as much as possible to simplify things  :smile: :stareyes:

 

 

No RepliesBe the first to reply

Resources