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.
- ################## Providers ########################################
- provider "azurerm" {
- version = "=2.36.0"
- features {}
- }
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.
- ################## Variables ########################################
- variable "resource_group_name" {
- type = string
- description = "RG name in Azure"
- default = "my_terraform_rg"
- }
-
- variable "resource_group_location" {
- type = string
- description = "RG location in Azure"
- default = "centralindia"
- }
-
- variable "app_service_plan_name" {
- type = string
- description = "App Service Plan name in Azure"
- default = "my-appserviceplan"
- }
-
- variable "app_service_name" {
- type = string
- description = "App Service name in Azure"
- default = "terraform-homedemo-010"
- }
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.
- ################## Resources ########################################
- resource "azurerm_resource_group" "rg" {
- name = var.resource_group_name
- location = var.resource_group_location
-
- tags = {
- environment = "development"
- }
- }
-
- resource "azurerm_app_service_plan" "app_plan" {
- name = var.app_service_plan_name
- location = azurerm_resource_group.rg.location
- resource_group_name = azurerm_resource_group.rg.name
- #kind = "Windows"
-
- sku {
- tier = "Standard"
- size = "S1"
-
- }
- tags = {
- environment = "development"
- }
- }
-
- resource "azurerm_app_service" "webapp" {
- name = var.app_service_name
- location = azurerm_resource_group.rg.location
- resource_group_name = azurerm_resource_group.rg.name
- app_service_plan_id = azurerm_app_service_plan.app_plan.id
-
- site_config {
- dotnet_framework_version = "v4.0"
- scm_type = "LocalGit"
- default_documents = [
- "hostingstart.html"
- ]
- }
-
- app_settings = {
- "SOME_KEY" = "some-value"
- }
-
- tags = {
- environment = "development"
- }
- }
Run above Terraform commands in the below sequence.
- Terraform version -> to check the version of terraform.
- terraform plan -out main.tfplan
- terraform apply “main.tfplan”
- Now, verify the created resource in Azure Portal. The below image shows it has been created successfully.
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.