Provisioning Azure Storage Blob With Terraform
Table of contents
In this post, I will show you how to deploy an Azure storage blob using Terraform. This will save you time wasted clicking through the Azure portal, which is the best part of Infrastructure-as-code. Difficulty level is 2/10.
So, let’s get started.
Prerequisite:
Authentication with AZ login
Terraform installed
Authentication With AZ Login
This is simply granting your terminal authenticated access to your Azure account, and it takes only two words; see below.
Az login
Once you type that in and hit enter, it launches your web browser, where you can enter your Azure portal credentials if you are not already logged in to Azure on that browser.
You are good to go if you see a screen like this on your browser; otherwise, double-check your login credentials.
Terraform installed
Follow the documentation provided by Hashicorp to install Terraform on your local machine.
Terraform Code
Alright, now to the main deal. You can clone my GitHub repo for this.
Below is the Terraform code that goes into the main.tf file.
#creates a resource group for the services below
resource "azurerm_resource_group" "demo-resource-group" {
name = var.resource_group_name
location = var.resource_group_geolocation
}
#creates a storage account for your containers
resource "azurerm_storage_account" "demo-storage-account-tf" {
name = var.storage_account_name
resource_group_name = azurerm_resource_group.demo-resource-group.name
location = azurerm_resource_group.demo-resource-group.location
account_tier = "Standard"
account_replication_type = "LRS"
}
#creates a container to store your content
resource "azurerm_storage_container" "demo-storage-container-tf" {
name = var.storage_container_name
storage_account_name = azurerm_storage_account.demo-storage-account-tf.name
container_access_type = "private"
}
#uploads a file/blob into the above created container
resource "azurerm_storage_blob" "my-file-demo" {
name = "my-file-name-and-its.extention"
storage_account_name = azurerm_storage_account.demo-storage-account-tf.name
storage_container_name = azurerm_storage_container.demo-storage-container-tf.name
type = "Block"
source = "path-to-my-file"
}
Code for the providers.tf file.
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.100.0"
}
}
}
provider "azurerm" {
features {}
}
Code for the variables.tf file.
variable "resource_group_name" {
default = "myresourcegroupname"
}
variable "resource_group_geolocation" {
default = "West Europe"
}
#your storage account name has to be unique across all of Azure
variable "storage_account_name" {
default = "myuniqueaccountname"
}
variable "storage_container_name" {
default = "mycontainername"
}
Subscribe to my newsletter
Read articles from Ucheagwu Onyike directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by