Open In App

How To Create Azure Storage Account Using Terraform ?

Last Updated : 27 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Cloud development has drastically altered the previously known reality of data storage and access, presently providing organizations of all sizes with scalable and cost-effective solutions. Microsoft Azure, one of the cloud computing platform’s leading services, provides data storage, which is Azure Storage and is a strong and secure one. While creating and properly configuring storage resources requires time and has a high potential for human error, especially when such processes have a high level of complexity, there is an automatic storage infrastructure provisioning option.

Terraform is an open-source infrastructure as code (IaC) tool that makes cloud provision of resources and management that involves multiple providers easier, such as Azure. In this way, you can verify reliability, reproducibility, and versioning processes for the Azure Storage systems by defining your infrastructure as code. In the following piece, we aim to walk you through the process required to create an Azure Storage Account via Terraform, as a means to digitalize your infrastructure automation and thus simplify the management of your cloud storage.

What is an Azure Storage Account?

An Azure Storage account represents the basic unit of Azure, which can be thought of as a virtual container for storing and accessing different types of data objects in the cloud. It takes on the role of a single place to control and administrate your storage in the cloud. The following are some of the features of the Azure Storage Account:

  • Blob Storage: This service is primarily intended for the storage of unstructured data, including files, images, videos, and backup. Some of the main advantages of Blob Storage is in handling of large amounts of data and has features for high availability and scalability.
  • File Storage: Azure File Storage is a way that enables you to create and use file shares in the cloud just like network file shares do. You are capable of mounting these file shares on Windows, Linux, or macOS systems which offer a familiar environment for the file working.
  • Queue Storage: This service allows storing and getting back the messages to make communication between application components that can be distributed to different machines or platforms reliable.
  • Table Storage: Azure Table Storage is a key-value store that operates as a NoSQL type of store providing speed and cost-effective storage for structured and non-relational data. It will be suitable for storing and querying large semi-structured data volumes.
  • Disk Storage: Azure Disk Storage enables you to develop and operate a persistent disk for a virtual machine in the cloud. These disks can serve the function of primary storage for your virtual machines or as data disks for storing application data.

What is Terraform?

HashiCorp is the progenitor of terraform, which is an open-source IaC (infrastructure as code) tool that has been embraced by the development community. It enables you to create and manage the cloud infrastructure and resources available to you using an imperative language. Terraform is a tool that works across most of the clouds and supports a huge number of cloud providers including Amazon Web Services (AWS), Microsoft Azure, Google Cloud Platform (GCP), and so on

Here are some key features and benefits of Terraform:

  • Infrastructure as Code: By Terraform you can be able to achieve a requirement of defining your entire configuration of the infrastructure with addition of virtual machines, networks, storage, and other resources as a form of a sensitive code. This code can be traced, reused, and implemented in various environments by anyone with an active version history of what is tested
  • Cloud-Agnostic: Terraform automation system is capable of managing the variety of cloud providers and services including its own, which allows you to oversee the infrastructure across several platforms through the same workflow and language.
  • Declarative Configuration: Terraform expresses the desired “layout” of your infrastructure service resource in a configuration language called HashiCorp Configuration Language (HCL). Terraform sketches the needed changes, and then it makes them operate in the most efficient ways.
  • Resource Lifecycle Management: terraform can be used to build, alter, and destroy virtual resources in a controlled way, which allows you to have more control of your environment components within a predictable manner.
  • Dependency Management: Terraform has the capability to resolve dependencies among the resources, which ensures that resources are created in the specified order and hence the scenario of conflict or error is avoided.

Create Azure Storage Account using Terraform

Step 1: Install Terraform

If you haven’t already, download and install Terraform on your local machine from the official Terraform website install terraform. Choose the appropriate package for your operating system.
Terraform

Step 2: Set Up Azure Credentials

Terraform needs authentication credentials to interact with Azure. You can set up these credentials using one of the following methods:

  • Environment Variables: Set the ARM_SUBSCRIPTION_ID, ARM_TENANT_ID, ARM_CLIENT_ID, and ARM_CLIENT_SECRET environment variables with your Azure subscription details.
  • Azure CLI: Run az login to authenticate with the Azure CLI, and Terraform will use the cached credentials.

az -Cli

  • Service Principal: Create a service principal and set the ARM_SUBSCRIPTION_ID, ARM_TENANT_ID, ARM_CLIENT_ID, and ARM_CLIENT_SECRET environment variables with the corresponding values.

Step 3: Create a Terraform Configuration File

Create a new directory for your Terraform configuration and create a file named main.tf. Open the file in a text editor, and add the following code:

# Configure the Azure provider
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 2.46.0"
}
}
}

provider "azurerm" {
features {}
}

# Create a Resource Group
resource "azurerm_resource_group" "rg" {
name = "your-resource-group-name"
location = "your-location"
}

# Create an Azure Storage Account
resource "azurerm_storage_account" "storage_account" {
name = "yourstorageaccountname"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
account tier = "Standard"
account_replication_type = "LRS"
}

Main.tf

Step 4: Initialize Terraform

Open a terminal or command prompt, navigate to the directory containing your main.tf file, and run the following command to initialize Terraform:

terraform init

This command downloads the required Azure provider and other dependencies.

Terraform Init

Step 5: Plan the Infrastructure Changes

Before applying the changes, you can review the planned actions by running:

terraform plan

This command will show you the resources that Terraform plans to create, update, or delete based on your configuration.

Plan

Step 6: Apply the Infrastructure Changes

If you’re satisfied with the planned changes, apply them by running:

terraform apply

Terraform will prompt you to confirm the changes before proceeding. Type yes and press Enter to confirm and create the Azure Storage Account and Resource Group.

Apply

Step 7: Verify the Resources

Once the apply operation completes successfully, you can verify the created resources in the Azure portal or by running the following command:

terraform show

This command will display the current state of the resources managed by Terraform.

That’s it! You’ve successfully created an Azure Storage Account using Terraform. You can now use this storage account for your application’s data storage needs.

terraform show

Step 7: Deleting the resource created

After creation it is necessary to remove the unwanted resource to avoid extra cost from the side of azure, you can run the below command to remove all the resources:

terraform destroy

terraform destroy

Advantages of Create Azure Storage Account using Terraform

Creating an Azure Storage Account using Terraform offers several advantages:

  • Infrastructure as Code: Your Azure Storage Account can now be represented in the Terraform configuration files that can be handled by using version control, shared, are replicated across many different environments. This aids organization in maintaining best practices, consistency, reproducibility and collaboration within their group.
  • Automation and Repeatability: When you Terraform and configure automation, you can easily provision and manage Azure Storage Accounts, without any need of human interventions and consequently mistakes. This provision could be kept repetitive thereby allowing you to manage the identical storage accounts not just in different types of environments but in different deployments too.
  • Declarative Configuration: Terraform is declarative in nature, where you define the desired state of your cloud system including the related Azure “Storage Account together with its configuration. Therefore, Terraform deals with the necessary API calls and procedures for creating or modifying resources at a lower level. Consequently, you can formulate the desired end state instead of diving into the details of every step which is necessary for the implementation.
  • Dependency Management: With terraform, it only takes one command and a bit of code to get all the Azure resources you need (such as Storage Account, Resource Groups or Virtual Networks), and you can be sure that the dependencies between the resources are handled correctly, avoiding resource conflicts or errors.
  • Multi-Cloud and Cross-Platform Support: Terraform provides a choice to users that want to develop their infrastructure by using all of the supported cloud providers, including Azure, AWS, GCP, and others. This creates a cloud-based storage management, giving you a control of your storage accounts in any part of the, consequently, enabling you to manage your entire infrastructure using one common programming frontend.

Disadvantages of Create Azure Storage Account using Terraform

There are many disadvantages of using terraform to create azure storage account or any other particular service some of them are listed below:

  • Learning Curve: Terrraform uses its own domain-specific language (HashiCorp Configuration Language or HCL) and workflow and thus it can be challenging to master them for the first time provided that you may not be familiar with IaC tools. It is probable that grasp of concepts, syntax and best practices of Terraform may take some time and a lot of work.
  • State File Management: terraform employs a state file in order to maintain the status of your infrastructure resources. Proper management and storing of state files should be paid enough attention, because any inconsistency or loss of the state file will cause trouble in the management or updating of resources. A well-structured state file management system is an absolute necessity, particularly for groups or multi-member work.
  • Vendor Lock-in: While Terraform has the flexibility to support cloud services offered by different providers, its configuration files and modules can be classified as provider/version specific. Switching from one provider to another or upgrading to a new version of a provider may be an inconvenient task that sometimes requires complicated refactoring of your Terraform configurations.
  • Resource Limits and API Throttling: Subsequently, working with big deployments or as a resource manager may put you in a position of dealing with resource limits or API throttling practiced by the cloud service provider (Azure, in our case). Geniforming might require some tweaks in order to handle these limits deliberately, which can in turn make the processing slower than expected.
  • Complexity with Large Configurations: As the complexity of your infrastructure increases with the addition of numerous interdependent components like resources and modules, maintenance and management of huge Terraform configurations becomes too taxing. Maintainability of the source code crucially depends on how carefully you carry out the organization, modularization, and documentation of the codebase.

Conclusion

The aforementioned article provided detailed steps on how to create the Azure storage application using Terraform which is a great cloud-based platform for creating infrastructures. Bringing Terraform into the equation will let you to relax in the comfortable surroundings of easy-to-manage creation and operation of cloud-based storage components. In so doing, it won’t let you worry whether your communication with various environments is effective, reliable (repeated), and controlled.

We illustrated, or to make these more explicit…though it would be better to utilize the imperative form… we showed which steps had to be performed in order to install the Terraform, set up the Azure credentials, create the Terraform configuration file, initialize the working directory, plan and apply the infrastructure changes as well as afterwards verify the resources in the following steps. This situation demonstrates how the process driven model not only shows the steps of formation of Azure Storage Account but also keeping the structure clean.

Azure Storage Account Using Terraform – FAQ’s

What does the terraform code look like to configure the redundancy strategy for the storage account?

To define the exact replication type for your Azure Storage Account, use the account_replication_type attribute in the resource block of azurerm_storage_account. Such as, account_replication_type = “LRS” will indicate a storage configuration which is to Locally Redundant Storage (LRS).

Can I build up my Azure Storage accounts using a single Terraform framework?

Yes, you can create numerous Microsoft Azure Storage Accounts by extending the azurerm_storage_account resource blocks into the Terraform configuration file. Each block serves as a separate account for every other one.

What is the proper way configuration should be done to my Terraform storage network rules and Azure Storage Account?

You can set network rules for your Azure storage account by including the network_rules block in the azurerm_storage_account resource among other attributes. In this phrase, you can set the IP address limit, virtual network subnet IDs and other miscellaneous network-related settings.

May I arrange Azure Storage Encryption using Terraform?

Yes, you can have the able_encrytion for your Azure Storage Account whuch you can set the enable_https_traffic_only argument to true in the azurerm_storage_account resource block. And also the service managed identity, enables authorized encrypted configurations by adding it into the identity block.

I would like to know how to export an existing Azure Storage Account and how it is passed to Terraform.

‘Importing an Azure Storage Account into Terraform through the terraform import command, follow by the resource address and the storage account resource ID, can be done.’ For example: terraform import azurerm_storage_account. account.storage_account/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myRG/providers/Microsoft.Storage/storageAccounts/mystorage.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads