Skip to content

Reusable private storage foundation

Level: Intermediate

Overview and purpose

Create a TLS-only StorageV2 account with public networking, shared keys, and public containers disabled.

Prerequisites and dependencies

Offline tests mock AzureRM. Live use requires a globally unique storage name and a real Azure root/provider configuration.

Inputs and outputs

name validates Azure lowercase alphanumeric length. location and resource_group_name are supplied by the caller.

Source and usage

Read main.tf and the focused tests. Use this directory as the working directory for the commands below. Azure modules are child modules: use examples/03-azure-foundation as the live deployment entry point rather than applying a module without its caller.

Validation and optional execution

The commands use Bash syntax (Azure Cloud Shell supports it). Plan-only tests never create Azure resources; Azure tests use mock_provider. init may download provider plugins from the Terraform Registry. For Azure live commands, authenticate, select the intended subscription, and set ARM_SUBSCRIPTION_ID and your own STORAGE_ACCOUNT_NAME first. No live Azure apply/destroy is part of CI.

terraform init -backend=false
terraform validate
terraform test

Inspect every plan before apply. A saved plan is sensitive and can contain values from configuration/state; do not commit or publish it.

Expected results and verification

The mocked plan verifies security defaults. A real account needs private endpoints, private DNS, and data roles before clients can access its data.

Read terraform output and the plan's resource addresses. A mocked assertion only checks configuration behavior, not provider APIs, credentials, cost, regional availability, or application runtime behavior.

Cleanup

For local data examples, terraform destroy removes the exercise's managed records. For Azure, use the root configuration's reviewed destroy plan with the same state, credentials, and input values. Confirm the resources are gone. Do not delete the state file as a substitute for destroying resources.

After confirming destruction, delete ignored local .terraform/, plan, and state files if you no longer need them. Keep committed provider lock files. Shared or retained resources may need separate approved cleanup.

Cost, security, and limitations

Storage capacity, operations, private endpoints, and traffic can be charged. No containers, endpoint, DNS, or role grants are created here. Provider state can contain sensitive resource metadata; protect it.

These are educational examples, not certified production modules. For additional Azure services, use the companion AzureTerraformTemplates library after reviewing its individual prerequisites, provider constraints, and security settings.

Complete Terraform source

modules/azure-storage/main.tf
terraform {
  required_version = ">= 1.7, < 2.0"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">= 5.14.0, < 6.0"
    }
  }
}

variable "name" {
  description = "Globally unique lowercase alphanumeric storage account name."
  type        = string

  validation {
    condition     = can(regex("^[a-z0-9]{3,24}$", var.name))
    error_message = "Use 3-24 lowercase letters and digits."
  }
}

variable "resource_group_name" {
  description = "Existing resource group name."
  type        = string
}

variable "location" {
  description = "Approved Azure region."
  type        = string
}

resource "azurerm_storage_account" "this" {
  name                            = var.name
  resource_group_name             = var.resource_group_name
  location                        = var.location
  account_tier                    = "Standard"
  account_replication_type        = "LRS"
  account_kind                    = "StorageV2"
  min_tls_version                 = "TLS1_2"
  https_traffic_only_enabled      = true
  allow_nested_items_to_be_public = false
  shared_access_key_enabled       = false
  public_network_access           = "Disabled"
  default_to_oauth_authentication = true
}

output "id" {
  description = "Storage account resource ID."
  value       = azurerm_storage_account.this.id
}

output "name" {
  description = "Storage account name, not an access credential."
  value       = azurerm_storage_account.this.name
}