Trending
Study links sugar-sweetened beverages, fruit juice consumption to high blood pressure is trending now The Silent Pandemic Killing 3 People Every Minute is trending now Lassa fever: 237 dead as fatality rate rises to 23.7% is trending now Black holes older than the Big Bang could explain dark matter is trending now Sudan: Heavy Meteor Showers to Appear in the Atmosphere Tuesday Evening for Three Days is trending now Carabao Cup second-round draw: Chelsea vs Luton, Tottenham vs Charlton, Newcastle vs West… is trending now Donald Trump says replacing Gianni Infantino would be a ‘terrible mistake’ is trending now 'Enjoy it' - Ronald Araujo reveals Luis Suarez’s advice after joining Liverpool on loan is trending now Despite defeat to Cameroon, World Cup remains our target - Super Falcons' Christy Ucheibe is trending now BBNaija S11: 16 housemates up for possible eviction in week three is trending now From tears to thanksgiving, Actress Mo Bimpe celebrates triplets ahead of grand thanksgiv… is trending now Apply Now: Jonathan Larson Grant Program (US) is trending now Study links sugar-sweetened beverages, fruit juice consumption to high blood pressure is trending now The Silent Pandemic Killing 3 People Every Minute is trending now Lassa fever: 237 dead as fatality rate rises to 23.7% is trending now Black holes older than the Big Bang could explain dark matter is trending now Sudan: Heavy Meteor Showers to Appear in the Atmosphere Tuesday Evening for Three Days is trending now Carabao Cup second-round draw: Chelsea vs Luton, Tottenham vs Charlton, Newcastle vs West… is trending now Donald Trump says replacing Gianni Infantino would be a ‘terrible mistake’ is trending now 'Enjoy it' - Ronald Araujo reveals Luis Suarez’s advice after joining Liverpool on loan is trending now Despite defeat to Cameroon, World Cup remains our target - Super Falcons' Christy Ucheibe is trending now BBNaija S11: 16 housemates up for possible eviction in week three is trending now From tears to thanksgiving, Actress Mo Bimpe celebrates triplets ahead of grand thanksgiv… is trending now Apply Now: Jonathan Larson Grant Program (US) is trending now
Automation

How to use terraform modules?

How to use terraform modules? The post How to use terraform modules? appeared first on The Automation Code.

Why terraform module is required?

If you are new to terraform, I will advise you to first check the terraform basics.

Alright, you might have worked with multiple programming languages where you want to reuse your code and you write functions.

Similarly in Terraform, we create modules so that the same resource block can be called and used multiple times by just passing values.

For example, consider this scenario where you have to create a storage account for two environments (QA and Prod). In a typical environment what you have done without a module is create two resources and pass variables separately.

For example, Prod Storage Account main.tf file. (To reduce the article length, variable values are directly passed to the main.tf file).

resource "azurerm_storage_account" "prodStorageAccount" { name = "prodStorageAccount" resource_group_name = "prodStorageRG" location = "EastUS" account_tier = "Standard" account_replication_type = "ZRS" }

QA main.tf file

resource "azurerm_storage_account" "qaStorageAccount" { name = "qaStorageAccount" resource_group_name = "qaStorageRG" location = "WestUS" account_tier = "Standard" account_replication_type = "LRS" }

From the above example, instead of creating a resource block 2 times for a storage account, we can create a reusable block using the terraform module and call it as many times for the different environments.

The diagram for the Module is shown below.

The syntax for the Module:

module ModuleName { source = "./modules/name" param = value } You need to specify the module keyword with the module name. Source – Path (local, relative, or remote) where the module is defined. param – Values to be passed to variables.

Let’s create a storage account module as per the above diagram and call them in both environments main.tf file.

Storage Account module main.tf.

resource "azurerm_storage_account" "prodStorageAccount" { name = var.storageAccountName resource_group_name = var.storageResourceGroup location = var.storageLocation account_tier = var.storageTier account_replication_type = var.storageReplicationType }

Module: variables.tf

variable "storageAccountName" { type = string} variable "storageResourceGroup" {type = string} variable "storageLocation" {type = string} variable "storageTier" {type = string} variable "storageReplicationType" {type = string}

Once the base resource block is ready, we are ready to call it in the module as shown below.

Prod Environment: main.tf

module Create-StorageAccount { source = "../Modules/StorageAccount" storageAccountName = "prodStorageAccount" storageResourceGroup = "prodStorageRG" storageLocation = "EastUS" storageTier = "Standard" storageReplicationType = "ZRS" }

QA Environment: main.tf

module Create-StorageAccount { source = "../Modules/StorageAccount" storageAccountName = "qaStorageAccount" storageResourceGroup = "qaStorageRG" storageLocation = "WestUS" storageTier = "Standard" storageReplicationType = "LRS" }

If you want to spin up any new environment, you just have to call the module and provide the source path of the module and parameters.

This is how the module hierarchy will look like,

If you want to create a new module for another resource then create a resource folder name the same as the storage account. For example,

The below example shows, how you can accommodate multiple modules in the main.tf file.

Prod: main.tf

module Create-Resourcegroup { source = "../../Modules/ResourceGroup" resourceGroupName = "prodStorageRG" rgLocation = "EastUS" } module Create-StorageAccount { source = "../../Modules/StorageAccount" storageAccountName = "prodStorageAccount" storageResourceGroup = "prodStorageRG" storageLocation = "EastUS" storageTier = "Standard" storageReplicationType = "ZRS" depends_on = [ module.Create-ResourceGroup ] }

QA: main.tf

module Create-Resourcegroup { source = "../../Modules/ResourceGroup" resourceGroupName = "qaStorageAccount" rgLocation = "WestUS" } module Create-StorageAccount { source = "../../Modules/StorageAccount" storageAccountName = "qaStorageAccount" storageResourceGroup = "qaStorageRG" storageLocation = "WestUS" storageTier = "Standard" storageReplicationType = "LRS" depends_on = [ module.Create-ResourceGroup ] }

Once you run the terraform init command, terraform will initialize the specified module.

You can get the above example code from the GitHub repo.

https://github.com/chiragce17/MyPublicRepo/tree/main/Terraform/Module-Intro

Loading

The post How to use terraform modules? appeared first on The Automation Code.
View original source →

Related

More from The Automation Code