Terraform with Microsoft Azure – Part 2

Hi Friends,

In this section, we will continue from the last post and delve further into terraform. Having said that let’s get started.

The very first thing, which we need to do is to download the terraform executables from here. We need to download the required version. Installation is pretty simple, just download the same and put it some global accessible location. In-case of windows, its System32. I have placed the binary here. I can now go ahead and type terraform –version. This will return the below version

Next, let’s go ahead create terraform project. I have created with the name terraform-basic and created one file main.tf.

My snippet looks like this

Having said that, my folder structure looks like I have also installed two terraform extensions to help me with terrform options and with azure as well. They are If I type anything in editor now, it will give me intellisense support like shown below. Let’s say, I apply tags, then it will look like   Now, let me explain the code here.

    • The provider directive indicates that I am using azurerm of version 2. You can learn more about azure provider here.


  • The resource directory indicates that I have created a new resource with name terraform-basic in the location eastus with dev as tags.



  • Let’s say, we just want to deploy our resource group itself to azure. Before that, let’s explore Terraform extension, which we got on the blade.When I click on this, it will present me same in below directory format.



Terraform initialization:

    •  We will run terraform init into our folder like shown below. Sole purpose of this initialization project is to pull the required dependencies to build the project.We will be using azure cli for authentication purpose in this case. In case, if you haven’t downloaded Azure CLI, you can download the same from here. We will delve service principle or managed identity later. Having said that, let’s login with az login command. It will open browser with your login credentials. Select the correct login. Then, it will redirect in the terminal with subscription details in 10 seconds. Since, I have two subscriptions, hence it will show below output like shown below.


  •  But before deployment, let’s go ahead and run terraform plan -out execPlan.plan command like shown below. Plan command generates an execution plan for our project, which will be used during deployment. We will see the same in a moment. Having said that, let’s run the command.



  •  It gave me error. And the reason is that, my script is not align with the new changes in this version. Hence, I need to include features flag like shown below.



  •   At this moment when I ran the command, it gave me below error.



    • Its good that we are facing these issues live and fixing it as well. Now, finished snippet looks like shown below.

  • I will go ahead and run the command, then it will give me following output

  • This also dropped following assets in our repository

  • We will learn about locking and other concepts in coming sections. Here, we can see that execution plan got created, which is nothing but executable file.
  • I will now go ahead and run terraform apply “execplan.plan” command or from command palette itself like shown below.

  • Having said that let’s go ahead and run the command like shown below.

  • It also dropped one state file which looks like

  • File will look like

  • At this stage, when I login into azure portal and check the resource groups, I can see that its created like shown below.

  • Next, we can destroy this resource using terraform destroy command like shown below.

  • Since, we are destroying the resource, hence it will ask for the confirmation message like shown above. We just need to type yes and continue.
  • It will show logs in the terminal that its destroying and in some time, it will print the confirmation message.

With this I would like to wrap this session. In the next session, we will delve deeper into terraform artifacts. Till then stay tuned and Happy Coding.

Rahul Sahay
Happy Coding

284 total views, 7 views today