Terraform Azure
--
- Configure Node js and terraform in Environment Variables (Use Variables — Path)
- Install VS Code extensions — Azure Terraform and Azure Cli
- Set the current Azure subscription
To view the current Azure subscription,
az account show
If you have access to multiple available Azure subscriptions, use az account list to display a list of subscription name ID values:
az account list --query "[].{name:name, subscriptionId:id}"
To use a specific Azure subscription for the current Cloud Shell session, use az account set.
az account set --subscription="<subscription_id>"
Install chocolatey
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString(‘https://community.chocolatey.org/install.ps1'))
Install terraform
choco install terraform
4. Create and apply a Terraform execution plan
To initialize the Terraform deployment, run terraform init. This command downloads the Azure modules required to create an Azure resource group.
terraform init
After initialization, you create an execution plan by running terraform plan.
terraform plan -out <terraform_plan>.tfplan
- The optional
-out
parameter allows you to specify an output file for the plan.
Once you’re ready to apply the execution plan to your cloud infrastructure, you run terraform apply.
terraform apply <terraform_plan>.tfplan
az account set — subscription=”c61c7331–30b2–4727–87a4–0048303139f6"
# Initializes the terraform exec
terraform init
# Generate a plan according to the tf file
terraform plan -out devsetup.tfplan
# Apply the plan remotely
terraform apply devsetup.tfplan
# Remove the remote resources based on the plan
# terraform destroy
##############################################################################
# Plan using dynamic variables
# terraform plan -var-file=”terraform.tfvars” -out devsetup.tfplan
# # Apply the plan remotely
# terraform apply devsetup.tfplan
##############################################################################
# Environment wise Plan and Apply
terraform plan -var-file=”terraform.tfvars” -var-file=”.\Development\development.tfvars” -state=”.\Development\dev.state”
terraform apply -var-file=”terraform.tfvars” -var-file=”.\Development\development.tfvars” -state=”.\Development\dev.state”
# terraform destroy -var-file=”terraform.tfvars” -var-file=”Development\development.tfvars” -state=”Development\dev.state”