Set Up your first Terraform environment on Windows
Table of content
If you want to create your own or contribute to an existing GitHub project, you are on the right page.
Prerequisites
IDE (Vscode): Download Visual Studio Code – Mac, Linux, Windows TF executable: Releases · hashicorp/terraform (github.com) or here https://releases.hashicorp.com/terraform/ Azure Subscription Azure CLI: How to install the Azure CLI | Microsoft Learn
Go
Prerequisites
Create a storager account and container
Define the subscription to store the state
Install Terraform
Add it to the path
Type to test: terraform –version
1terraform --version
Create a new SPN or MI
Test connection
AZ login
1az login --use-device-code --tenant <your_tenant_id>
Set sub if you have multiple subscriptions
1az account set --subscription <your_subscription_id>
Create a container to store the tfstate
Store Terraform state in Azure Storage Microsoft Learn
1$RESOURCE_GROUP_NAME='<your_rg_name>'
2$STORAGE_ACCOUNT_NAME='<your_sta_name>'
3$CONTAINER_NAME='tfstate'
4$LOCATION = "westeurope"
5
6# Create resource group
7az group create --name $RESOURCE_GROUP_NAME --location $LOCATION
8
9# Create storage account
10az storage account create --resource-group $RESOURCE_GROUP_NAME --name $STORAGE_ACCOUNT_NAME --sku Standard_LRS --encryption-services blob
Create the container
Add storage blobh data contributor right
Create a backend file
1terraform {
2 backend "azurerm" {
3 resource_group_name = "rg-terraform"
4 storage_account_name = "<your_sta_name>"
5 container_name = "tfstate"
6 key = "mystatefile.terraform.tfstate"
7 }
8}
Run the following command to run the configuration:
1terraform init
A new state file will be created in the storage account.
Create your terraform code
Validate the code
1terraform validate
Format the code
1terraform fmt -recursive
Plan or apply
1terraform apply -auto-approve