If you want to create your own or contribute to an existing GitHub project using Terraform, here is how to get your first environment up and running on Windows.
Prerequisites
- IDE: Visual Studio Code
- Terraform executable: Releases ยท hashicorp/terraform or the official downloads page
- An Azure subscription
- Azure CLI: How to install the Azure CLI
Install Terraform
Download the Terraform executable and add it to your PATH. Verify the install with:
terraform --versionCreate a service principal or managed identity
Create a new service principal (or use a managed identity) that Terraform will use to authenticate against Azure, then test the connection.
az login --use-device-code --tenant <your_tenant_id>Set the subscription if you have multiple subscriptions:
az account set --subscription <your_subscription_id>Create a container to store the Terraform state
Store the Terraform state in an Azure Storage account container. See Store Terraform state in Azure Storage for more details.
$RESOURCE_GROUP_NAME='<your_rg_name>'
$STORAGE_ACCOUNT_NAME='<your_sta_name>'
$CONTAINER_NAME='tfstate'
$LOCATION = "westeurope"
# Create resource group
az group create --name $RESOURCE_GROUP_NAME --location $LOCATION
# Create storage account
az storage account create --resource-group $RESOURCE_GROUP_NAME --name $STORAGE_ACCOUNT_NAME --sku Standard_LRS --encryption-services blobCreate the container, then grant your identity the Storage Blob Data Contributor role on it.
Create a backend file
terraform {
backend "azurerm" {
resource_group_name = "rg-terraform"
storage_account_name = "<your_sta_name>"
container_name = "tfstate"
key = "mystatefile.terraform.tfstate"
}
}Run the following command to initialize the configuration:
terraform initA new state file will be created in the storage account.
Write, validate, format, and apply
Create your Terraform code, then validate it:
terraform validateFormat the code:
terraform fmt -recursivePlan or apply:
terraform apply -auto-approve