Skip to content

Commit 7f8707a

Browse files
committed
[ACA] [353435] Update code to cloud tutorial to use managed identity.
1 parent 09b108d commit 7f8707a

File tree

1 file changed

+118
-19
lines changed

1 file changed

+118
-19
lines changed

articles/container-apps/tutorial-code-to-cloud.md

Lines changed: 118 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ms.custom:
99
- devx-track-azurepowershell
1010
- ignite-2023
1111
ms.topic: tutorial
12-
ms.date: 05/11/2022
12+
ms.date: 12/10/2024
1313
ms.author: cshoe
1414
zone_pivot_groups: container-apps-image-build-type
1515
---
@@ -145,25 +145,118 @@ cd code-to-cloud/src
145145

146146
## Create an Azure Container Registry
147147

148-
After the album API container image is built, create an Azure Container Registry (ACR) instance in your resource group to store it.
148+
1. After the album API container image is built, create an Azure Container Registry (ACR) instance in your resource group to store it.
149149

150-
# [Bash](#tab/bash)
150+
# [Bash](#tab/bash)
151151

152-
```azurecli
153-
az acr create \
154-
--resource-group $RESOURCE_GROUP \
155-
--name $ACR_NAME \
156-
--sku Basic \
157-
--admin-enabled true
158-
```
152+
```azurecli
153+
az acr create \
154+
--resource-group $RESOURCE_GROUP \
155+
--name $ACR_NAME \
156+
--sku Basic
157+
```
159158
160159
# [Azure PowerShell](#tab/azure-powershell)
161160
162-
```azurepowershell
163-
$acr = New-AzContainerRegistry -ResourceGroupName $ResourceGroup -Name $ACRName -Sku Basic -EnableAdminUser
164-
```
161+
```azurepowershell
162+
$acr = New-AzContainerRegistry `
163+
-ResourceGroupName $ResourceGroup `
164+
-Name $ACRName `
165+
-Sku Basic
166+
```
165167
166-
---
168+
---
169+
170+
1. Your container registry must allow Azure Resource Manager (ARM) audience tokens for authentication in order to use managed identity to pull images.
171+
172+
Use the following command to check if ARM tokens are allowed to access your Azure Container Registry (ACR).
173+
174+
# [Bash](#tab/bash)
175+
```azurecli
176+
az acr config authentication-as-arm show --registry "$ACR_NAME"
177+
```
178+
179+
TODO1 Use PS command
180+
# [Azure PowerShell](#tab/azure-powershell)
181+
```powershell
182+
$acr = Get-AzContainerRegistry -Name $ACRName
183+
$acr.Config.AuthenticationAsArm
184+
```
185+
186+
---
187+
188+
If ARM tokens are allowed, the command outputs the following.
189+
190+
```
191+
{
192+
"status": "enabled"
193+
}
194+
```
195+
196+
If the `status` is `disabled`, allow ARM tokens with the following command.
197+
198+
# [Bash](#tab/bash)
199+
```azurecli
200+
az acr config authentication-as-arm update --registry "$ACR_NAME" --status enabled
201+
```
202+
203+
TODO1 Use PS command
204+
# [Azure PowerShell](#tab/azure-powershell)
205+
```powershell
206+
$acr.Config.AuthenticationAsArm.Enabled = $true
207+
Set-AzContainerRegistry -ResourceGroupName $acr.ResourceGroupName -Name $acr.Name -Registry $acr
208+
```
209+
210+
---
211+
212+
## Create a user-assigned managed identity
213+
214+
To avoid using administrative credentials, pull images from private repositories in Microsoft Azure Container Registry using managed identities for authentication. When possible, use a user-assigned managed identity to pull images.
215+
216+
1. Create a user-assigned managed identity. Before you run the following commands, choose a name for your managed identity and replace the `\<PLACEHOLDER\>` with the name.
217+
218+
# [Bash](#tab/bash)
219+
220+
```bash
221+
IDENTITY="<YOUR_IDENTITY_NAME>"
222+
```
223+
224+
```azurecli
225+
az identity create \
226+
--name $IDENTITY \
227+
--resource-group $RESOURCE_GROUP
228+
```
229+
230+
# [Azure PowerShell](#tab/azure-powershell)
231+
232+
TODO1 Use PS command
233+
```powershell
234+
$IdentityName="<YOUR_IDENTITY_NAME>"
235+
$Identity = New-AzUserAssignedIdentity -ResourceGroupName $ResourceGroup -Name $IdentityName
236+
```
237+
238+
---
239+
240+
1. Get the identity's resource ID.
241+
242+
# [Bash](#tab/bash)
243+
244+
```azurecli
245+
IDENTITY_ID=$(az identity show \
246+
--name $IDENTITY \
247+
--resource-group $RESOURCE_GROUP \
248+
--query id \
249+
--output tsv)
250+
```
251+
252+
# [Azure PowerShell](#tab/azure-powershell)
253+
254+
TODO1 Use PS command
255+
```powershell
256+
$IdentityId = $identity.Id
257+
```
258+
259+
---
167260
168261
::: zone pivot="acr-remote"
169262
@@ -183,8 +276,10 @@ az acr build --registry $ACR_NAME --image $API_NAME .
183276

184277
# [Azure PowerShell](#tab/azure-powershell)
185278

279+
TODO1 Was this already in here? We think there is no PS equivalent for az acr build. If so, verify that. We think we say so elsewhere. Or maybe it's that there is no PS equivalent for az containerapp up.
186280
```azurepowershell
187-
az acr build --registry $ACRName --image $APIName .
281+
New-AzAcrBuildTask -RegistryName $ACRName -ImageName $APIName -ContextPath "."
282+
188283
```
189284

190285
---
@@ -229,12 +324,14 @@ az acr login --name $ACR_NAME
229324

230325
# [Azure PowerShell](#tab/azure-powershell)
231326

327+
TODO1 Use PS command
232328
```powershell
233-
az acr login --name $ACRName
329+
Connect-AzContainerRegistry -Name $ACRName
234330
```
235331

236332
---
237333

334+
TODO1 Shouldn't az acr build take care of this? Try skipping this.
238335
Now, push the image to your registry.
239336

240337
# [Bash](#tab/bash)
@@ -319,6 +416,8 @@ az containerapp create \
319416
--target-port 8080 \
320417
--ingress external \
321418
--registry-server $ACR_NAME.azurecr.io \
419+
--user-assigned "$IDENTITY_ID" \
420+
--registry-identity "$IDENTITY_ID" \
322421
--query properties.configuration.ingress.fqdn
323422
```
324423

@@ -342,6 +441,7 @@ $ImageParams = @{
342441
$TemplateObj = New-AzContainerAppTemplateObject @ImageParams
343442
```
344443

444+
TODO1 Remove?
345445
Run the following command to get your registry credentials.
346446

347447
```azurepowershell
@@ -353,11 +453,11 @@ Create a registry credential object to define your registry information, and a s
353453
```azurepowershell
354454
$RegistryArgs = @{
355455
Server = $ACRName + '.azurecr.io'
356-
PasswordSecretRef = 'registrysecret'
357-
Username = $RegistryCredentials.Username
456+
Identity = $IdentityId
358457
}
359458
$RegistryObj = New-AzContainerAppRegistryCredentialObject @RegistryArgs
360459
460+
TODO1 Remove.
361461
$SecretObj = New-AzContainerAppSecretObject -Name 'registrysecret' -Value $RegistryCredentials.Password
362462
```
363463

@@ -377,7 +477,6 @@ $AppArgs = @{
377477
ManagedEnvironmentId = $EnvId
378478
TemplateContainer = $TemplateObj
379479
ConfigurationRegistry = $RegistryObj
380-
ConfigurationSecret = $SecretObj
381480
IngressTargetPort = 8080
382481
IngressExternal = $true
383482
}

0 commit comments

Comments
 (0)