You can use Terraform to create a secure and managed Azure Blob container in just a few steps. This is done by defining a resource block in your Terraform configuration file.
Azure Blob containers are a key component of Azure Storage, providing a highly scalable and secure way to store and manage unstructured data.
To create a new Azure Blob container, you need to specify the name of the container, the storage account name, and the resource group name.
The storage account name should be unique across all of Azure, so choose a name that is descriptive and easy to remember.
Azure Blob containers can be configured to use different access tiers, such as hot, cool, or archive, depending on the type of data being stored.
This allows you to optimize storage costs and performance based on the specific needs of your application.
Getting Started
To get started with Terraform and Azure Blob Container, you'll need to have an Azure subscription and the Terraform CLI installed on your machine.
First, create a new Azure resource group using the Azure CLI or the Azure portal. This will be the parent resource group for your blob container.
You'll also need to create a new storage account in the Azure portal or using the Azure CLI. This will be used to store your blob container.
Next, create a new Terraform configuration file (`.tf` file) in your project directory. This file will contain the Terraform code for your Azure resources.
In the Terraform configuration file, you'll need to define the Azure provider and configure it with your Azure subscription credentials.
Make sure to replace the placeholders in the Terraform configuration file with your actual Azure subscription ID and storage account name.
Once you've completed these steps, you'll be ready to start provisioning your Azure Blob Container using Terraform.
Azure Storage Basics
Azure Storage Accounts come in different kinds, and the most common one is StorageV2. This kind can be set using the account_kind argument.
You can create an Azure storage account with a set of containers and access levels using Terraform. The access level can be set for each container.
To create a storage account, you need to define the kind of account, and the valid options are BlobStorage, BlockBlobStorage, FileStorage, Storage, and StorageV2.
Terraform automatically computes the appropriate values for account_tier and account_replication_type based on the account kind.
The static_website feature can only be set when the account kind is set to StorageV2.
Security and Access
The Azure storage firewall provides access control for the public endpoints of the storage account, allowing you to block all access through the public endpoint when using private endpoints.
To block all access through the public endpoint, you can use network policies in the storage firewall configuration. This also enables select trusted Azure platform services to access the storage account securely.
You can also configure managed identities to access Azure Storage, which provides Azure services with an automatically managed identity in Azure Active Directory. This allows you to authenticate to any service that supports Azure AD authentication without having credentials in your code.
There are two types of managed identities: System-assigned and User-assigned. Regardless of the type chosen, a managed identity is a service principal of a special type that may only be used with Azure resources.
Configure Azure Storage Firewalls and Virtual Networks
To configure Azure Storage firewalls and virtual networks, you should first understand that the Azure storage firewall provides access control for the public endpoints of the storage account.
The default action of the storage firewall is set to Allow, so if no network rules match, access is granted.
Using network policies, you can block all access through the public endpoint when using private endpoints.
A subnet_ids or ip_rules can be added to the network_rules block to allow a request that is not from Azure Services.
This configuration also enables select trusted Azure platform services to access the storage account securely.
Configure Managed Identities
You can configure managed identities to access Azure Storage, which provides an automatically managed identity in Azure Active Directory.
There are two types of managed identities: system-assigned and user-assigned.
System-assigned managed identities are tied to the lifecycle of the service instance and are automatically deleted when the resource is deleted. Only the Azure resource can use this identity to request tokens from Azure AD.
User-assigned managed identities are standalone Azure resources, with the identity managed separately from the resources that use it.
A managed identity is a service principal of a special type that may only be used with Azure resources.
Storage Management
Storage management is a crucial aspect of Terraform Azure Blob container management. You can create an Azure storage account with a set of containers and access levels using the Azure Storage Account Terraform Module.
The module supports various storage account kinds, including StorageV2, BlobStorage, BlockBlobStorage, FileStorage, Storage, and StorageV2. If you want to create a StorageV2 account, you can set the account_kind argument to "StorageV2". This will automatically compute the appropriate values for account_tier and account_replication_type.
You can also manage the Azure Blob storage lifecycle using the Azure Blob storage lifecycle management policy. This policy lets you transition blobs to a cooler storage tier, delete blobs at the end of their lifecycles, define rules to be run once per day, and apply rules to containers or a subset of blobs.
Here are the valid options for account kind:
- BlobStorage
- BlockBlobStorage
- FileStorage
- Storage
- StorageV2
Soft Delete for Blobs
Soft delete for Blobs is a safety net that protects your blob data from accidental or erroneous modification or deletion. It allows you to recover deleted blobs within a specified retention period.
You can set the retention period for blob soft delete using the blob_soft_delete_retention_days argument, which must be between 1 and 365 days. The default retention period is 7 days.
Enabling blob soft delete is essential for protecting individual blobs in a container, as it cannot be used to restore a deleted blob within a container.
Manage Azure Storage Lifecycle
Managing Azure Storage Lifecycle is a crucial aspect of storage management. You can use Azure Blob storage lifecycle management to transition your data to the appropriate access tiers or expire at the end of the data's lifecycle.
Azure Blob storage lifecycle management offers a rich, rule-based policy for General Purpose v2 (GPv2) accounts, Blob storage accounts, and Premium Block Blob storage accounts. This policy lets you transition blobs to a cooler storage tier, delete blobs at the end of their lifecycles, define rules to be run once per day at the storage account level, and apply rules to containers or a subset of blobs.
To implement storage lifecycle management, you must specify network rules, which includes either ip_rules or subnet_ids, and set default_action to Deny.
Here are the possible lifecycle management policy actions:
To define the lifecycle policy, you can use the following objects: prefix_match, tier_to_cool_after_days, tier_to_archive_after_days, delete_after_days, and snapshot_delete_after_days. These objects must have keys, and their default values are specified in the article.
Sources
- https://www.techielass.com/store-terraform-state-in-azure-blob-storage/
- https://trstringer.com/azure-blob-storage-remote-terraform-state/
- https://github.com/kumarvna/terraform-azurerm-storage
- https://www.linkedin.com/posts/build5nines_terraform-deploy-azure-storage-account-and-activity-7221491604065267712-Qen_
- https://lgulliver.github.io/deploy-storage-account-static-site-terraform-azure-devops/
Featured Images: pexels.com