Post

Azure | Infrastructure as Code with ARM Template and Terraform

In modern cloud computing environments, Infrastructure as Code (IaC) has become a cornerstone for managing and provisioning resources efficiently. Two popular tools for implementing IaC are Azure Resource Manager (ARM) Templates and Terraform. Below, we’ll explore both with detailed examples.

Azure Resource Manager (ARM) Template

ARM Templates are JSON files used to define the infrastructure and configuration of Azure resources. They offer a declarative way to provision resources consistently.

Example ARM Template

Below is a simple ARM template that deploys an Azure Storage Account:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-06-01",
      "name": "mystorageaccount",
      "location": "eastus",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "StorageV2",
      "properties": {}
    }
  ]
}

In this template:

  • type: Specifies the Azure resource type (Microsoft.Storage/storageAccounts).
  • apiVersion: Specifies the API version to use for deployment.
  • name: Specifies the name of the storage account.
  • location: Specifies the Azure region for deployment.
  • sku: Specifies the pricing tier and replication type.
  • kind: Specifies the kind of storage account (StorageV2 in this case).
  • properties: Specifies additional properties (empty in this example).

Terraform

Terraform is an open-source infrastructure as code software tool created by HashiCorp. It allows users to define and provision data center infrastructure using a high-level configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON.

Example Terraform Configuration

Below is a simple Terraform configuration that deploys an Azure Resource Group:

1
2
3
4
5
6
7
8
provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "East US"
}

In this configuration:

  • provider "azurerm": Specifies the Azure provider for Terraform.
  • azurerm_resource_group: Specifies the resource type (azurerm_resource_group) and a logical name (example).
  • name: Specifies the name of the resource group.
  • location: Specifies the Azure region for deployment.

What Next?

Both ARM Templates and Terraform offer powerful ways to manage infrastructure as code in Azure environments. While ARM Templates are native to Azure and use JSON, Terraform is a multi-cloud tool that supports various providers and uses HCL or JSON for configuration. Choose the tool that best fits your needs and preferences for managing Azure resources efficiently.

This post is licensed under CC BY 4.0 by the author.