NashTech Blog

Table of Contents
blur business close up code

Getting started with the Terraform modules in Azure as we all know Infrastructure as a Service (IaaS) is the important and main service for any application to build.

Creating Terraform Modules in Azure

As, we use our old traditional way to create terraform module with the Hierarchy :

terraformexample
  |_____module
                 |_____storage-account
                               |_______main.tf
                               |_______variable.tf

Then we will define the terraform files as per the standards:

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

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

Now, we will create another main.tf inside the terraformexample and implement the module resource block.

terraformdemo
    └── main.tf
    └──modules 
            └──storage-account
                    └── main.tf
                    └── variables.tf

We will define the new main.tf with the module block :

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
}

Now, we will use the terraform commands:

terraform init

Initializing modules...
- storage_account in modules/storage-account

Initializing the backend...

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "azurerm" (hashicorp/azurerm) 1.38.0...

As the initialize command will load all the dependencies and plugins to run the terraform module and create the resource in the Azure environment in the

cloud itself. Although terraform registry provides a vast amount of modules that can be used to configure the Azure Cloud resources.

Conclusion

In the last, it will add all the dependencies in the module and install them to run the module. So, In this way we can use the terraform scripts or piece of code for the azure cloud environments.

Hope this blog found the good amount of information to all the viewers. Please follow for more such insightfull and amazing blogs, click here.

Picture of Vikas Vashisth

Vikas Vashisth

Vikas Vashisth is working as a Sr.DevOps Engineer at Knoldus | Part of Nashtech with having more than multiple years of experience in the field of continuous integration and delivery, infrastructure automation, and containerization enables me to build scalable, reliable, and highly available environments. I am proficient in tools such as Docker, Kubernetes, Jenkins, Ansible, and Terraform, and have experience working with cloud platforms such as AWS, GCP, and Azure.

Leave a Comment

Suggested Article

Discover more from NashTech Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading