avatartarun bhatt

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2083

Abstract

re>resource “azurerm_resource_group” “newrg” { name = var.resource_group_name <span class="hljs-keyword">location</span> <span class="hljs-title">= var</span>.<span class="hljs-keyword">location</span> <span class="hljs-title">}</span></pre></div><p id="c6d8"><b>Step 2: Apply changes</b></p><p id="351b"><b>Login using Az Cli</b></p><p id="3309">Use ‘Az login’ command to login into your Azure subscription.</p><p id="bcb7">Important points to be noted before we move on to the next step:</p><ol><li>Az Cmdlets should be installed on the machine</li><li>In case there are multiple subscriptions associated with the account, choose the subscription where the resource should be created. Az command to set a particular subscription is:</li></ol><p id="02e1"><code>az account set --subscription "My Demos"</code></p><p id="3765"><b>Initialize Terraform project</b></p><p id="404c">Once the configuration file is ready, it time to apply the changes. If the tf file is executed for the first time, it needs to be initialized with the command</p><div id="abf2"><pre>terraform <span class="hljs-keyword">init</span></pre></div><figure id="5756"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*0Nzsc9v5yM34rR6p6NqXtQ.png"><figcaption></figcaption></figure><p id="08b9">Terraform init created a .terraform folder which consists of all the plugins needed to run the terraform tf file.</p><figure id="8208"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*iq1TzbR6dcetSxnOqDOh0w.png"><figcaption></figcaption></figure><p id="6a8b"><b>Apply Changes</b></p><p id="ffc3">The configuration file is ready and the plugins has been downloaded. Its time to run the tf file using the command</p><div id="8609"><pre>terraform <span class="hljs-built_in">apply</span></pre></div><figure id="f981"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*oggny7OR0Lq7pYd9HM72Ow.png"><figcaption></figcaption></figure><p id="9056"><b>Verify Resource</b></p><p id="2960">Login to Azure portal and the new resource group (tblab) should be present.</p><p id="d448

Options

"><b>Terraform State and Backup files</b></p><p id="6700">Terraform apply will also create a terraform.tfstate file. State files are used to store the current state of configuration file. When a configuration file is used to provision resources, the state of the configuration file is saved in the state files.</p><figure id="7451"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*nFsBesA8jzHKCElYjHSiwg.png"><figcaption></figcaption></figure><p id="c58a">The easiest way to understand the significance of the state file is to re-run the apply command and change the name of the resource group at runtime.</p><figure id="678e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*IRez-h4OtXSE4oHS7lAuGA.png"><figcaption></figcaption></figure><p id="ba90">A new file terraform.tfstate.backup is also created. The backup file will store the state of the terraform project before the apply command was executed. A quick comparison with the newly created state file will look like:</p><figure id="76df"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*pNDvXmRbxwvSpggQRnKKvA.png"><figcaption></figcaption></figure><p id="2ffd">In the coming articles, I would cover some complex scenarios. Hopefully this article was of help for readers new to Terraform.</p><p id="abed">Link to access files for this project is <a href="https://github.com/tarunbhatt9784/Terraform/tree/master/Terraform-Create%20Azure%20RG">here</a>.</p><p id="9a33">Regards Tarun</p><p id="a5a9">P.S — Medium is an excellent platform to read, write and learn from fellow authors. If you want to join me in this journey, Join <a href="https://tarunbhatt9784.medium.com/membership">medium</a> today.</p><p id="afd3"><b>References</b>:</p><p id="8a7f"><a href="https://readmedium.com/say-hi-to-terraform-5833cfade4cd">Overview</a></p><p id="a070"><a href="https://learn.hashicorp.com/terraform/getting-started/install.html">Installation</a></p><p id="5aa2"><a href="https://app.pluralsight.com/library/courses/getting-started-terraform">Getting Started</a></p></article></body>

A Demo on Terraform: Create Azure Resource Group

In my previous article, I gave a high level introduction of Terraform to my readers. The article was focused on:

  1. Advantages and Need for IaC (Infrastructure as a code)
  2. Introduction to Terraform
  3. Different components of a Terraform project
  4. Terraform Config file
  5. Installation

No amount of theory is good enough to learn a new technology. Hence, let’s get our hands dirty with a demo.

Demo Time

In this demo, I will create a Terraform project to provision an Azure Resource Group. So let’s get started.

Step 1: Terraform Configuration File

Terraform configuration file is the most important component. It contains the code which is primarily responsible to provision resources on Cloud.

The configuration file will contain two main elements. Variables and Resources

Set up Variables

Variables are used to store important values in a Terraform Configuration file. We will need two variables to store Resource Group Name and the Location.

  1. resource_group_name : The value will be passed at run time
  2. location: Default value of the location will be provided as ‘Australia Southeast’

Set up Provider

A provider is responsible for creating resources on a cloud platform. To provision Azure Resource group, we will azurerm.

Create Resources

With variables and provider in place, its time to create resources:

############################################
# RESOURCES
############################################
resource “azurerm_resource_group” “newrg” {
 name = var.resource_group_name
 location = var.location
}

Step 2: Apply changes

Login using Az Cli

Use ‘Az login’ command to login into your Azure subscription.

Important points to be noted before we move on to the next step:

  1. Az Cmdlets should be installed on the machine
  2. In case there are multiple subscriptions associated with the account, choose the subscription where the resource should be created. Az command to set a particular subscription is:

az account set --subscription "My Demos"

Initialize Terraform project

Once the configuration file is ready, it time to apply the changes. If the tf file is executed for the first time, it needs to be initialized with the command

terraform init

Terraform init created a .terraform folder which consists of all the plugins needed to run the terraform tf file.

Apply Changes

The configuration file is ready and the plugins has been downloaded. Its time to run the tf file using the command

terraform apply

Verify Resource

Login to Azure portal and the new resource group (tblab) should be present.

Terraform State and Backup files

Terraform apply will also create a terraform.tfstate file. State files are used to store the current state of configuration file. When a configuration file is used to provision resources, the state of the configuration file is saved in the state files.

The easiest way to understand the significance of the state file is to re-run the apply command and change the name of the resource group at runtime.

A new file terraform.tfstate.backup is also created. The backup file will store the state of the terraform project before the apply command was executed. A quick comparison with the newly created state file will look like:

In the coming articles, I would cover some complex scenarios. Hopefully this article was of help for readers new to Terraform.

Link to access files for this project is here.

Regards Tarun

P.S — Medium is an excellent platform to read, write and learn from fellow authors. If you want to join me in this journey, Join medium today.

References:

Overview

Installation

Getting Started

Terraform
Infrastructure As Code
Azure
Azure Resource Group
Azure Cli
Recommended from ReadMedium