Terraform On Azure - Creating A Storage Account, Blob Container And Queue

Introduction

Azure Storage Account is a widely used storage service. It is a convenient data store for any kind of application whether running on Azure or on-premises or any other cloud. It offers Blobs, Files, Queues, and Table services. Furthermore, the Azure Blob service is so robust that it is used as Generation 2 Data Lake in the Azure environment. In this article, we will learn how to create an Azure Storage Account with a Blob service and a Queue service.

In the previous articles we learned the basics of Terraform, we created an Azure WebApp using Terraform, and then we created Azure Virtual Machine. The following are the links to the previous articles.

Create Azure Storage Account using Terraform 

Let us create a Resource Group and inside it, we can create an Azure Storage Account. Then we can add a Blob service and a Queue service to the Storage Account.

Log in to the Azure portal. Let us use Azure Cloud Shell to create resources using Terraform. Azure Cloud Shell has Terraform installed and you need not do any installation or configuration to work with Terraform. 

Terraform On Azure

Once the Azure Cloud Shell opens up, select Bash. Let us start creating scripts to create an Azure Storage Account. We can use a nano editor to create the Infrastructure as a Code script for the Storage Account using Terraform. 

Terraform On Azure

Execute the following command to open a nano editor and create a file named myterraformscript.tf.

nano myterraformscript.tf

Add the following code to the nano editor. This would create a Resource Group. Replace {ResourceGroup} with the name of your Resource Group that you are planning to create.

terraform {  
  required_providers {  
    azurerm = {  
      source = "hashicorp/azurerm"  
    }  
  }  
}  
provider "azurerm" {  
  features {}  
}  
resource "azurerm_resource_group" "{ResourceGroup}" {  
  name = "{ResourceGroup}"  
  location = "eastus"  
} 

Add the following code in nano editor to create a Storage Account. Replace {ResourceGroup} with the name of your Resource Group and {StorageAccount} with the name of your Storage Account. 

resource "azurerm_storage_account" "{StorageAccount}" {
  name                     = "{StorageAccount}"
  resource_group_name      = azurerm_resource_group.{ResourceGroup}.name
  location                 = azurerm_resource_group.{ResourceGroup}.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

Add the following code to create a Blob service inside the Storage Account. Replace {ResourceGroup} with the name of your Resource Group, {StorageAccount} with the name of your Storage Account, and {Blob} with the name of your Blob container.

resource "azurerm_storage_container" "{Blob}" {
  name                  = "{Blob}"
  storage_account_name  = azurerm_storage_account.{StorageAccount}.name
  container_access_type = "private"
}

Add the following code to create a Queue inside the Storage Account. Replace {ResourceGroup} with the name of your Resource Group, {StorageAccount} with the name of your Storage Account and {Queue} with the name of your Queue.

resource "azurerm_storage_queue" "{Queue}" {
  name                 = "{Queue}"
  storage_account_name = azurerm_storage_account.{StorageAccount}.name
}

The following is the script file that you created. You can refer to the attached script file and try out the sample.

terraform {  
  required_providers {  
    azurerm = {  
      source = "hashicorp/azurerm"  
    }  
  }  
}  
provider "azurerm" {  
  features {}  
}  
resource "azurerm_resource_group" "{ResourceGroup}" {  
  name = "{ResourceGroup}"  
  location = "eastus"  
} 

resource "azurerm_storage_account" "{StorageAccount}" {
  name                     = "{StorageAccount}"
  resource_group_name      = azurerm_resource_group.{ResourceGroup}.name
  location                 = azurerm_resource_group.{ResourceGroup}.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

resource "azurerm_storage_container" "{Blob}" {
  name                  = "{Blob}"
  storage_account_name  = azurerm_storage_account.{StorageAccount}.name
  container_access_type = "private"
}

resource "azurerm_storage_queue" "{Queue}" {
  name                 = "{Queue}"
  storage_account_name = azurerm_storage_account.{StorageAccount}.name
}

Run the following command to initiate Terraform. This would fetch all dependencies needed to execute the Terraform script.

terraform init

Now let us create an execution plan for Terraform. Let us provide the name of the execution plan in the out parameter.

terraform plan -out mysa.tfplan

Execute the execution plan using the following command. The Storage Account gets created.

terraform apply "mysa.tfplan"

Conclusion

In this article, we learned how to create an Azure Storage Account. In the next article, we will learn how to create an Azure Function using Terraform scripts. 


Similar Articles