|
101 | 101 | ``` |
102 | 102 |
|
103 | 103 | 4. **Infrastructure Code Fixes** |
| 104 | + |
| 105 | + **Identify the Source of Configuration Values** |
| 106 | + |
| 107 | + Before suggesting fixes, determine WHERE the problematic value is defined: |
| 108 | + |
| 109 | + a. **Hardcoded in Infrastructure Files (Bicep/Terraform)** |
| 110 | + - Search for hardcoded values in `main.bicep`, `*.bicep`, `main.tf`, `*.tf` files in the `infra/` directory |
| 111 | + - **Example:** `name: 'kv-hardcoded-name'` or `resource "azurerm_key_vault" "kv" { name = "hardcoded-name" }` |
| 112 | + - **Action Required:** Update the infrastructure file directly to use parameters or variables instead |
| 113 | + |
| 114 | + b. **Defined in Environment Files** |
| 115 | + - Values in `.env`, `.azure/<env-name>/.env`, `parameters.json`, `terraform.tfvars` |
| 116 | + - **Action Required:** Update the environment/parameter file |
| 117 | + |
| 118 | + **For Resource Naming Conflicts (e.g., VaultAlreadyExists, StorageAccountAlreadyExists, ResourceExists):** |
| 119 | + |
| 120 | + 1. **Locate the Name Definition:** |
| 121 | + ```bash |
| 122 | + # Search Bicep files for hardcoded resource names |
| 123 | + grep -r "name:" infra/*.bicep infra/**/*.bicep |
| 124 | + |
| 125 | + # Search Terraform files for hardcoded resource names |
| 126 | + grep -r "name =" infra/*.tf infra/**/*.tf |
| 127 | + ``` |
| 128 | + |
| 129 | + 2. **If Name is Hardcoded in Infrastructure Files:** |
| 130 | + - **Bicep Example Fix:** |
| 131 | + ```bicep |
| 132 | + // ❌ BEFORE (Hardcoded - causes conflicts) |
| 133 | + resource keyVault 'Microsoft.KeyVault/vaults@2023-02-01' = { |
| 134 | + name: 'kv-hardcoded-name' // This hardcoded name will conflict |
| 135 | + location: location |
| 136 | + // ... |
| 137 | + } |
| 138 | + |
| 139 | + // ✅ AFTER (Parameterized with unique suffix) |
| 140 | + targetScope = 'subscription' |
| 141 | + |
| 142 | + @minLength(1) |
| 143 | + @maxLength(64) |
| 144 | + @description('Name of the the environment which is used to generate a short unique hash used in all resources.') |
| 145 | + param environmentName string |
| 146 | + |
| 147 | + @minLength(1) |
| 148 | + @description('Primary location for all resources') |
| 149 | + param location string |
| 150 | +
|
| 151 | + var abbreviations = loadJsonContent('./abbreviations.json') |
| 152 | + var resourceToken = toLower(uniqueString(subscription().id, environmentName, location)) |
| 153 | + |
| 154 | + resource keyVault 'Microsoft.KeyVault/vaults@2023-02-01' = { |
| 155 | + name: !empty(keyVaultName) ? keyVaultName : '${abbreviations.keyVaultVaults}${resourceToken}' // Generates unique name per deployment |
| 156 | + location: location |
| 157 | + // ... |
| 158 | + } |
| 159 | + ``` |
| 160 | + |
| 161 | + - **Terraform Example Fix:** |
| 162 | + ```terraform |
| 163 | + # ❌ BEFORE (Hardcoded - causes conflicts) |
| 164 | + resource "azurerm_key_vault" "kv" { |
| 165 | + name = "kv-myapp" |
| 166 | + location = azurerm_resource_group.rg.location |
| 167 | + # ... |
| 168 | + } |
| 169 | + |
| 170 | + # ✅ AFTER (Using variables with unique suffix) |
| 171 | + resource "random_string" "resource_token" { |
| 172 | + length = 13 |
| 173 | + special = false |
| 174 | + upper = false |
| 175 | + } |
| 176 | + |
| 177 | + resource "azurerm_key_vault" "kv" { |
| 178 | + name = "kv-${random_string.resource_token.result}" |
| 179 | + location = azurerm_resource_group.rg.location |
| 180 | + # ... |
| 181 | + } |
| 182 | + ``` |
| 183 | + |
| 184 | + 3. **If Name is in Environment/Parameter Files:** |
| 185 | + - Update `.env` or `parameters.json` with new unique value |
| 186 | + - Use `azd env set KEY_VAULT_NAME=kv-new-unique-name` if applicable |
| 187 | + |
| 188 | + 4. **Verification:** |
| 189 | + - **Show the user which specific file(s) need to be updated** |
| 190 | + - **Provide the exact file path** (e.g., `infra/main.bicep` line 45) |
| 191 | + - **Explain why updating only `.env` won't work if the value is hardcoded in infrastructure files** |
| 192 | + |
| 193 | + **General Infrastructure Code Fixes:** |
104 | 194 | - **Bicep Files:** Correct bicep files based on error root cause |
105 | 195 | - **Terraform Files:** Correct terraform files based on error root cause |
106 | 196 | - Update parameter files with valid values |
| 197 | + - Ensure consistency between infrastructure definitions and environment variables |
107 | 198 |
|
108 | 199 | 5. **Verification Commands if user installed Azure CLI. Otherwise skip this part** |
109 | 200 | - Consider using following commands if fits: |
|
0 commit comments