Skip to main content

Get Started Terraform on Azure

· 5 min read
Hasan Gural

These days, Lots of people want to use Terraform which is from Hashi Corp. In this article, I'm going to be writing about Terraform and Azure. The Terraform is an open source software. As a tool for building, changing, versioning infrastructure. Terraform within configuration files I can explain to Terraform the components need to run, I could say single application or multiple application. As you know, when you heard Terraform, you suppose it which was working with only Cloud Provider. However, Terraform works with Cloud Providers such as Amazon, Cloudfare DigitalOcean, or Azure etc. also works with On-Premises resources like Vmware. The Main idea is Infrastructure as Code.

Terraform manages different type of components. The components can be low-level or high-level resources. I can give an example of components. As Low-Level Resource "Storage, Network" or As High-Level Resource like "DNS Enter record or change rule on Load Balancer". Moreover, we know all that, we have used different types of tool to deploy resource on Azure. I mean, we have used Azure Resource Manager in the typical case or use Azure Portal or Azure Software Development Kit or maybe REST API something like that. We know, if you've used Amazon Web Services, Amazon Web Services do same. Therefore, DevOps Engineer loves using YAML or JSON so the Problem space is here. The Terraform which is starting to solve this problem has magic tricks. The tool is doing create a common structure or well-known format. That allow developers or DevOps Engineer for get used to the same format. DevOps Engineer will have described their resources and they will use their code on Azure or Azure. Conclusion, it allows people to develop, manage their infrastructure as code. It's enthusing. Briefing for Terraform has completely written in GO.

Firstly, we have need to setup Terraform access to your Azure Services. We have used the shared account in this demonstration. I will demonstrate in Azure CLI.

#Firstly, login to the Azure CLI using:

az login

The Subscription which will be going to demonstration.

az account set --subscription="3b40246b-ffa0-43df-a51e-0c2317b4afc3"

Next, create separate credentials for Terraform.

az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/3b40246b-ffa0-43df-a51e-0c2317b4afc3"

Now you see your AppId, DisplayName, Password, tenant.

Now, I have created Azure Service Principal for managing our Azure Resource with Terraform. The Service Principal will have used by Terraform. After, We are going to use our Service Principal Account because we need to authenticate within Service Principal Account. We have to use those lines for authenticating in Visual Studio Code. The previous picture as you can see our Service Principal Details like AppId, DisplayName, Name, Password. You have learned details in the previous picture our Service Principal Details like App Id, DisplayName, Name, Password. Now, I'm going to authenticate with them.

Now, We are ready to use Terraform. Almost we are done. Let's learn to Terraform. Do not forget, I have created a project in the Visual Studio Code, therefore, I have installed the necessary extension and client application on my computer. I will be trying to create a few resources within Terraform. When I wrote this article, I was using Visual Studio code. If you are interested in this editor, you should be able to check my personal blog. You can check this heading on the web site. "How to use Visual Studio Code".

I have written a few lines in Visual Studio Code before I'm calling Terraform Application for Azure CLI. It is not a complicated process. You must write "Terraform init" for initializing this application. After All, I have called Terraform. Let's see.

Now, I think, We are good to go. I'm going to show you our Terraform structure. As you know, It has a common structure and quite easily. You could see our Terraform structure below.

Now we can push to our code into the Terraform client application. As you can see top of the code block, I have indicated provider to "Azurerm". If you want to change it for your provider, you can have a look this website. I deployed that code block into the Terraform with parameter. The "Apply" parameter will deploy your defined-code on Azure.

provider "azurerm" {

If you want to add your Azure Service Pricipal Account details, you can manifest here.

As you know I did add before.


Create a resource group

Create a resource group

resource "azurerm_resource_group" "network" { name = "terraform-RG" location = "West US" }

Create a virtual network within the resource group

resource "azurerm_virtual_network" "network" { name = "terraform-vNET" address_space = [""] location = "${}" resource_group_name = "${}"

subnet { name = "terraForm-VNET" address_prefix = "" } }

Conclusion, It was successfully when we had deployed our resources on Azure Portal. The best ways for Terraform, DevOps Engineer does not need to focus which one is the best YAML or JSON. I hope so, they will have focused their business.