Terraform On Azure - Getting Started

Introduction

 
Infrastructure as Code is an important aspect to be considered while building a cloud deployment solution. We can plan our cloud infrastructure, design our cloud solution, and then use the portal or the user interface provided by the cloud provider to provision the cloud components as per the cloud solution. However, this approach would hold good in a development environment. In the production or the client environment, we need to build a one-click solution that should spin up the cloud infrastructure and deploy the application to the cloud. Infrastructure as Code can be used here to build one-click cloud infrastructure and deployment solutions and simplify cloud environment deliveries.
 
In this article, we will explore the basics of Infrastructure as Code in the context of Terraform. In the subsequent articles, we will dive deep into how to build cloud infrastructure using Terraform.
 

What is Infrastructure as Code and Terraform

 
Infrastructure as Code helps you depict the infrastructure resources as descriptive configuration files and software code. Then you can apply these configuration files and software code in the target environment to spin up the infrastructure. For example, you need a Virtual Machine in a private Virtual Network with Tomcat installed. You are planning to host the Java application code on the Tomcat. To address this scenario, you can write a software code that can create a Virtual Network and Subnet, create a Virtual Machine in the Subnet and install Tomcat on the Virtual Machine once it is ready. All necessary configurations like name and size of Virtual Machine, name of Virtual Network, and many more can be kept in a configuration file.
 
There are many vendors that provide Infrastructure as Code solutions like Terraform, Chef, Puppet, and many more. You can also use Azure CLI or Azure PowerShell or Azure ARM Templates to build Infrastructure as a Code solution. Azure DevOps also provides an excellent mechanism to build Infrastructure as Code solutions. You need to select the right provider that fits your infrastructure design.
 
Adopting Infrastructure as Code is easy. You need to analyze the Infrastructure requirements, plan the infrastructure, prepare a blueprint or design for the infrastructure, choose the right Infrastructure as a Code provider and then transform the infrastructure blueprint into an Infrastructure as a Code solution.
 
 
The following are a few of the advantages you get while adopting Infrastructure as Code.
  • Build an immutable infrastructure and ensure that you spin out identical infrastructure resources across environments
  • Build a reusable solution to deploy infrastructure for your application across clients and environments. You can make changes to the configuration files and apply the Infrastructure as Code solution across environments
  • Saves manual effort and time required to build infrastructure
  • Reduces manual error as the infrastructure creation process gets automated
  • Organizes the Infrastructure solution and makes it a one-click activity. You package the infrastructure deployment solution and execute it in the target environment.
Terraform is an Infrastructure as Code offering from HashiCorp and is widely adopted for creating cloud infrastructures. It is highly compatible with Azure and is widely used to spin up resources in Azure using Infrastructure as Code.
 

Create a Resource Group using Terraform

 
Log in to the Azure portal at https://portal.azure.com. Let us use Azure Cloud Shell to create resources using Terraform. Azure Cloud Shell has Terraform installed and you need not do any installation or configuration to work with Terraform.
 
 
Once the Azure Cloud Shell opens up, select Bash.
 
 
Let us create a Terraform script to create a Resource Group. Open nano editor using the following command.
  1. nano myterraformscript.tf
Paste the following code in the nano editor and follow on-screen instructions to save the Terraform script file. Replace {Resource Group Name} with the name of the Resource Group and {Resource Group Location} with the location for the Resource Group.
  1. terraform {  
  2.   required_providers {  
  3.     azurerm = {  
  4.       source = "hashicorp/azurerm"  
  5.     }  
  6.   }  
  7. }  
  8. provider "azurerm" {  
  9.   features {}  
  10. }  
  11. resource "azurerm_resource_group" "resourcegroup" {  
  12.   name = "{Resource Group Name}"  
  13.   location = "{Resource Group Location}"  
  14. }  
Run the following command to initiate Terraform. This would fetch all dependencies needed to execute the Terraform script.
  1. terraform init  
Now let us create an execution plan for Terraform. Let us provide the name of the execution plan in the out parameter.
  1. terraform plan -out myrg.tfplan  
Execute the execution plan using the following command. The Resource Group gets created.
  1. terraform apply "myrg.tfplan"  

Conclusion

 
In this article, we explored the details of Infrastructure as Code and created a Resource Group using Terraform. In the upcoming articles, we will deep dive into the concepts of Terraform and create Azure Resources using Terraform scripts.