Back to Blog

Managing Azure Storage Accounts with Terraform

Complete tutorial about azurerm_storage_account in Terraform. Learn storage accounts, containers, access tiers, replication.

Managing Azure Storage Accounts with Terraform

Managing Azure Storage Accounts with Terraform

Introduction

Azure Storage accounts are essential resources in Microsoft Azure that provide a unique namespace for storing various types of data objects, such as blobs, files, queues, and tables. They ensure your data is durable, highly available, secure, and massively scalable. The capability to manage these resources using Infrastructure as Code (IaC) tools like Terraform not only automates deployment but also enhances collaboration and consistency in your infrastructure management. 🚀

In this tutorial, we will explore how to create and manage Azure Storage accounts using Terraform, including configuring containers, access tiers, and replication strategies. You will learn how to utilize the azurerm_storage_account resource effectively, enabling you to implement best practices in your infrastructure setup. This tutorial will cover practical examples from basic to advanced configurations, helping you understand how to leverage Azure Storage for your applications.

Prerequisites

Before you begin, ensure you have the following prerequisites:

  1. Terraform CLI installed on your machine. Download Terraform.
  2. An active Azure subscription. You can create one here.
  3. Azure CLI installed for managing Azure resources. Install Azure CLI.
  4. A service principal for authentication. You can create one using:
    az ad sp create-for-rbac --name "myServicePrincipal" --role Contributor --scopes /subscriptions/{subscription-id}
    

Fundamental Concepts

Understanding some key concepts is crucial for working with Azure Storage accounts:

  • Storage Account: A container for all storage objects (blobs, files, queues, and tables).
  • Blob Storage: A service that stores unstructured data in the cloud as objects/blobs.
  • Containers: Logical partitions within a storage account to organize blobs.
  • Access Tiers: Define the storage costs based on how frequently data is accessed (Hot, Cool, Archive).
  • Replication: Determines how data is replicated across regions for durability (LRS, GRS, RA-GRS, etc.).

Resource Dependencies: When deploying Azure resources, dependencies may exist between resources. Terraform manages these dependencies automatically.

State Management: Terraform uses a state file to keep track of resources it manages. Ensure you secure this state file, especially if it contains sensitive information.

Resource Syntax

The azurerm_storage_account resource has the following basic syntax in HCL (HashiCorp Configuration Language):

resource "azurerm_storage_account" "example" {
  name                     = "examplestoracc"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
  enable_https_traffic_only = true
  
  # Optional fields
  access_tier             = "Hot" # or "Cool" or "Archive"
  min_tls_version         = "TLS1_2"
}

Arguments Table

Argument Type Description
name string The name of the storage account (must be unique).
resource_group_name string The name of the resource group in which to create the storage account.
location string The Azure region where the storage account will be created.
account_tier string The performance tier (Standard or Premium).
account_replication_type string The replication strategy (LRS, GRS, RA-GRS, etc.).
enable_https_traffic_only bool Specifies whether to allow only HTTPS traffic.
access_tier string The default access tier for the Blob service (Optional).
min_tls_version string The minimum TLS version required for secure connections.

Practical Examples

Example 1: Basic Storage Account Creation

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "East US"
}

resource "azurerm_storage_account" "example" {
  name                     = "examplestoracc"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

Example 2: Storage Account with HTTPS Only

resource "azurerm_storage_account" "example_https" {
  name                     = "examplestoracc2"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
  enable_https_traffic_only = true
}

Example 3: Storage Account with Access Tier Configuration

resource "azurerm_storage_account" "example_tier" {
  name                     = "examplestoracc3"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
  access_tier             = "Cool"
}

Example 4: Creating Containers in the Storage Account

resource "azurerm_storage_container" "example_container" {
  name                  = "example-container"
  storage_account_name   = azurerm_storage_account.example.name
  container_access_type = "private"  # Options: private, blob, container
}

Example 5: Advanced Storage Account with Replication

resource "azurerm_storage_account" "example_advanced" {
  name                     = "examplestoracc4"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "GRS"    # Geo-redundant storage
}

Example 6: Configuring a Storage Account with Data Lake Settings

resource "azurerm_storage_account" "example_datalake" {
  name                     = "examplestoracc5"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
  enable_hierarchical_namespace = true
}

Example 7: Adding Custom Domain to Storage Account

resource "azurerm_storage_account" "example_custom_domain" {
  name                     = "examplestoracc6"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
  
  custom_domain {
    name = "www.example.com"
  }
}

Example 8: Outputting Storage Account Information

output "storage_account_name" {
  value = azurerm_storage_account.example.name
}

output "storage_account_primary_access_key" {
  value = azurerm_storage_account.example.primary_access_key
}

Real-World Use Cases

  1. Backup and Disaster Recovery: Azure Storage accounts can be configured with geo-redundancy to ensure data is backed up in another region, providing disaster recovery capabilities.

  2. Hosting Static Websites: Use Azure Blob Storage with a static website configuration to host static content like HTML pages, CSS, and JavaScript.

  3. Data Lake Storage: Azure Storage accounts can be used with Data Lake Storage features, allowing organizations to store large amounts of data for analytics and big data solutions.

Best Practices

  1. Use Unique Names: Ensure your storage account names are unique across Azure to avoid deployment failure.
  2. Secure State Files: Store Terraform state files in a secure manner, possibly in an Azure Storage account with restricted access.
  3. Use Variables for Configurations: Utilize variables for frequently changed configurations like location and replication types.
  4. Modularize Your Code: Organize your Terraform code into reusable modules for better maintainability.
  5. Regularly Audit Access: Regularly review and audit access to your storage accounts to ensure security compliance.

Common Errors

  1. Storage account name already exists:

    • Cause: The specified name for the storage account is not unique.
    • Solution: Choose a different name that meets Azure's naming conventions.
  2. Invalid replication type:

    • Cause: The specified replication type is not supported in the selected region.
    • Solution: Check the Azure documentation for supported replication types in your chosen region.
  3. Insufficient permissions:

    • Cause: The service principal does not have enough permissions to create resources in the specified resource group.
    • Solution: Update the role assignments for the service principal to include necessary permissions.
  4. Invalid access tier for the selected account type:

    • Cause: The access tier specified is not valid for the current storage account configuration.
    • Solution: Review the available access tiers for the storage account type and adjust accordingly.

Related Resources

Resource Name Description
azurerm_storage_account Terraform resource for managing Azure Storage Accounts.
azurerm_storage_container Terraform resource for managing containers in Azure Storage.
Azure Storage Documentation Official Azure documentation for various storage services.
Terraform Modules Explore Terraform modules for Azure Storage and other resources.

Complete Infrastructure Script

Here’s a complete Terraform script for creating an Azure Storage account along with a container:

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "East US"
}

resource "azurerm_storage_account" "example" {
  name                     = "examplestoracc"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
  enable_https_traffic_only = true
}

resource "azurerm_storage_container" "example_container" {
  name                  = "example-container"
  storage_account_name   = azurerm_storage_account.example.name
  container_access_type = "private"
}

output "storage_account_name" {
  value = azurerm_storage_account.example.name
}

output "storage_container_name" {
  value = azurerm_storage_container.example_container.name
}

Conclusion

In this tutorial, you learned how to manage Azure Storage accounts using Terraform, from basic setup to advanced configurations. You now have the knowledge necessary to automate the deployment of Azure Storage resources using Infrastructure as Code. As you progress, consider exploring other Azure services and integrating them into your infrastructure management strategy.

References

Feel free to reach out if you have any questions or need further assistance! 💡