Terraform Azure - Creating And Managing The Infrastructure Resources

Terraform Azure

The previous article, What is Terraform? discussed Infrastructure as Code and how Terraform enabled it. In this article, we'll learn to code the Azure Infrastructure using Terraform minimizing the requirement of tedious process and hassle of having to configure manually substitution with automating through code like configuration file through HasiCorp Configuration Language (HCL). We’ll then go through a hands-on process to create the main.tf file with the configuration to setup the infrastructure resources in Azure.  

Visual Studio Code

Visual Studio Code can be understood as a code editor with an integrated development environment that is optimized and refined in order to support the process of building and debugging applications for the modern web and cloud. It was made by Microsoft for operating systems ranging from Windows, macOS, and Linux and supports numerous features such as intelligent code completion, syntax highlighting code refactoring, and many more. 

The Terraform Language  

The main purpose of the Terraform language is to declare resources that represent infrastructure objects. Multitudes of resources from SQL Server to Virtual Machines and Network and the resource groups themselves represented as a resource which is an infrastructure object can be declared using the Terraform language also known as the Terraform’s primary user interface. Let us explore the basic syntax of the Terraform language.  

The Basic Syntax 

The resource block is the primary declarative statement that is used. Moreover, different blocks such as data blocks are also used. The Block Label starts with the prefix of the cloud provider followed by the name of the type of resource to be used. Following that would be the Block Label which represents the name given to the resource which will be used to identify it though out the other terraform modules in the Terraform configuration file. Here, it is “rg” which could be “test” or “demo” resource groups which is similar to the variable name.  Next, we have declarative key-value pairs of Identifier and Expression that are known as arguments. The minimum arguments that should be represented are the name of the resource group and the location where this resource group is to be deployed. This now represents a complete terraform block which is the basic unit of the Terraform language.  

What does tf mean? 

The codes for the configuration in the Terraform language are stored in basically two ways. One method is storing in plain text files with the file extension .tf which we can say abbreviates as TerraForm file to understand. Moreover, it can also be stored with the JSON format through the .tf.JSON extension.  

Moreover, when these files of .tf and .tf.JSON are stored together in one particular directory, we call it a module.  

Set up Terraform Config Extension in VS Code 

For this, go to the extension section of Press Ctrl + Shift + X.

Then Search for Terraform in the search bar. 

Install the Terraform

Now, as we can see, with the Terraform extension, VS Code will now recognize the .tf and .tf.json files and will make it easier for us to code with the features of Syntax highlighting Syntax Validation, and more.  

Now, let us learn to create a Terraform configuration file.   

Let us create a new file and name it main.tf.

As we learned above about the structure of Terraform configuration, we’ll now write the config for Azure to create resources of resource groups and virtual networks following the guidelines.  

terraform{
    required_providers {
      azurerm = {
          source = "hashicorp/azurerm"
          version = "2.81.0"
      }
    }
}
provider "azurerm"{
    features{

    }
}
variable "prefix" {
  type        = string
  default     = "demo"
  description = "description"
}
resource "azurerm_resource_group" "demo-rg"{
    name = "demo-rg"
    location = "West Europe"
}
resource "azurerm_virtual_network" "demo-vn"{
    name = "$demo-vn"
    azurerm_resource_group = azurerm_resource_group.demo_rg.name
    location = azurerm_resource_group.demo_rg.location
    address_space = ["10.0.0.0/16"]
}

Now, let us declare the variable prefix to make our config more dynamic and robust.  

variable "prefix" {
  type        = string
  default     = "demo"
  description = "description"
}

With the new change of declaration of a variable, we’ll not need to repeat the term demo for our resources with the new config representing something like this.

terraform{
    required_providers {
      azurerm = {
          source = "hashicorp/azurerm"
          version = "2.81.0"
      }
    }
}
provider "azurerm"{
    features{

    }
}
variable "prefix" {
  type        = string
  default     = "demo"
  description = "description"
}
resource "azurerm_resource_group" "demo-rg"{
    name = "${var.prefix}-rg"
    location = "West Europe"
}
resource "azurerm_virtual_network" "demo-vn"{
    name = "${var.prefix}-vn"
    azurerm_resource_group = azurerm_resource_group.demo_rg.name
    location = azurerm_resource_group.demo_rg.location
    address_space = ["10.0.0.0/16"]
}

This is a high-level demo where we declared and defined resources such as resource groups and virtual networks.  

To learn more about Terraform and coding the Azure infrastructure using Terraform, watch this video.

Deployment

Open the Powershell in VS Code and type in the commands. 

terraform init 

This initializes the terraform in the directory.  

terraform init 

Commands in Terraform
 

terraform fmt 

The terraform fmt commands make sure the Directory structure is formatted properly. It is extremely useful for multilevel directories formatting.  

terraform fmt 

terraform validate 

This command allows us to validate our configuration.  

terraform validate 

terraform plan 

The plan command is used to create the execution plan which reads the present state of the existing remote objects to make sure that the state of Terraform is up-to-date and then compares the current configuration to the previous state and makes sure if there are any changes. Moreover, a set of action changes are proposed if required.

terraform plan 

Now, the resource groups have been created. Let us check our Azure Portal and see if they are created. 

As we can see above. The demo-rg resource has been created with the demo-vn virtual network. This shows how easily resources can be created and planned through the Terraform. 

Conclusion

In this article, we learned about creating and managing the resources in Azure through Terraform. We learned basics about the Terraform language, its basic syntax and then went through a hands-on tutorial experience to create a terraform configuration file. We created the azure resources group demo-rg and virtual network demo-vn in Azure. Later, we learned to deploy these through PowerShell using the Terraform commands. Besides, we also learned about various commands in terraform that would be useful in the future. 


Similar Articles