Skip to content

Commit 8643a4e

Browse files
Add tags in resource group when deployment is in pre-provision state
1 parent 155628a commit 8643a4e

3 files changed

Lines changed: 98 additions & 0 deletions

File tree

infra/main.bicep

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,12 @@ resource resourceGroupTags 'Microsoft.Resources/tags@2025-04-01' = {
218218
tags: union(
219219
deploymentTags,
220220
{
221+
'azd-env-name': environmentName
221222
TemplateName: 'Deploy Your AI Application in Prod'
222223
Type: networkIsolation ? 'WAF' : 'Non-WAF'
223224
CreatedBy: createdBy
224225
DeploymentName: deployment().name
226+
Location: location
225227
}
226228
)
227229
}

scripts/preprovision-integrated.ps1

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,57 @@ if ([string]::IsNullOrWhiteSpace($Location) -or [string]::IsNullOrWhiteSpace($Re
194194
exit 1
195195
}
196196

197+
# ========================================
198+
# [0] Pre-create resource group with tags
199+
# ========================================
200+
Write-Host "[0] Ensuring resource group exists with tags..." -ForegroundColor Cyan
201+
202+
# Determine CreatedBy (matches Bicep logic: UPN prefix or objectId)
203+
$createdBy = $null
204+
try {
205+
$upn = (& az ad signed-in-user show --query userPrincipalName -o tsv 2>$null)
206+
if (-not [string]::IsNullOrWhiteSpace($upn)) {
207+
$createdBy = $upn.Split('@')[0]
208+
}
209+
} catch { }
210+
if ([string]::IsNullOrWhiteSpace($createdBy)) {
211+
try {
212+
$createdBy = (& az ad signed-in-user show --query id -o tsv 2>$null)
213+
} catch { }
214+
}
215+
if ([string]::IsNullOrWhiteSpace($createdBy)) {
216+
$createdBy = $env:USERNAME
217+
}
218+
219+
# Type tag based on networkIsolation setting
220+
$networkIsolationValue = $env:NETWORK_ISOLATION
221+
$typeTag = if ($networkIsolationValue -eq 'true') { 'WAF' } else { 'Non-WAF' }
222+
223+
$rgTags = @(
224+
"TemplateName=Deploy Your AI Application in Prod"
225+
"Type=$typeTag"
226+
"CreatedBy=$createdBy"
227+
"Location=$($env:AZURE_LOCATION)"
228+
)
229+
230+
$rgExists = (& az group exists --name $ResourceGroup --subscription $SubscriptionId 2>$null)
231+
if ($rgExists -eq 'true') {
232+
# RG exists — merge tags without removing existing ones
233+
& az tag update `
234+
--resource-id "/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroup" `
235+
--operation Merge `
236+
--tags @rgTags `
237+
--only-show-errors | Out-Null
238+
}
239+
240+
if ($LASTEXITCODE -ne 0) {
241+
Write-Host "[X] Failed to create/update resource group '$ResourceGroup' with tags." -ForegroundColor Red
242+
exit 1
243+
}
244+
245+
Write-Host " [+] Resource group '$ResourceGroup' ready with tags" -ForegroundColor Green
246+
Write-Host ""
247+
197248
# Navigate to AI Landing Zone submodule
198249
$aiLandingZonePath = Join-Path $PSScriptRoot ".." "submodules" "ai-landing-zone"
199250

scripts/preprovision-integrated.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,51 @@ if [ -z "${AZURE_LOCATION}" ] || [ -z "${AZURE_RESOURCE_GROUP}" ] || [ -z "${AZU
7070
exit 1
7171
fi
7272

73+
# ========================================
74+
# [0] Pre-create resource group with tags
75+
# ========================================
76+
echo "[0] Ensuring resource group exists with tags..."
77+
78+
# Determine CreatedBy (matches Bicep logic: UPN prefix or objectId)
79+
CREATED_BY=""
80+
CREATED_BY="$(az ad signed-in-user show --query userPrincipalName -o tsv 2>/dev/null | cut -d@ -f1 || true)"
81+
if [ -z "$CREATED_BY" ]; then
82+
CREATED_BY="$(az ad signed-in-user show --query id -o tsv 2>/dev/null || true)"
83+
fi
84+
if [ -z "$CREATED_BY" ]; then
85+
CREATED_BY="${USER:-unknown}"
86+
fi
87+
88+
# Type tag based on networkIsolation setting
89+
NETWORK_ISOLATION_VALUE="${NETWORK_ISOLATION:-false}"
90+
if [ "$NETWORK_ISOLATION_VALUE" = "true" ]; then
91+
TYPE_TAG="WAF"
92+
else
93+
TYPE_TAG="Non-WAF"
94+
fi
95+
96+
RG_EXISTS="$(az group exists --name "$AZURE_RESOURCE_GROUP" --subscription "$AZURE_SUBSCRIPTION_ID" 2>/dev/null || echo 'false')"
97+
if [ "$RG_EXISTS" = "true" ]; then
98+
# RG exists — merge tags without removing existing ones
99+
az tag update \
100+
--resource-id "/subscriptions/$AZURE_SUBSCRIPTION_ID/resourceGroups/$AZURE_RESOURCE_GROUP" \
101+
--operation Merge \
102+
--tags \
103+
"TemplateName=Deploy Your AI Application in Prod" \
104+
"Type=$TYPE_TAG" \
105+
"CreatedBy=$CREATED_BY" \
106+
"Location=$AZURE_LOCATION" \
107+
--only-show-errors > /dev/null
108+
fi
109+
110+
if [ $? -ne 0 ]; then
111+
echo "[X] Failed to update resource group '$AZURE_RESOURCE_GROUP' with tags."
112+
exit 1
113+
fi
114+
115+
echo " [+] Resource group '$AZURE_RESOURCE_GROUP' ready with tags"
116+
echo ""
117+
73118
# Check if submodule exists
74119
AI_LANDING_ZONE_PATH="$REPO_ROOT/submodules/ai-landing-zone/bicep"
75120

0 commit comments

Comments
 (0)