Skip to content

Commit 45602b9

Browse files
author
Jill Grant
authored
Merge pull request #291916 from craigshoemaker/aca/jason/353435
[Container Apps] Add MI to Code to Cloud tutorial
2 parents 9726f64 + f934374 commit 45602b9

File tree

2 files changed

+191
-79
lines changed

2 files changed

+191
-79
lines changed

articles/container-apps/managed-identity-image-pull.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,7 @@ $AppArgs = @{
458458
ResourceGroupName = $ResourceGroupName
459459
ManagedEnvironmentId = $EnvId
460460
ConfigurationRegistry = $CredentialObject
461-
IdentityType = 'UserAssigned'
462-
IdentityUserAssignedIdentity = @{ $IdentityId = @{ } }
461+
UserAssignedIdentity = @($IdentityId)
463462
TemplateContainer = $TemplateObj
464463
IngressTargetPort = 80
465464
IngressExternal = $true

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

Lines changed: 190 additions & 77 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/12/2024
1313
ms.author: cshoe
1414
zone_pivot_groups: container-apps-image-build-type
1515
---
@@ -24,7 +24,7 @@ This is the first tutorial in the series of articles that walk you through how t
2424
> [!NOTE]
2525
> You can also build and deploy this app using the [az containerapp up](/cli/azure/containerapp#az_containerapp_up) by following the instructions in the [Quickstart: Build and deploy an app to Azure Container Apps from a repository](quickstart-code-to-cloud.md) article. The `az containerapp up` command is a fast and convenient way to build and deploy your app to Azure Container Apps using a single command. However, it doesn't provide the same level of customization for your container app.
2626
27-
The next tutorial in the series will build and deploy the front end web application to Azure Container Apps.
27+
The next tutorial in the series will build and deploy the front end web application to Azure Container Apps.
2828

2929
The following screenshot shows the output from the album API deployed in this tutorial.
3030

@@ -145,23 +145,126 @@ 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.
149+
150+
# [Bash](#tab/bash)
151+
152+
```azurecli
153+
az acr create \
154+
--resource-group $RESOURCE_GROUP \
155+
--location $LOCATION \
156+
--name $ACR_NAME \
157+
--sku Basic
158+
```
159+
160+
# [Azure PowerShell](#tab/azure-powershell)
161+
162+
```azurepowershell
163+
$acr = New-AzContainerRegistry `
164+
-ResourceGroupName $ResourceGroup `
165+
-Location $Location `
166+
-Name $ACRName `
167+
-Sku Basic
168+
```
169+
170+
---
171+
172+
1. Your container registry must allow Azure Resource Manager (ARM) audience tokens for authentication in order to use managed identity to pull images.
173+
174+
Use the following command to check if ARM tokens are allowed to access your Azure Container Registry (ACR).
175+
176+
# [Bash](#tab/bash)
177+
178+
```azurecli
179+
az acr config authentication-as-arm show --registry "$ACR_NAME"
180+
```
181+
182+
If ARM tokens are allowed, the command outputs the following.
183+
184+
```
185+
{
186+
"status": "enabled"
187+
}
188+
```
189+
190+
If the `status` is `disabled`, allow ARM tokens with the following command.
191+
192+
```azurecli
193+
az acr config authentication-as-arm update --registry "$ACR_NAME" --status enabled
194+
```
195+
196+
# [Azure PowerShell](#tab/azure-powershell)
197+
198+
```azurepowershell
199+
$acr.AzureAdAuthenticationAsArmPolicyStatus
200+
```
201+
202+
If the command returns `disabled`, allow ARM tokens with the following command.
203+
204+
```azurepowershell
205+
Update-AzContainerRegistry `
206+
-ResourceGroupName $acr.ResourceGroupName `
207+
-Name $acr.Name `
208+
-AzureAdAuthenticationAsArmPolicyStatus enabled
209+
```
210+
211+
---
212+
213+
## Create a user-assigned managed identity
214+
215+
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.
149216
150217
# [Bash](#tab/bash)
151218
152-
```azurecli
153-
az acr create \
154-
--resource-group $RESOURCE_GROUP \
155-
--name $ACR_NAME \
156-
--sku Basic \
157-
--admin-enabled true
158-
```
219+
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.
220+
221+
```bash
222+
IDENTITY="<YOUR_IDENTITY_NAME>"
223+
```
224+
225+
```azurecli
226+
az identity create \
227+
--name $IDENTITY \
228+
--resource-group $RESOURCE_GROUP
229+
```
230+
231+
1. Get the identity's resource ID.
232+
233+
```azurecli
234+
IDENTITY_ID=$(az identity show \
235+
--name $IDENTITY \
236+
--resource-group $RESOURCE_GROUP \
237+
--query id \
238+
--output tsv)
239+
```
159240
160241
# [Azure PowerShell](#tab/azure-powershell)
161242
162-
```azurepowershell
163-
$acr = New-AzContainerRegistry -ResourceGroupName $ResourceGroup -Name $ACRName -Sku Basic -EnableAdminUser
164-
```
243+
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.
244+
245+
```azurepowershell
246+
$IdentityName="<YOUR_IDENTITY_NAME>"
247+
$Identity = New-AzUserAssignedIdentity -ResourceGroupName $ResourceGroup -Name $IdentityName
248+
```
249+
250+
1. Get the identity's resource and principal ID.
251+
252+
```azurepowershell
253+
$IdentityId = $Identity.Id
254+
$PrincipalId = (Get-AzUserAssignedIdentity -Name $IdentityName -ResourceGroupName $ResourceGroup).PrincipalId
255+
```
256+
257+
1. Get the registry's resource ID. Before you run the following command, replace the *\<placeholders\>* with the resource group name for your registry.
258+
259+
```azurepowershell
260+
$RegistryId = (Get-AzContainerRegistry -ResourceGroupName $ResourceGroup -Name $ACRName).Id
261+
```
262+
263+
1. Create the `acrpull` role assignment for the identity.
264+
265+
```azurepowershell
266+
New-AzRoleAssignment -ObjectId $PrincipalId -Scope $RegistryId -RoleDefinitionName acrpull
267+
```
165268
166269
---
167270
@@ -183,8 +286,18 @@ az acr build --registry $ACR_NAME --image $API_NAME .
183286

184287
# [Azure PowerShell](#tab/azure-powershell)
185288

186-
```azurepowershell
187-
az acr build --registry $ACRName --image $APIName .
289+
The `az acr build` command does not have a PowerShell equivalent, but can be run in PowerShell.
290+
291+
To sign into Azure with the Azure CLI, run the following command and follow the prompts to complete the authentication process.
292+
293+
```powershell
294+
az login
295+
```
296+
297+
Then build the container.
298+
299+
```powershell
300+
az acr build --registry $AcrName --image $APIName .
188301
```
189302

190303
---
@@ -197,7 +310,7 @@ Output from the `az acr build` command shows the upload progress of the source c
197310

198311
## Build your application
199312

200-
The following steps, demonstrate how to build your container image locally using Docker and push the image to the new container registry.
313+
The following steps show how to build your container image locally using Docker and push the image to the new container registry.
201314

202315
### Build the container with Docker
203316

@@ -229,8 +342,8 @@ az acr login --name $ACR_NAME
229342

230343
# [Azure PowerShell](#tab/azure-powershell)
231344

232-
```powershell
233-
az acr login --name $ACRName
345+
```azurepowershell
346+
Connect-AzContainerRegistry -Name $ACRName
234347
```
235348

236349
---
@@ -239,13 +352,13 @@ Now, push the image to your registry.
239352

240353
# [Bash](#tab/bash)
241354

242-
```azurecli
355+
```bash
243356
docker push $ACR_NAME.azurecr.io/$API_NAME
244357
```
245358

246359
# [Azure PowerShell](#tab/azure-powershell)
247360

248-
```powershell
361+
```bash
249362
docker push "$ACRName.azurecr.io/$APIName"
250363
```
251364

@@ -319,6 +432,8 @@ az containerapp create \
319432
--target-port 8080 \
320433
--ingress external \
321434
--registry-server $ACR_NAME.azurecr.io \
435+
--user-assigned "$IDENTITY_ID" \
436+
--registry-identity "$IDENTITY_ID" \
322437
--query properties.configuration.ingress.fqdn
323438
```
324439

@@ -328,67 +443,65 @@ az containerapp create \
328443

329444
* Without a `query` property, the call to `az containerapp create` returns a JSON response that includes a rich set of details about the application. Adding a query parameter filters the output to just the app's fully qualified domain name (FQDN).
330445

446+
* This command adds the `acrPull` role to your user-assigned managed identity, so it can pull images from your container registry.
447+
331448
# [Azure PowerShell](#tab/azure-powershell)
332449

333450
To create the container app, create template objects that you pass in as arguments to the `New-AzContainerApp` command.
334451

335-
Create a template object to define your container image parameters.
336-
337-
```azurepowershell
338-
$ImageParams = @{
339-
Name = $APIName
340-
Image = $ACRName + '.azurecr.io/' + $APIName + ':latest'
341-
}
342-
$TemplateObj = New-AzContainerAppTemplateObject @ImageParams
343-
```
344-
345-
Run the following command to get your registry credentials.
346-
347-
```azurepowershell
348-
$RegistryCredentials = Get-AzContainerRegistryCredential -Name $ACRName -ResourceGroupName $ResourceGroup
349-
```
350-
351-
Create a registry credential object to define your registry information, and a secret object to define your registry password. The `PasswordSecretRef` refers to the `Name` in the secret object.
352-
353-
```azurepowershell
354-
$RegistryArgs = @{
355-
Server = $ACRName + '.azurecr.io'
356-
PasswordSecretRef = 'registrysecret'
357-
Username = $RegistryCredentials.Username
358-
}
359-
$RegistryObj = New-AzContainerAppRegistryCredentialObject @RegistryArgs
360-
361-
$SecretObj = New-AzContainerAppSecretObject -Name 'registrysecret' -Value $RegistryCredentials.Password
362-
```
363-
364-
Get your environment ID.
365-
366-
```azurepowershell
367-
$EnvId = (Get-AzContainerAppManagedEnv -EnvName $Environment -ResourceGroup $ResourceGroup).Id
368-
```
369-
370-
Create the container app.
371-
372-
```azurepowershell
373-
$AppArgs = @{
374-
Name = $APIName
375-
Location = $Location
376-
ResourceGroupName = $ResourceGroup
377-
ManagedEnvironmentId = $EnvId
378-
TemplateContainer = $TemplateObj
379-
ConfigurationRegistry = $RegistryObj
380-
ConfigurationSecret = $SecretObj
381-
IngressTargetPort = 8080
382-
IngressExternal = $true
383-
}
384-
$MyApp = New-AzContainerApp @AppArgs
385-
386-
# show the app's fully qualified domain name (FQDN).
387-
$MyApp.IngressFqdn
388-
```
389-
390-
* By setting `IngressExternal` to `external`, your container app is accessible from the public internet.
391-
* The `IngressTargetPort` parameter is set to `8080` to match the port that the container is listening to for requests.
452+
1. Create a template object to define your container image parameters.
453+
454+
```azurepowershell
455+
$ImageParams = @{
456+
Name = $APIName
457+
Image = $ACRName + '.azurecr.io/' + $APIName + ':latest'
458+
}
459+
$TemplateObj = New-AzContainerAppTemplateObject @ImageParams
460+
```
461+
462+
1. Create a registry credential object to define your registry information.
463+
464+
```azurepowershell
465+
$RegistryArgs = @{
466+
Server = $ACRName + '.azurecr.io'
467+
Identity = $IdentityId
468+
}
469+
$RegistryObj = New-AzContainerAppRegistryCredentialObject @RegistryArgs
470+
```
471+
472+
1. Get your environment ID.
473+
474+
```azurepowershell
475+
$EnvId = (Get-AzContainerAppManagedEnv -EnvName $Environment -ResourceGroup $ResourceGroup).Id
476+
```
477+
478+
1. Create the container app.
479+
480+
```azurepowershell
481+
$AppConfig = @{
482+
IngressTargetPort = 8080
483+
IngressExternal = $true
484+
Registry = $RegistryObj
485+
}
486+
$AppConfigObj = New-AzContainerAppConfigurationObject @AppConfig
487+
488+
$AppArgs = @{
489+
Name = $APIName
490+
Location = $Location
491+
ResourceGroupName = $ResourceGroup
492+
ManagedEnvironmentId = $EnvId
493+
TemplateContainer = $TemplateObj
494+
Configuration = $AppConfigObj
495+
UserAssignedIdentity = @($IdentityId)
496+
}
497+
$MyApp = New-AzContainerApp @AppArgs
498+
499+
# Show the app's fully qualified domain name (FQDN).
500+
$MyApp.LatestRevisionFqdn
501+
```
502+
503+
* By setting `IngressExternal` to `$true`, your container app is accessible from the public internet.
504+
* The `IngressTargetPort` parameter is set to `8080` to match the port that the container is listening to for requests.
392505
393506
---
394507

0 commit comments

Comments
 (0)