Terraform - create storage_container in Azure for backups with azcopy on Linux

Posted on Mon 04 May 2020 by Pavlo Khmel

File with Azure environment variables setenv.sh

#!/bin/sh
export ARM_SUBSCRIPTION_ID="1113bbd1-cccc-aaaa-7777-e7254290dccc"
export ARM_CLIENT_ID="888469e3-5555-8888-2222-ca0dd6989fff"
export ARM_CLIENT_SECRET="@xe9_QQQQ4kN.7AxxxlwEY/zzz?U]81Y"
export ARM_TENANT_ID="444959f4-1111-2222-3333-aefce3d1eeee"
export ARM_ENVIRONMENT=public

Terraform file main.tf

#############################################################################
# VARIABLES
#############################################################################

variable "resource_group_name" {
  type    = string
  default = "aaxx22yy3zzbackup"
}

variable "location" {
  type    = string
  default = "West Europe"
}

#############################################################################
# PROVIDERS
#############################################################################

provider "azurerm" {
  version = "=2.6.0"
  features {}
}

#############################################################################
# RESOURCES
#############################################################################

resource "azurerm_resource_group" "aaxx22yy3zzrg" {
  name     = var.resource_group_name
  location = var.location

  tags = {
    environment = "aaxx22yy3zz"
  }
}


resource "azurerm_storage_account" "aaxx22yy3zzsa" {
  name                     = var.resource_group_name
  resource_group_name      = var.resource_group_name
  location                 = var.location
  account_tier             = "Standard"
  account_replication_type = "RAGRS"
  account_kind             = "BlobStorage"
  access_tier              = "Cool"
  enable_https_traffic_only = true

  tags = {
    environment = "aaxx22yy3zz"
  }

  depends_on = [azurerm_resource_group.aaxx22yy3zzrg]
}


resource "azurerm_storage_container" "aaxx22yy3zzco" {
  name                  = var.resource_group_name
  storage_account_name  = azurerm_storage_account.aaxx22yy3zzsa.name
  container_access_type = "private"
  depends_on = [azurerm_storage_account.aaxx22yy3zzsa]
}

#############################################################################
# Data
#############################################################################

data "azurerm_storage_account_blob_container_sas" "aaxx22yy3zzsas" {
  connection_string = azurerm_storage_account.aaxx22yy3zzsa.primary_connection_string
  container_name    = azurerm_storage_container.aaxx22yy3zzco.name

  start  = "2020-05-03T17:18:00Z"
  expiry = "2020-08-20T17:18:00Z"

  permissions {
    read   = true
    add    = true
    create = true
    write  = true
    delete = true
    list   = true
  }

}

#############################################################################
# OUTPUTS
#############################################################################

output "url_for_azcopy" {
  value = "${azurerm_storage_container.aaxx22yy3zzco.id}${data.azurerm_storage_account_blob_container_sas.aaxx22yy3zzsas.sas}"
}

Create storage:

# source setenv.sh 
# terraform init
# terraform plan
# terraform apply
. . .
. . .

Outputs:

url_for_azcopy = https://aaxx22yy3zzbackup.blob.core.windows.net/aaxx22yy3zzbackup?sv=2018-11-09&sr=c&st=2020-05-03T17%3A18%3A00Z&se=2020-08-20T17%3A18%3A00Z&sp=racwdl&spr=https&sig=IZkMb%2Bn%22222221Jdh2aJKFSDaC9anArJvHO4k111111%3D

Copy file:

# ./azcopy copy "/root/azcopy_v10.tar.gz" "https://aaxx22yy3zzbackup.blob.core.windows.net/aaxx22yy3zzbackup?sv=2018-11-09&sr=c&st=2020-05-03T17%3A18%3A00Z&se=2020-08-20T17%3A18%3A00Z&sp=racwdl&spr=https&sig=IZkMb%2Bn%22222221Jdh2aJKFSDaC9anArJvHO4k111111%3D"
. . .
. . .
Final Job Status: Completed