We use Terraform template to reliably deploy Azure resource.
Terraform is not an “installable” program. Simply download from Terraform Downloads page the Windows AMD64 zip file and extract the terraform.exe binary and execute it.
To initiate terraform
terraform init
To test/check what the template will change (add/change/destroy)
terraform plan
To deploy resources in the template
terraform apply
To remove all resources in the template
terraform destory
To import existing resources into the templace. Please note, this will instruct Terraform to effectively manage existing resources imported into the template. When run terraform destory, those imported resources will also be deleted.
terraform import
To define a resource in the template
resource "azurerm_virtual_network" "lab-vnet" {
name = var.vnet_name
location = var.location
resource_group_name = var.resource_group_name
address_space = var.vnet_address_space
}
If we don’t want to import existing resource, but need to referrence it in the template, use “data” instead of “resource” keyword
data "azurerm_virtual_network" "myDev" {
name = "MyDev-vnet"
resource_group_name = "myDev"
}
Before you run terraform command, make sure you are connect to the correct subscription.
az login
az account show
# or use this command
az account list | ConvertFrom-Json | Select-Object name, state, isDefault, tenantId | Format-Table
# or
az account list --query "[?state=='Enabled'].{Name:name, IsDefault:isDefault, Tenant:tenantId}" --output table
If it’s not the correct subscription, set to the correct subscription.
az account set --subscription Your-Subscription-Name-or-ID