Software Development

Maintain your Azure Infrastructure with Terraform

Young Caucasian Female Programmer In Glasses Writes Program Code On A Laptop

Terraform is an Infrastructure as Code (IaC) tool owned by HashiCorp, which allows you to build, deploy, change, and manage infrastructure using code. It helps you define resources and infrastructure in human-readable, declarative configuration files and manages your infrastructure’s lifecycle.

Code is the instruction written in a human-readable format with extension (.tf) or in (.tf.JSON), which is written in Hashicorp Configuration Language (HCL).

What is IaC?

Infrastructure as code is managing and provisioning computer data centers through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools.

Why Use IaC?

Historically, managing IT infrastructure was a manual process. People would physically put servers in place and configure them, which was time-consuming and costly. Manual infrastructure management can no longer meet the demands of today’s rapidly growing businesses.

IT organizations are quickly adopting the public cloud, primarily API-driven, to meet evolving customer demands and save costs. This allows them to build applications to support a much higher level of elasticity and deploy their application on supporting technologies like Docker container and Public Cloud. Building, managing, and deploying code on tools like Terraform accelerates delivery.

Benefits of IaC

The most significant benefits of using IaC are:

Speed

IaC benefits a company’s IT architecture and workflow as it uses automation to substantially increase the provisioning speed of the infrastructure’s development, testing, and production.

Consistency

Since it is code, it generates the same result every time. It provisioned the same environment every time, always enabling improved infrastructure consistency.

Cost

One of the main benefits of IaC is lowering the costs of infrastructure management. With everything automated and organized, engineers save time and money, invested in performing other manual tasks and higher-value jobs.

Minimum Risk

IaC allows server configuration that can be documented, logged, and tracked later for reference. Configuration files will be reviewed by a person or policy as a code (sentinel) for security leakages.

Everything Codified

The main benefit of IaC is explicit coding to configure files in use. You can share codes with the team, test them to ensure accuracy, maintain uniformity, and update your infrastructure into the same flow of IaC.

Version Controlled

Since the infrastructure configurations are codified, we can check in to version control like GitHub and start versioning it. IaC allows you to track and give insight on what, who, when, and why anything changed in the process of deployment. This has greater transparency, which is often lacking in traditional infrastructure management.

Terraform Workflow

Let’s take a closer look at a typical terraform workflow.

Terraform Workflow

Terraform Init

The terraform init command is used to initialize a working directory containing Terraform configuration files.

Terraform plan

The terraform plan command is used to create an execution plan.

Terraform Apply

The terraform apply command is used to apply the changes required to reach the desired state of the configuration.

Terraform Refresh

The terraform refresh command is used to reconcile the state Terraform knows about (via its state file) with the real-world infrastructure. This does not modify infrastructure but does modify the state file.

Terraform Destroy

The terraform destroy command is used to destroy the Terraform-managed infrastructure.

Advantages of Terraform

There are many advantages to using Terraform:

Multi-Cloud

Terraform can manage infrastructure on multiple cloud platforms like AWS, Azure, and GCP.

Human Readable Language

Terraform can also support human-readable language to build the code and track the resource changes throughout the Deployments.

Maintain and Reuse

Configuration templates, i.e., pre-made modules for infrastructure components, are written, reviewed, managed, and reused after storing them under VCS (Version Control System).

Flexibility

Terraform not only handles IaaS (AWS, Azure, etc.) but also PaaS (SQL, NodeJS). It can also store local variables such as cloud tokens and passwords in encrypted form on the terraform registry.

Terraform 10

What’s New with Terraform?

Terraform is written in GO and available across platforms as a single binary which you place in the system path. Plugins Provide service-specific Integration, including an Azure provider. Terraform is natively available in the Azure Cloud Shell.

Getting Started with Terraform

Step 1: Install Visual Studio for writing a terraform code and add Terraform Plugins in Visual Studio

Step 2: Create a Directory to test the sample terraform  Code.

Step 3: Open a Newly created directory in Visual Studio and execute the following commands to get started with Terraform:

  •    To Intialize the terraform directory use #terraform init
  •   To login into the Azure Cloud, use #az login
  •    To confirm the current Azure subscription, use #az account show
  •    To set specific Azure subscription use #az account set –subscription “<subscription_id_or_subscription_name>”

Step 4: Create a file named main.tf and insert the following code as shown in the snapshot to deploy the different resources.

The following snapshots show the code for IT Infrastructure deployment in Azure Cloud. All the values of resources are indicated with the test values. You can change those values to whatever makes sense for our environment.

Providers

We can work on Terraform to deploy the resources on Multi-Cloud.

Terraform 1

Version Control  

Terraform 2

Resource Group

Terraform 3

Vnet Configuration

Terraform 4

Subnet Configuration

Terraform 5

Database-Virtual-Network 

Terraform 6

Database Subnet

Terraform 7

Network Security Group

Terraform 8

Virtual Machine

Terraform 9

There are many different ways to use Terraform: as an individual user, a single team, or an entire organization at scale. Choosing the best approach for the density of collaboration needed will provide the most return on your investment in the core Terraform workflow. Terraform introduces new layers that build on this core workflow to solve problems unique to teams and organizations for organizations using Terraform at scale.

 

 

About the Author

Praful Itankar is a technical consultant with over 7 years of experience in cloud technologies. He looks forward to sharing his knowledge on the latest technology trends through blogging.

More from this Author

Thoughts on “Maintain your Azure Infrastructure with Terraform”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up