This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021
4. Create modules
Creating modules in Terraform is very easy; All we need is a standard configuration of input variables and resources. In this example, we will create our first module for storage accounts.
We will first create the module folder and then reference the module in another Terraform configuration. We’ll start with a folder hierarchy with the following:
Copy the code for the main.tf and variable.tf configurations and create each file. We will place each file according to the directory structure above.
The variable. tf file contains our input Variables. Input variables are the parameters that our module accepts to customize its deployment. For our storage account module, we can keep it this simple by receiving input for the storage account name, location, and resource group: variable.tf
variable "saname" {
type = string
description = "Name of storage account"
}
variable "rgname" {
type = string
description = "Name of resource group"
}
variable "location" {
type = string
description = "Azure location of storage account environment"
default = "westus2"
}
Copy the code
The main.tf file contains the code to create a storage account. In this example, we only have a small set of parameters for our storage account to keep things simple.
However, in a real production environment, we might want to implement network policies as well as logging options. We can then use our module to define “criteria” to configure all storage accounts in order to configure all storage accounts: main.tf
resource "azurerm_storage_account" "sa" {
name = var.saname
resource_group_name = var.rgname
location = var.location
account_tier = "Standard"
account_replication_type = "GRS"
}
Copy the code
Next, we will create another main.tf file in the root of the TerraformDemo folder that will reference our newly created storage account module directory:
In the main.tf file on the root module, we call the submodule. Inside the block, we need to refer to the module we are using by declaring source parameters. In this example, we reference the module only from the module subfolder, so the path is./modules/ storage-account.
We also need to provide any variable input required for your storage account module. These are the same variables we created in the variablible.tf file in the directory storing the accounts module:
The main root module. Tf
Provider "azurerm" {version = "1.38.0"} #create resource group resource "azurerM_resource_group" "rg" {name = "rg-MyFirstTerraform" location = "westus" } #Create Storage Account module "storage_account" { source = "./modules/storage-account" saname = "statfdemosa234" rgname = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location }Copy the code
In our main.tf file, we also include the Azurerm provider block. It is best to specify the provider in the root module file; This way, all modules that are invoked will inherit from the provider. When creating a module, try not to include as many providers as possible within the module itself. This can lead to further complexity and make the module brittle.
Boy, haven’t you seen enough? Click on the stone’s home page and take a look at it casually. Maybe there will be a surprise? Welcome to support the likes/attention/comments, your support is my biggest motivation, thank you!