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