Hi there

As an introduction of Infrastructure as Code (IaC) with Azure Resource Manager (ARM) Template I’d like to cover this in a couple articles.

At this one we’re gonna cover the concept and how to prepare our environment on Windows 10 machine using Visual Studio Code (you can also do the same on Notepad++) with Terraform.

Advantages of using IaC on your Azure environment with ARM (before Terraform)

  • Consistency: Consistently achieve standardized provisioning or deployment
  • Accelerating: Accelerating provisioning or deployment rapidly
  • Reusability: Reusable JSON code for repeatable or similar provisioning or deployment
  • Extensibility: Extensible JSON code for incorporating with additional items

Advantages of using Terraform to automate Azure deployments

  • Orchestration, not merely configuration
  • Immutable infrastructure
  • Declarative, not procedural code
  • Client-only architecture

Setup your environment

  1. Tutorial: Configure the Azure Terraform Visual Studio Code extension
  2. Prerequisites
    • Azure subscription: If you don’t have an Azure subscription, create a free account before you begin.
    • Terraform: Install and configure Terraform. I’ve tested with the Terraform v0.12.24 and provider.azurerm v2.5.0
    • Visual Studio Code: Install the version of Visual Studio Code that is appropriate for your environment.
  3. Prepare your dev environment
  4. Install Git
  5. Install Terraform
  6. Install Node.jsLatest Version (checked April 14th)
  7. Install GraphViz
  8. Install Terraform Visual Studio Code extension
    1. Launch Visual Studio Code.
    2. Select Extensions.
    3. Use the Search Extensions in Marketplace text box to search for the Azure Terraform extension:

    4. Select Install.
  9. Prepare a test plan file

      1. In Visual Studio Code, select File > New File from the menu bar.
        1. copy the code below
          #Provider
          provider "azurerm" {
          features {}
          }
          resource "azurerm_resource_group" "example" {
            name     = "terraform"
            location = "canadacentral"
          }
      2. From the menu bar, select File > Save As.
      3. In the Save As dialog, navigate to a location of your choice and then select New folder. (Change the name of the new folder to something more descriptive than New folder.)
      4. Make sure your new folder is highlighted (selected) and then select Open.
      5. In the Save As dialog, change the default name of the file to main.tf.
      6. Select Save.
      7. In the menu bar, select File > Open Folder. Navigate to and select the new folder you created.
        c:\Azure\1\
        check your main.tf file content

    1. For this deployment let’s use your Powershell or CMD.exe (make sure you have terraform in your Environment PATH)
      1. Run all commands from the folder where your main.tf is located
            1. terraform init 

            2. terraform plan 

            3. terraform apply (before we need to login in Azure)
              az login
              – enter your Azure Subscription login on the browser if you have MFA enabled
              – proceed after the browser notifies you to close it and go back to your deployment
              answer YES to the proceed with this deployment

              check the deployment was successful

              check the Azure Resource Group created in Azure

References

https://www.terraform.io/docs/providers/azurerm/index.html
https://learn.hashicorp.com/
http://portal.azure.com/
https://registry.terraform.io/providers/hashicorp/azurerm/2.5.0
https://registry.terraform.io/browse/providers

Thanks,

Thiago Beier
TwitterLinkedInFacebookRSS