Creating the Terraform State File in Azure

This is part two of a multipart post that walks through how to authenticate to Azure using a GitHub Action to run a Terraform template and deploy your projects infrastructure. In the first part, I shared a bare-bones Terraform template that stores the Terraform state file in an Azure Storage Account. This post will focus on actually initializing the state file.

In the previous post, I mentioned that we need some resources in Azure to hold the state file. We will need a Resource Group, an Azure Storage Account and a Blob Container. I am going to use a tiny PowerShell script that uses the Azure CLI to create those resources.

In VS Code, add a file named bootstrap.ps1 to the project folder and paste in the code above.

Now, use a terminal to log into Azure and run that script.

After that runs, we have our resource group, storage account and blob container.

Finally, back in the Terminal, run terraform init.

“Terraform has been successfully initialized!” 🙂 A .terraform.lock.hcl has been added to the project folder. The lock file fixes the version of Terraform modules so deployments are repeatable (idempotent). The Terraform state file has been created in the tfstate blob container.

In this post, I have demonstrated how to bootstrap the necessary resources to store the Terraform state file in Azure. I also demonstrated initializing the state file. In the next post, I’ll demonstrate getting our code into GitHub.

Next: Creating the GitHub Repository