top of page

Terraform Module to Create Azure Storage Account

Updated: May 21, 2023

An Azure storage account contains all of your Azure Storage data objects, including blobs, file shares, queues, tables, and disks. The storage account provides a unique namespace for your Azure Storage data that's accessible from anywhere in the world over HTTP or HTTPS. Data in your storage account is durable and highly available, secure, and massively scalable.


Terraform Storage Account Module


# main.tf

resource "azurerm_storage_account" "storage_account" {
  count                = length(var.storage_accounts)
  name                 = var.storage_accounts[count.index]["name"]
  resource_group_name  = var.storage_accounts[count.index]["resource_group_name"]
  location             = var.storage_accounts[count.index]["location"]
  account_kind         = var.storage_accounts[count.index]["account_kind"]
  account_tier         = var.storage_accounts[count.index]["account_tier"]
  account_replication  = var.storage_accounts[count.index]["account_replication"]
  enable_https_traffic = var.storage_accounts[count.index]["enable_https_traffic"]

  tags = var.storage_accounts[count.index]["tags"]
}

output "storage_account_ids" {
  value = [for s in azurerm_storage_account.storage_account : s.id]
}


Terraform Script By Calling the Azure Storage Module


# main.tf

module "multiple_storage_accounts" {
  source = "./storage_account_module"
  
  storage_accounts = [
    {
      name                 = "storageaccount1"
      resource_group_name  = "myresourcegroup1"
      location             = "eastus"
      account_kind         = "StorageV2"
      account_tier         = "Standard"
      account_replication  = "LRS"
      enable_https_traffic = true
      tags                 = {
        environment = "dev"
      }
    },
    {
      name                 = "storageaccount2"
      resource_group_name  = "myresourcegroup2"
      location             = "westus"
      account_kind         = "StorageV2"
      account_tier         = "Premium"
      account_replication  = "ZRS"
      enable_https_traffic = true
      tags                 = {
        environment = "prod"
      }
    }
  ]
}

output "storage_account_ids" {
  value = module.multiple_storage_accounts.storage_account_ids
}

In the example above, the module is invoked by specifying the source path and providing the storage_accounts variable with a list of maps containing the details of each Storage Account to be created. The module will create the Storage Accounts with the specified names, resource group names, locations, account kind, account tier, account replication, enable HTTPS traffic flag, and tags.


To use the output, you can reference module.multiple_storage_accounts.storage_account_ids, which will return a list of the created Storage Account IDs.


Adjust the values according to your requirements and Azure environment.





Is this post is useful?

  • Yes

  • No

  • Can be improved?




bottom of page