Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Validate Azure configurations, ARM/Bicep templates, and resource settings before deployment
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/recipes/terraform/README.md
1# Terraform Validation23Validation steps for Terraform deployments.45## Prerequisites67- `./infra/main.tf` exists8- State backend accessible910## Validation Steps1112- [ ] 1. Terraform Installation13- [ ] 2. Azure CLI Installation14- [ ] 3. Authentication15- [ ] 4. Initialize16- [ ] 5. Format Check17- [ ] 6. Validate Syntax18- [ ] 7. Plan Preview19- [ ] 8. State Backend20- [ ] 9. Azure Policy Validation21- [ ] 10. Template Variable Resolution Check (AZD+Terraform)2223## Validation Details2425### 1. Terraform Installation2627Verify Terraform is installed:2829```bash30terraform version31```3233**If not installed:** See https://developer.hashicorp.com/terraform/install3435### 2. Azure CLI Installation3637Verify Azure CLI is installed:3839```bash40az version41```4243**If not installed:**44```45mcp_azure_mcp_extension_cli_install(cli-type: "az")46```4748### 3. Authentication4950```bash51az account show52```5354**If not logged in:**55```bash56az login57az account set --subscription <subscription-id>58```5960### 4. Initialize6162```bash63cd infra64terraform init65```6667### 5. Format Check6869```bash70terraform fmt -check -recursive71```7273**Fix if needed:**74```bash75terraform fmt -recursive76```7778### 6. Validate Syntax7980```bash81terraform validate82```8384### 7. Plan Preview8586```bash87terraform plan -out=tfplan88```8990### 8. State Backend9192Verify state is accessible:9394```bash95terraform state list96```9798### 9. Azure Policy Validation99100See [Policy Validation Guide](../../policy-validation.md) for instructions on retrieving and validating Azure policies for your subscription.101102### 10. Template Variable Resolution Check (AZD+Terraform)103104> ⚠️ **CRITICAL for azd+Terraform projects.** azd substitutes `${VAR}` references in105> `main.tfvars.json` via envsubst, but does NOT interpolate Go-style template variables106> (`{{ .Env.* }}`). Unresolved Go-style template strings passed to Terraform cause107> cascading deployment failures, state conflicts, and timeouts.108109**Check for Go-style template variables:**110111```bash112# Check for Go-style template variables in Terraform files113grep -rn '{{ *\.Env\.' infra/ --include='*.tf' --include='*.tfvars.json' || echo "OK: No Go-style template variables found"114115# Check main.tfvars.json uses correct ${VAR} syntax116if test -f infra/main.tfvars.json; then117grep -n '{{ *\.Env\.' infra/main.tfvars.json && echo "WARNING: Use \${VAR} syntax instead of {{ .Env.* }}" || echo "OK: main.tfvars.json syntax is correct"118fi119```120121**If Go-style template variables are found:**1221. **Fix the syntax** in `main.tfvars.json` — replace `{{ .Env.VAR }}` with `${VAR}`:123```json124{125"environment_name": "${AZURE_ENV_NAME}",126"location": "${AZURE_LOCATION}"127}128```1292. For additional variables, use **`TF_VAR_*` environment variables**:130```bash131azd env set TF_VAR_environment_name "$(azd env get-value AZURE_ENV_NAME)"132```1333. **Verify** that `variables.tf` declares all required variables1344. **Re-run** `terraform validate` and `terraform plan` to confirm135136**If `.tfvars.json` uses wrong syntax:**137- Replace Go-style `{{ .Env.* }}` with `${VAR}` (azd's envsubst format)138- Prefer putting static defaults in `variables.tf` `default` values. Using `terraform.tfvars` (HCL) for static defaults is acceptable if your team prefers it; this restriction is specifically about avoiding Go-style template expressions in `.tfvars.json` files.139140## References141142- [Error handling](./errors.md)143144## Next145146All checks pass → **azure-deploy**147