Retrieving resource ids with Terraform remote state

Occasional Contributor

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  : 




##Create the resource group
##Create Virtual Network
##Create Subnet Workload
##Create "AzureFirewallSubnet"
##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 ( 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 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 

MohamedT_Trabelsi_2-1654886847002.png file 


- location : 

Output from netwokring 

MohamedT_Trabelsi_3-1654886883262.png file 


- Subnet Firewall : 

Output from netwokring 

MohamedT_Trabelsi_4-1654886961938.png file 



ALL the file : 



We can see the deployment now : 





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



0 Replies