Twitter Facebook Google+ LinkedIn
Sun, Nov 05, 2017

Using Terraform to Provision your Azure Environment

Terraform provides a nice interface from the command line through the Azure GO SDK to Azure itself. In order to accomplish this, we need to utilize the AzureRM provider for Terraform. When you have a file that uses an AzureRM configuration, Terraform will automatically download the AzureRM provider when calling terraform init (see below).

One important difference between Azure Resource Management templates (ARM Templates) and Terraform is that the “state” of your environment is held locally in the same directory as the file. One other unique difference is that ARM Templates utilize a “CreateOrUpdate” method of creating azure resources, while Terraform utilizes a “merge” model to ensure that what you have locally is deployed out in Azure (or rather in sync).

To install Terraform using the Windows Subsystem for Linux just use the following commands.

wget -O
chmod +x terraform
sudo mv terraform /usr/local/bin/terraform

Terraform uses a language called Hashicorp Configuration Language - HCL. This language has it’s own configuration but allows for variables and conditionals.

For instance, if we wanted to create an Azure Container Instance service, we could place the following code inside a file named

resource "azurerm_resource_group" "aci-rg" {
  name     = "aci-test"
  location = "west us"

resource "azurerm_storage_account" "aci-sa" {
  name                = "acistorageacct"
  resource_group_name = "${}"
  location            = "${azurerm_resource_group.aci-rg.location}"
  account_type        = "Standard_LRS"

resource "azurerm_storage_share" "aci-share" {
  name = "aci-test-share"

  resource_group_name  = "${}"
  storage_account_name = "${}"

  quota = 50

resource "azurerm_container_group" "aci-helloworld" {
  name                = "aci-hw"
  location            = "${azurerm_resource_group.aci-rg.location}"
  resource_group_name = "${}"
  ip_address_type     = "public"
  os_type             = "linux"

  container {
    name = "hw"
    image = "seanmckenna/aci-hellofiles"
    cpu ="0.5"
    memory =  "1.5"
    port = "80"

    environment_variables {

    command = "/bin/bash -c '/path to/'"

    volume {
      name = "logs"
      mount_path = "/aci/logs"
      read_only = false
      share_name = "${}"
      storage_account_name = "${}"
      storage_account_key = "${azurerm_storage_account.aci-sa.primary_access_key}"

  container {
    name   = "sidecar"
    image  = "microsoft/aci-tutorial-sidecar"
    cpu    = "0.5"
    memory = "1.5"

  tags {
    environment = "testing"

Example Credit:

Then in that same directory, we would run the following commands.

terraform init
terraform plan
terraform apply

The init command downloads the required files and providers to needed for terraform to work. The plan command compiles the file and shows any errors in validating the script(s). The apply command executes the script against Azure.

Once we run apply on any of the example scripts we should be able to go to the Azure Portal and see our resource group with resources in it.

The real magic comes in after you run apply once. Now any changes to the file will add and delete resources as needed to bring Azure up to sync with the file.

Twitter Facebook Google+ LinkedIn