Open In App

How To Create Azure Storage Account Using Terraform ?

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:

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:

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.

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:

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"
}

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.

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.

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.

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.

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

Advantages of Create Azure Storage Account using Terraform

Creating an Azure Storage Account using Terraform offers several advantages:

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:

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.


Article Tags :