Deploy Azure Resources with Terraform – Storage Account Containers

Well first we need to understand what is terraform and what is used for?

Terraform is an open-source tool designed for automating and managing infrastructure as code (IaC). Created by HashiCorp, it allows developers and DevOps teams to define, provision, and manage cloud and on-premises resources in a declarative, code-based format.

This includes servers, storage, networking, databases, and more, across multiple providers like AWS, Azure, Google Cloud, and even on-premises systems.

How Terraform Works

Terraform operates in three main steps:

Write: Define your infrastructure in .tf files using HCL.

Plan: Terraform generates an execution plan, showing you the changes it will make to reach the desired state.

Apply: Terraform applies the changes, creating or modifying resources as needed.

Terraform is a robust, flexible tool that helps you define, manage, and automate infrastructure—empowering teams to handle complex setups with ease and precision.

But let’s get back to how can we use it to create blob containers in a storage account!

Well, this is a POC, and you should follow it to the extent of your needs, and scale from there if necessary.

Setting Up Terraform in Visual Studio Code

To start this POC, make sure you have Visual Studio Code installed on your PC and:

  • You are logged in with your credentials.
  • You have an Azure Subscription
  • You have a Storage Account (in your Azure Subscription)
  • Next create a folder on your disk C:\ named terraform
  • C:\terraform
  • Move the downloaded Terraform executable (.exe file) into this folder.
  • Go to Control Panel and search for System, then navigate to Advanced System Settings > Environment Variables
  • Under System Variables, find and double-click on Path.
  • Next add a new environment variable with the path to your terraform folder:
    • C:\terraform

Click OK to save and close all windows.

  • Now you need to also download and install Azure CLI from this link:

https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli

  • Open Visual Studio Code and install the Terraform extensions. Consider installing any other recommended extensions for this project.

Next, it is time to create a new project and to do so, create a folder on your pc for this terraform project, with the project folder inside, as it follows:

  • In Visual Studio Code, go to File > Open Folder and select your project folder.
  • This will open it in the VS Code workspace.
  • In the Explorer panel in VS Code, create a new file named main.tf. This file will define your Terraform configuration.

We are not going to do CI/CD but we can create the files preparing terrain for that anyway.

So for now just copy and paste the following code into main.tf file.

provider "azurerm" {
  features {}
  subscription_id = var.subscription_id
}

# Reference the existing Resource Group using a variable
data "azurerm_resource_group" "example" {
  name = var.resource_group_name
}

# Reference the existing Storage Account using a variable
data "azurerm_storage_account" "existing_account" {
  name                = var.storage_account_name
  resource_group_name = data.azurerm_resource_group.example.name
}

# Create a new container in the existing storage account
resource "azurerm_storage_container" "variable_container_name" {
  name                  = var.container_name  # Use variable for container name
  storage_account_name  = data.azurerm_storage_account.existing_account.name
  container_access_type = "private"  # Adjust access level if needed
}

  • This code uses variables instead of hardcoded values to create a blob container in an existing storage account.
  • And to continue with this we need to create a variables.tf file indicating which of these fields are the variables
  • Create a new file named variables.tf in the project folder, and paste in the following code to declare your variables:
variable "subscription_id" {}
variable "resource_group_name" {}
variable "storage_account_name" {}
variable "container_name" {}
  • Next set the variable values, to do that create another file named terraform.tfvars and add your actual values:
subscription_id = "xxxxx-xxxx-xxxx"                   # Your Azure subscription ID
resource_group_name = "RG-XX-XX-POC"       # The existing resource group name
storage_account_name = "statrainingpoc"       # The existing storage account name
container_name = "demo-terraform-poc"        # The name for the new container
  • Now open your Terminal in VS code.
  • And in here the write cd followed by the path of your project folder:
  • cd C:\Users\YourUserName\Desktop\Terraform-POC\my-terraform-project
  • Press enter to run the command
  • Run the following command to initialize the project: terraform init
  • Press enter to run the command
  • You can now use terraform plan to see what changes will be applied or terraform apply to make them. When prompted, type “yes” to confirm the apply command.
  • After completion, check your storage account to verify that the container demo-terraform-poc was created.
  • If for some reason you cannot create the container, on PowerShell run the command: az login and log with your credentials
  • Ensure the correct subscription is selected and retry the steps in VS Code.

Hope you find this helpful! If you enjoyed the content or found it useful and wish to support our efforts to create more, you can contribute towards purchasing a Star Wars Lego for Sandro’s son!

Author: Luis Rigueira

Luis Rigueira is a Enterprise Integration Consultant at DevScope

Leave a Reply

Your email address will not be published. Required fields are marked *

turbo360

Back to Top