Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Terraform Using one module variable in another module

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.

Terraform pass module variable to another module tree main.tf output.tf variable.tf azure provider local provisioner automation devops azure resources data.png

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"
    }
}

Terraform main.tf output.tf variable.tf module variable pass variable data resource from one module to another resource group storage account microsoft azure portal automation devops location tags azurerm_resource_group.png

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"
    }
}

Terraform main.tf variable.tf storage account resource group azure microsoft portal pass variable from one module to another module pass module resource to another resource data account tier replication type tags location.png

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. 

#Location: .\Modules\Storage_Account
#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
  ]  
}

Terraform Microsoft Azure provider azurerm Module resource group location tags source storage account module pass variable to another module account tier replication type depends_on.png

I will initiate terraform tf file modules and script folder, this all looks good.

Terrraform Init Azure portal how to use module resource group storage account hashicorp azurerm provider lck lock file initialization main.tf pass module variable from one module to another.png

terraform plan will generate the execution plan what will be created and added.

Terraform Plan Powershell azure storage account resource group pass one module variable to another module variable path allowed headers share properties smb routing table api.png\

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.

Microsoft Azure portal terraform apply --auto-apply destroy execution plan resource group pass module variable to another module.png

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

Go Back

Comment

Blog Search

Page Views

5884714

Follow me on Blogarama