TerraformPilot

DevOps

Fix Terraform Error - Azure Storage Account Name Already Taken

Fix 'storage account name already taken' errors in Terraform for Azure. Handle globally unique naming with random suffixes and check name availability.

LLuca Berton1 min read

Quick Answer

#

Azure Storage Account names are globally unique across all Azure customers. Your chosen name is taken by someone else (or by you in another subscription). Use a random suffix or hash to generate unique names.

The Error

#
Error: creating Storage Account "mystorageaccount": 
The storage account named mystorageaccount is already taken

What Causes This

#
  • Storage account names are globally unique across all of Azure (not just your subscription)
  • Names must be 3-24 characters, lowercase letters and numbers only (no hyphens, underscores, or uppercase)
  • Someone else already has that name

How to Fix It

#

Solution 1: Use Random Suffix

#
resource "random_id" "storage" {
  byte_length = 4
}
 
resource "azurerm_storage_account" "main" {
  name                     = "myapp${random_id.storage.hex}"  # e.g., "myapp1a2b3c4d"
  resource_group_name      = azurerm_resource_group.main.name
  location                 = azurerm_resource_group.main.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

Solution 2: Check Availability First

#
# Check if a name is available
az storage account check-name --name mystorageaccount
# Returns: { "nameAvailable": true/false, "reason": "..." }

Solution 3: Naming Convention with Project + Environment

#
locals {
  # Remove non-alphanumeric chars, lowercase, truncate
  storage_prefix = substr(lower(replace("${var.project}${var.env}", "/[^a-z0-9]/", "")), 0, 16)
}
 
resource "random_id" "storage" {
  byte_length = 4
}
 
resource "azurerm_storage_account" "main" {
  name = "${local.storage_prefix}${random_id.storage.hex}"
  # Max 24 chars: 16 prefix + 8 hex = 24 ✓
}

Storage Account Naming Rules

#
RuleRequirement
Length3-24 characters
CharactersLowercase letters and numbers only
UniquenessGlobally unique across all Azure
No hyphensmy-storage is invalid
No underscoresmy_storage is invalid
No uppercaseMyStorage is invalid

Troubleshooting Checklist

#
  1. ✅ Is the name globally unique? (az storage account check-name)
  2. ✅ Is it 3-24 characters, lowercase alphanumeric only?
  3. ✅ Did you use a random suffix for uniqueness?
  4. ✅ Do you have a naming convention that prevents collisions?
#

Conclusion

#

Azure Storage Account names are globally unique and have strict character rules. Always use a random suffix (like random_id.hex) to guarantee uniqueness. Check availability with az storage account check-name before choosing a name.

#Terraform#Troubleshooting#DevOps#Error Fix#Azure

Share this article