While working on Azure Terraform IAC infrastructure as a code script Writing and Using Terraform modules, I wanted to pass or use variable from a module to another module, so I will have to define module data once and I can keep reusing it in another modules. Here is the screenshot of my folder hierarchy where all the tf files codes are kept. I have two modules one is to create Resource Group and another to create Storage Account. Now here storage account requires resource group to be mention while creating it. Using two modules I didn't want to mention the resource group name in another module manually, and wanted to pickup or pass the module Resource Group variable value into module Storage Account.
Here is the tf files inside module Resource Group folder, to use variable name of RG you can use output in seperate output.tf. I will use this output value in Storage Account resource module.
Terraform output values allow you to export structured data about your resources. You can use this data to configure other parts of your infrastructure with automation tools, or as a data source for another Terraform workspace. Outputs are also necessary to share data from a child module to your root module.
#Location: .\Modules\Resource_Group #Resource group module main.tf file resource "azurerm_resource_group" "example_rg" { name = var.name location = var.location tags = var.tags } #Resource group module ouput.tf file output "RG_name" { value = var.name } #Resource group module variable.tf file variable "name" { description = "The name of the module demo resource group in which the resources will be created" type = string default = "example_module_rg" } variable "location" { description = "The location where module demo resource group will be created" type = string default = "East Us" } variable "tags" { description = "A map of the tags to use for the module demo resources that are deployed" type = map(string) default = { environment = "Example" Owner = "vcloud-lab.com" } }
This is the script code inside Storage Account module folder, I have created normal module with main.tf and variable.tf files to create Storage Account.
#Location: .\Modules\Storage_Account #Storage Account module main.tf file resource "azurerm_storage_account" "example_sa_name" { name = var.name resource_group_name = var.resource_group_name location = var.location account_tier = var.account_tier account_replication_type = var.account_replication_type tags = var.tags } #Storage Account module variable.tf file variable "name" { description = "The name of the module demo resource group in which the resources will be created" type = string default = "example_module_sa" } variable "resource_group_name" { description = "The name of the module demo resource group in which the resources will be created" type = string default = "example_module_rg" } variable "location" { description = "The name of the module demo resource group in which the resources will be created" type = string default = "East Us" } variable "account_tier" { description = "The location where module demo resource group will be created" type = string default = "Standard" } variable "account_replication_type" { description = "The location where module demo resource group will be created" type = string default = "LRS" } variable "tags" { description = "A map of the tags to use for the module demo resources that are deployed" type = map(string) default = { environment = "Example" Owner = "vcloud-lab.com" } }
This is the text code script inside root main.tf file. Note down the reference inside SA terraform module I am using to get the Resource Group name value.
Download this script bundle here or it is also available on github.com.
#Location: .\ #Root main.tf file # Configure the Microsoft Azure Provider provider "azurerm" { features {} } module "Demo_Azure_Module_RG" { source = "./Modules/Resource_Group" name = "demo_RG" location = "West US" tags = { environment = "DemoRG" Owner = "http://vcloud-lab.com" } } module "Demo_Azure_Module_SA" { source = "./Modules/Storage_Account" name = "examplesa001" resource_group_name = module.Demo_Azure_Module_RG.RG_name location = "West US" account_tier = "Standard" account_replication_type = "LRS" tags = { environment = "DemoSA" Owner = "http://vcloud-lab.com" } depends_on = [ module.Demo_Azure_Module_RG ] }
I will initiate terraform tf file modules and script folder, this all looks good.
terraform plan will generate the execution plan what will be created and added.
If there are no errors in the plan, apply it with terraform apply -auto-approve. Here in the bottom 2 items where resources are getting created.
Useful Articles
Create an Azure App registrations in Azure Active Directory using PowerShell & AzureCLI
Get started and configure with certificate-based authentication in Azure
Create a Virtual machine on Microsoft Azure
PowerShell List All Azure Resverations
Powershell get the list of Azure Reservations Virtual Machines instances
Get the list Azure Reservation Catalog with PowerShell and AzureCLI
Azure automation account DSC for On-Premise Virtual Machine on boarding
Azure Powershell : Operation returned an invalid status code 'BadRequest'
Get Azure virtual machine backup reports using Powershell