Terraform for Azure : First Look

What is Terraform ?

Terraform is a tool developed by HashiCope for building, changing, and versioning infrastructure safely and efficiently. By using Terraform we can manage existing and well-known service as well as custom in-house developed solutions. Terraform support verity of cloud platforms like Azure, AWS, GCP etc. Find full supported provides from here.

Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.

The key features of Terraform as follows.

  • Infrastructure as Code
  • Execution Plans
  • Resource Graph
  • Change Automation

How can I Install Terraform ?

Terraform is completely written in GO language there for we don’t have to install any dependencies,dll or binaries. It only include a single excitable file. We can download it from Terraform for supported OS. After it download we have to add the path to OS.

 Deploying Resources to Azure

As I mentioned previously we can use Terraform to deploy Azure services (Infrastructure as code). But before we start first we have to do some house keeping stuff.

  1. Install Terraform and add it to OS path
  2. Install Azure CLI
  3. Text Editor (I prefer VS Code)

Before you deploy (apply according to Terraform lingo) we have to install Azure CLI, we can find Azure CLI from here . Terraform use Azure CLI to authenticate with the subscription so its necessary to have install in your workstation.

If you have any previous experience in Azure ARM template creating then this will be a familiar playground for them. In Terraform it doesn’t use JSON as ARM template it use its own language with extension .tf

For VS Code you can find few extensions to access Terraform snippets.

Step 1

In Terraform file first we have to mention the provider as bellow

provider "azurerm" {
   subscription_id = "<Subscription ID>"
   tenant_id = "<Tenant ID>"
}

Step 2

open the command prompt and navigate to the folder which contain .tf file. first run the Azure CLI login command to authenticate to Azure

az login

Now we are ready to apply (deploy) the code to azure using Terraform, from this post I wont explain each resources and component in Terraform. From this I use a Terraform file I created to deploy a VM in Azure.

Before apply this code we need to run following Terraform command it will initialize Terraform working directory.

terraform init

Following is the code I used

provider "azurerm" {
   subscription_id = "<Subscription ID>"
   tenant_id = "<Tenant ID>"
}

resource "azurerm_resource_group" "terraform" {
   name = "terraform"
   location = "Southeast Asia"
}

resource "azurerm_virtual_network" "test-vnet" {
   name = "test-vnet"
   location = "${azurerm_resource_group.terraform.location}"
   resource_group_name = "${azurerm_resource_group.terraform.name}"
   address_space = ["10.30.0.0/16"]
}

resource "azurerm_subnet" "vnet-subnet" {
    name = "default"
    resource_group_name = "${azurerm_resource_group.terraform.name}"
    virtual_network_name ="${azurerm_virtual_network.test-vnet.name}"
    address_prefix = "10.30.10.0/24"
}

resource "azurerm_network_interface" "linuxvm-nic" {
   name = "linuxnic1"
   resource_group_name = "${azurerm_resource_group.terraform.name}"
   location = "${azurerm_resource_group.terraform.location}"

   ip_configuration {
       name = "ipconfig1"
       subnet_id ="${azurerm_subnet.vnet-subnet.id}"
       private_ip_address_allocation = "Dynamic"
       public_ip_address_id = "${azurerm_public_ip.linuvmpublic.id}"
   }
}

resource "azurerm_public_ip" "linuvmpublic" {
   name = "vmpublic"
   resource_group_name = "${azurerm_resource_group.terraform.name}"
   public_ip_address_allocation = "dynamic"
   location = "${azurerm_resource_group.terraform.location}"
}   

resource "azurerm_storage_account" "vmstorage" {
   name = "vmstoragekasun"
   resource_group_name = "${azurerm_resource_group.terraform.name}"
   location = "${azurerm_resource_group.terraform.location}"
   account_tier = "Standard"
   account_replication_type = "LRS"
}

resource "azurerm_storage_container" "vhds" {
   name = "vhds"
   resource_group_name = "${azurerm_resource_group.terraform.name}"
   storage_account_name = "${azurerm_storage_account.vmstorage.name}"
   container_access_type = "private"
}

resource "azurerm_virtual_machine" "linuxvm" {
   name = "terraformTest"
   resource_group_name = "${azurerm_resource_group.terraform.name}"
   location = "${azurerm_resource_group.terraform.location}"
   network_interface_ids = ["${azurerm_network_interface.linuxvm-nic.id}"]
   vm_size = "Standard_A0"

   storage_image_reference {
       publisher = "Canonical"
       offer     = "UbuntuServer"
       sku       = "14.04.2-LTS"
       version   = "latest"
   }

   storage_os_disk {
       name          = "myosdisk1"
       vhd_uri       = "${azurerm_storage_account.vmstorage.primary_blob_endpoint}${azurerm_storage_container.vhds.name}/myosdisk1.vhd"
       caching       = "ReadWrite"
       create_option = "FromImage"
   }

   storage_data_disk {
       name          = "datadisk0"
       vhd_uri       = "${azurerm_storage_account.vmstorage.primary_blob_endpoint}${azurerm_storage_container.vhds.name}/datadisk0.vhd"
       disk_size_gb  = "1023"
       create_option = "empty"
       lun           = 0
   }

   os_profile {
       computer_name  = "vm1"
       admin_username = "localadmin"
       admin_password = "<Password>"
   }

   os_profile_linux_config {
       disable_password_authentication = false
   }
}

To apply the code we can use following command (make sure you running the command in the folder which .tf has)

terraform apply

After run above it authenticate with Azure and start to deploy infrastructure.

if you want to destroy the infrastructure we can use following command,this will destroy the infrastructure specified in the code deploy.

terraform destroy

Additional Info

Terraform: Up and Running: Writing Infrastructure as Code

, , , ,

Post navigation