An Introduction To Terraform For Beginners

Infrastructure as Code (IaC) is a widely used terminology in DevOps. It is a process to manage and provision the complete IT infrastructure using configuration files. these configuration files can be interpreted by terraform. It helps in automating the complete infrastructure by using a declarative programming language.
 

What is Terraform?

 
Terraform is an open-source infrastructure as a Code tool developed by HashiCorp.
 
It helps to automate the infrastructure creation process whether in Azure, AWS, or google cloud by using relative providers for e.g for "azurerm" for AZURE. you can leverage Terraform to automate the infrastructure and take more control over the complete infrastructure management via code.
 

Terraform Lifecycle

 
Terraform lifecycle consists below commands.
  • “init” command => initializes the working directory which consists of all the required component those are required to provide the infrastructure.
  • "plan" command=> It is used to create an execution plan to achieve the desired state of the infrastructure. it also detects the changes in the configuration files whether something is changed(added/deleted) in order to achieve the desired state.
  • "apply" command=> It makes the changes as defined in the execution plan.
  • "destroy."=> It is used to delete all the old resources, those are marked as created after the application phase.

Configuration Overview

 
Once we have gone through the commands, it is always a good idea to know how to write configuration files. We will create one simple configuration file here as an example and provision a webapp resource in Azure.
 
There is a declarative configuration language called HCL, which allows us to write a description of infrastructure requirements. configuration file has .tf extension. It may also be written in JSON format having the extension .tf.JSON.
 
Configuration files can be a group of modules. Now the question arises, what is a module?
 
Well, let’s try to explain this concept in simple words. When you set up an infrastructure, the most important part is to declare resources. now the question comes what is a resource? a resource is an element concerning the infrastructure, like resource groups, virtual networks, databases, etc. Now, when such resources are grouped and kept together in a relationship, they form a module. Basically, we keep all the related files in a windows directory in the same way you can keep the related resources in a module.
 
One important point, there are many predefined reusable modules are given by HashiCorp those can be reused in different requirement.
 
A configuration file always contains at least a root module, where the execution always begins. This root module may or may not call other child modules.
Use the link https://www.terraform.io/downloads.html to download Terraform. Download the .zip file and place the terraform.exe file at your desired location. You can configure the location of the terraform.exe file in environment paths so that it can be accessed from anywhere.
 
Let's go through the below configuration file “main.tf” which is responsible to create a resource group and webapp.
 
The configuration contains different sections, as described below.
 

Providers

 
Terraform relies on plugins called "providers" to interact with remote systems.
 
Below example shows the "azurerm" for Azure.
  1. ################## Providers ########################################  
  2. provider "azurerm" {  
  3.   version = "=2.36.0"  
  4.   features {}  
  5. }   

Variables

 
The Terraform language includes a few kinds of blocks for requesting or publishing named values.
  • Input Variables serve as parameters for a Terraform module, so users can customize behavior without editing the source.
  • Output Values are like return values for a Terraform module.
  • Local Values are a convenience feature for assigning a short name to an expression.
  1. ################## Variables ########################################  
  2. variable "resource_group_name" {  
  3.   type        = string  
  4.   description = "RG name in Azure"  
  5.   default     = "my_terraform_rg"  
  6. }  
  7.   
  8. variable "resource_group_location" {  
  9.   type        = string  
  10.   description = "RG location in Azure"  
  11.   default     = "centralindia"  
  12. }  
  13.   
  14. variable "app_service_plan_name" {  
  15.   type        = string  
  16.   description = "App Service Plan name in Azure"  
  17.   default     = "my-appserviceplan"  
  18. }  
  19.   
  20. variable "app_service_name" {  
  21.   type        = string  
  22.   description = "App Service name in Azure"  
  23.   default     = "terraform-homedemo-010"  
  24. }  

Resources

 
Resources are the most important element in the Terraform language. Each resource block describes one or more infrastructure objects, such as virtual networks and compute instances.
  1. ################## Resources ########################################  
  2. resource "azurerm_resource_group" "rg" {  
  3.   name     = var.resource_group_name  
  4.   location = var.resource_group_location  
  5.   
  6.   tags = {  
  7.     environment = "development"  
  8.   }  
  9. }  
  10.   
  11. resource "azurerm_app_service_plan" "app_plan" {  
  12.   name                = var.app_service_plan_name  
  13.   location            = azurerm_resource_group.rg.location  
  14.   resource_group_name = azurerm_resource_group.rg.name  
  15.   #kind                = "Windows"  
  16.   
  17.   sku {  
  18.     tier = "Standard"  
  19.     size = "S1"  
  20.   
  21.   }  
  22.   tags = {  
  23.     environment = "development"  
  24.   }  
  25. }  
  26.   
  27. resource "azurerm_app_service" "webapp" {  
  28.   name                = var.app_service_name  
  29.   location            = azurerm_resource_group.rg.location  
  30.   resource_group_name = azurerm_resource_group.rg.name  
  31.   app_service_plan_id = azurerm_app_service_plan.app_plan.id  
  32.   
  33.   site_config {  
  34.     dotnet_framework_version = "v4.0"  
  35.     scm_type                 = "LocalGit"  
  36.     default_documents = [  
  37.       "hostingstart.html"  
  38.     ]  
  39.   }  
  40.   
  41.   app_settings = {  
  42.     "SOME_KEY" = "some-value"  
  43.   }  
  44.   
  45.   tags = {  
  46.     environment = "development"  
  47.   }  
  48. }  
Run above Terraform commands in the below sequence.
  • Terraform version -> to check the version of terraform.

    Introduction to Terraform
  • terraform init
Introduction to Terraform
  • terraform plan -out main.tfplan
Introduction to Terraform
 
Introduction to Terraform
  •  terraform apply “main.tfplan”
Introduction to Terraform
  • Now, verify the created resource in Azure Portal. The below image shows it has been created successfully.
Introduction to Terraform
 

Conclusion

 
We have successfully created a resource group, service plan, and web app in Azure using infrastructure as code. It is a powerful and easy-to-use tool that provides more control over infrastructure provisioning at a remote destination. It can also be incorporated in the Azure DevOps CI/CD pipeline easily.