Overcome Terraform Azure Challenges with CLI and PowerShell
Resolving Terraform Azure environment challenges for complex deployments through Azure CLI and PowerShell
To repeatedly deploy cloud-native applications on Azure through Infrastructure as Code (IaC) scripts, Terraform is an excellent choice. There are situations where Terraform results into execution problems due to nature of Azure Services operations and delayed resources management on Azure. In such scenarios, Terraform is integrated with Azure Command-line Interface (CLI) scripts wrapped inside PowerShell scripts.
Some real-world scenarios which you may come across while working in an Terraform Azure environment have been covered in this blog based on our experience gained while working on an e-Commerce application. In this blog, we intend to explain how combination 3 technologies (Terraform, Azure CLI and PowerShell) helped us address situations better.
Read about a terraform Azure situation that interests you or browse them all
- If Diagnostic settings are enabled and / or key vault soft delete is enabled, then resource already exist error occurred
- Environment gets deleted if we provision multiple environments from the same folder
- Unable to destroy created environment
- Terraform script fails if provisioning environment is configured for IPv6
- For the case of Private Link, DNS entries for Key Vault and MySQL are not removed
- VNET Peering settings are not removed with peered destination VNET
1. If Diagnostic settings are enabled and / or key vault soft delete is enabled, then resource already exist error occurred
Follow these steps:
- Create environment using terraform scripts with the respective settings.
- Delete environment using Azure Portal / Terraform command.
- Re-create the environment using Terraform with the same settings
- Observation: Terraform will throw error that resource already exist and need to be imported.
Diagnosed root cause
- Diagnostic settings are not deleted along with the resource group deletion. Same kind of issue is referred on Stack Overflow: https://stackoverflow.com/questions/70876645/diagnostic-settings-master-already-exists-to-be-managed-via-terraform-this.
- Refer the GitHub link for Key Vault specific issue: https://stackoverflow.com/questions/70876645/diagnostic-settings-master-already-exists-to-be-managed-via-terraform-this
- Microsoft has kept soft delete enabled for all new Key Vaults. This can’t be managed by Terraform. This is documented on Microsoft docs at: https://docs.microsoft.com/en-us/azure/key-vault/general/soft-delete-overview
2. Environment gets deleted if we provision multiple environments from the same folder
Follow these steps:
Try to create multiple environments from the same folder using different vars file. When you try a second time, the earlier environment gets deleted.
Diagnosed root cause:
Terraform maintains only one state file so when we execute the scripts for second time, it destroys the environment present in the existing state file.
The Solution:
Maintain separate state files by passing file names in both terraform plan and terraform Azure apply commands.
3. Unable to destroy created environment
Follow these steps:
Use the command terraform apply -destroy –auto-approvewhenever you’ve diagnostic settings enabled.
Diagnosed root cause:
The plan file gets stale due to drift in the file as Diagnostic settings take some time to fork through.
The Solution:
Delete diagnostic settings first and use Azure CLI for destroying Resource Group. Terraform destroy doesn’t work mostly in scenarios like this.
4. Terraform script fails if provisioning environment is configured for IPv6
Follow these steps:
- Use Internet Service Provider that has IPV6 enabled by default, e.g., Jio in India.
- From the computer connected to this ISP, run command to fork environment.
- Script will throw an error while provisioning MySQL server for opening IP Addresses in Firewall to execute SQL Commands from local computer.
The Solution:
Use (https://ipv4.icanhazip.com) to get IPv4 address and provide the obtained IPV4 address to put entry in Firewall settings of Azure MySQL Database Firewall.
5. For the case of Private Link, DNS entries for Key Vault and MySQL are not removed
Follow these steps:
- Run script to create an environment with Private Link enabled.
- Destroy the environment.
- Run script to re-create environment with same configuration.
- Observation: Terraform script will fail with reason that A Record in Private DNS Zone of MySQL and Key Vault already exists.
Diagnosed root cause:
DNS entries in Private Link of MySQL and Key Vault are not deleted by Terraform.
The Solution:
Delete these entries using Azure CLI scripts invocation.
6. VNET Peering settings are not removed with peered destination VNET
Follow these steps:
- Run script to create an environment with Private Link enabled.
- Destroy the environment.
- Run script to re-create environment with same configuration.
- Observation: Terraform script will fail with reason stating that VNET Peering already exists in the parent VNET / VNET with which current environment is being peered.
Diagnosed root cause:
VNET Address peering moves to Disconnected state when Environment is destroyed.
The Solution:
Delete these entries using Azure CLI scripts invocation.


Cloud Architecture Consultant and Mentor | Motifworks
Seasoned Enterprise and Multi-cloud Architect with strong experience in Global delivery and Technical Presales targeting North America Region. Experience in rollout of complex applications with latest technologies and balancing integration with existing systems. Mainly my areas of expertise lies in designing systems architecture that comply with business drivers such as – Performance, Scalability, Security using captive and 3rd party Data Centres including cloud services (PaaS, IaaS, SaaS). I have a strong hold on Microsoft technologies & open-source platforms.
Still facing difficulties creating and maintaining cloud-native applications?
Let us help you deploy a stateful application within minutes