Deploy ARM template using Terraform

Deploy ARM template using Terraform? Yes; you’ve read the blog title correctly!

From time-to-time you you may have to deploy a resource or feature in Azure that is not yet available within Terraform.

When this happens, there is an option of deploying an ARM template using terraform resource azurerm_template_deployment

In this blog, I will show you how you can deploy an ARM template using the Terraform resource azurerm_template_deployment. I have created a module to deploy the specific resource documented below

For my example the ARM template will be deploying a Storage Account (Not recommended to deploy a Storage Account this way as there already is a terraform resource for this, using as example only as it is an easy ARM template to follow)

I have created the following folder structure for my Terraform ARM Module

modules/tf-storageaccount-arm(folder)
│   main.tf
│   variables.tf 
│
└───templates(folder)
│   │   storage-account.json

storage-account.json below, basic ARM template that references some parameters

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
      "storageAccountType": {
        "type": "string",
        "metadata": {
          "description": "Storage Account type"
        }
      },
      "location": {
        "type": "string",
        "metadata": {
          "description": "Location for all resources."
        }
      },
      "StorageAccountName": {
        "type": "string",
        "metadata": {
          "description": "Storage Account Name"
        }
      },
      "StorageAccountKind": {
        "type": "string",
        "metadata": {
          "description": "Storage Account Kind"
        }
      }
    },
    "variables": {
    },
    "resources": [
      {
        "type": "Microsoft.Storage/storageAccounts",
        "apiVersion": "2019-04-01",
        "name": "[parameters('StorageAccountName')]",
        "location": "[parameters('location')]",
        "sku": {
          "name": "[parameters('storageAccountType')]"
        },
        "kind": "[parameters('StorageAccountKind')]",
        "properties": {}
      }
    ]
  }

main.tf of the module will reference the use of Terraform resource azurerm_template_deployment

resource "azurerm_template_deployment" "storage_account_arm" {
  name                = var.storage_account_deployment_name
  resource_group_name = var.storage_account_resource_group_name
  deployment_mode     = "Incremental"
  template_body       = file("Modules/tf-storageaccount-arm/templates/storage-account.json",)
  parameters = {
    StorageAccountName      = var.storage_account_StorageAccountName
    storageAccountType      = var.storage_account_storageAccountType
    location                = var.storage_account_location
    StorageAccountKind      = var.storage_account_StorageAccountKind
  }
}

variables.tf file, notice the reference of default?

variable "storage_account_StorageAccountName" {
  description = "Storage Account Name"
}

variable "storage_account_storageAccountType" {
  description = "Storage Account Type"
  default     = "Standard_LRS"
}

variable "storage_account_location" {
  description = "Storage Account Location"
}

variable "storage_account_StorageAccountKind" {
  description = "Storage Account Kind"
  default = "StorageV2"
}

variable "storage_account_resource_group_name" {
  description = "Storage Account Resource Group"
}

variable "storage_account_deployment_name" {
  description = "Storage Account Deployment Name"
}

Awesome; the Terraform ARM module is now ready to be deployed within your Terraform code.

I have used the below code to deploy my Storage Account via ARM module. I am referencing some var (variables) that I have previously declared within my initial deployment.

module "storage_account" {
 source                               = "./Modules/tf-storageaccount-arm"
 storage_account_StorageAccountName   = "tamopssaarm"
 storage_account_storageAccountType   = "Standard_LRS"
 storage_account_location             = var.location
 storage_account_StorageAccountKind   = "StorageV2"
 storage_account_resource_group_name  = module.rg.name
 storage_account_deployment_name      = "tamopsa-arm-deployment"
}

Now lets deploy the above module, I use a Terraform pipeline I created in Azure DevOps to run my testing, I’ve blogged a sample pipeline Deploy Terraform using Azure DevOps– check it out!

Awesome, with a successful Terraform run, tamopssaarm has been created as below!

ARM templates can be ran using Terraform

Deploying ARM templates using Terraform is not something I do recommend creating daily but as I mentioned at the start of my blog post Terraform resource(s) may not be available just yet so having this in your Terraform toolbox is very benefical!

Hopefully this blog post will assist you if you ever have the need or requirement to run an ARM template using Terraform!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s