Loading...

Terraform with Azure Cloud Shell & State file in Azure Storage

Terraform with Azure Cloud Shell & State file in Azure Storage

Azure Cloud Shell automatically has the latest version of Terraform installed. Also, as Terraform is run from the cloud it automatically uses information from the current Azure subscription and you can securely save the Terraform state file in Azure Storage. Best of all, you can still do all your authoring of Terraform from VS Code. 

 

Btw, the last section on the Terraform state file in Azure storage is documented here on Microsoft Docs, however I have captured the complete solution here.

 

Four easy steps:

  1. Connect to CloudDrive (Azure Files Share)
  2. Copy files from this reop to Azure CloudDrive
  3. Setup the Terraform State in Azure Blob Storage
  4. Sign into Azure Cloud Shell from VS Code

 

1. Connect to CloudDrive (Azure Files Share)

Assuming you have previously connected to Azure Cloud Shell before and already setup the Storage Account more details here. Next, you will need to connect to the Azure Files share in which CloudDrive is configured to use, so you can copy and paste the Terraform files in this repo. 

 

To check the region where Azure Cloud Shell is running from along with other info, type env at the prompt:

 

MarcKean_0-1664153926715.png

 

To see which Azure Files share CloudDrive is using, run the df command.

 

MarcKean_1-1663219507007.png

In this instance

  • the storage account is 20220915azurecloudshell
  • the Azure Files Share is acs

Azure CloudDrive maps to the one single Azure File Share, where you can create N number of folders. 

 

2. Copy the repo files to the CloudDrive

Next, you can either use Azure Storage Explorer to copy the files to CloudDrive. Or you can Mount the SMB Azure file share on your local device, Windows, Linux, macOS – https://docs.microsoft.com/en-us/azure/storage/files/storage-how-to-use-files-windows.

 

Simply create a folder in the root of Azure CloudDrive file share itself. In my instance, the file share would be acs so create a folder in here.

 

So it would be like this: acs\Terraform\CAF-Landing-Zones-Design_Implement

 

Which is: <AzureFileShare>\<Folder>\<Folder>

 

I am using Azure Storage Explorer:

 

MarcKean_2-1664154312801.png

 

Copy all the files/folders over.

 

Then using the Azure Tools Extension to VS Code, you can access the storage account:

 

MarcKean_3-1664154370450.png

 

TIP: you might want to increase the quota of the Azure CloudDrive File Share, as 6GiB is not enough once you start copying files to it:

 

MarcKean_0-1663284937335.png

 

3. Setup the Terraform State in Azure Blob Storage

The state for Terraform should live in a stateful place which is central, common, secure and accessible to everything. E.g. Azure Storage is a perfect candidate. You’ll need to setup a separate, dedicated Azure Storage account with a container. Recommendation would be to apply Azure resource locking on this storage account so that it doesn’t get deleted accidentally. Also, maybe apply some tags to this storage account, clearly specifying what it’s used for.

 

Edit the terraform.tf and change the values for backend "azurerm" to suit your own Azure Storage Account. key = "prod.terraform.tfstate" the same.

 

You can keep key = "prod.terraform.tfstate" as is, no change.

 

 

terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = ">= 2.68.0" # was 2.46.1 } } backend "azurerm" { resource_group_name = "TerraformState_CloudShell" storage_account_name = "tfstatecloudshell2021" container_name = "tfstate" key = "prod.terraform.tfstate" } }

 

 

4. Sign into Azure Cloud Shell from VS Code

You just need to sign in, run az login from the prompt. Once signed in, navigate to the clouddrive folder, where you'll find your Terraform files. 

 

MarcKean_1-1664154219023.png

 

Then simply run your normal Terraform commands (e.g. Terraform -init or Terraform -apply).

 

Published on:

Learn more
Azure Architecture Blog articles
Azure Architecture Blog articles

Azure Architecture Blog articles

Share post:

Related posts

End-to-End Full-Stack Web Application with Azure AD B2C Authentication: A Complete Guide

Application Overview The purpose of this sample application is to demonstrate the usage of Azure Active Directory B2C (Azure AD B2C) for authe...

1 year ago

Complex Data Extraction using Document Intelligence and RAG

Section 1: Introduction   Historically, data extraction from unstructured documents was a manual and tedious process. It consisted of a c...

1 year ago

Harnessing Generative AI with Weaviate on Azure Kubernetes Service and Azure NetApp Files

Table of Contents Introduction Prerequisites Install Weaviate Approximate Nearest Neighbor (ANN) Benchmarks ANN Benchmarks Setup ANN Benchmark...

1 year ago

Securing Containerized Applications with SSH Tunneling

As cloud engineers and architects embrace containerization, ensuring secure communication becomes paramount. Data transmission and access cont...

1 year ago

Exploring AI Agent-Driven Auto Insurance Claims RAG Pipeline.

Introduction: In this post, I explore a recent experiment aimed at creating a RAG pipeline tailored for the insurance industry, specificall...

1 year ago

Azure NetApp Files now stores sensitive data DoD IL5 compliant in Azure US Government regions

Table of Contents Introduction Why Azure NetApp Files? DoD IL5 compliance in Azure Government Azure NetApp Files reaches feature parity betwee...

1 year ago

Data Intelligence End-to-End with Azure Databricks and Microsoft Fabric

This Azure Architecture Blog was written in conjunction with Isaac Gritz, Senior Solutions Architect, at Databricks.   The Data Inte...

1 year ago

AI Studio End-to-End Baseline Reference Implementation

  Azure AI Studio is designed to cater to the growing needs of developers seeking to integrate advanced AI capabilities into their appli...

1 year ago

Mastering AI adoption: Essentials to building, operating and optimizing genAI workloads on Azure

Mastering your AI adoption: Essentials to building, operating and optimizing genAI workloads on Azure As the demand for scalable, efficient AI...

2 years ago

Optimize Azure Stack HCI with the Well-Architected Framework

  Azure Stack HCI is a hyperconverged infrastructure (HCI) solution that provides storage, network, and compute resources in on-premises...

2 years ago

Newsletter

Get the latest Dynamics 365 and Power Platform content in your inbox

A curated digest of community blogs, product news, videos, and podcasts — delivered without the noise.

Weekly updates Unsubscribe anytime Fresh community picks
We use your email only for the newsletter and you can unsubscribe at any time.
By subscribing, you agree to the privacy policy.