Skip to content

Commit 7feb685

Browse files
authored
Merge pull request #204078 from cebundy/add-ps-cmdlets-quickstarts-tutorials
[Container Apps]: Add PowerShell cmdlets to quickstarts and tutorials
2 parents f69fd18 + f4bbf10 commit 7feb685

File tree

7 files changed

+724
-414
lines changed

7 files changed

+724
-414
lines changed

articles/container-apps/background-processing.md

Lines changed: 93 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,55 @@ az containerapp env create \
3838
--location "$LOCATION"
3939
```
4040

41-
# [PowerShell](#tab/powershell)
41+
# [Azure PowerShell](#tab/azure-powershell)
4242

43-
```azurecli
44-
az containerapp env create `
45-
--name $CONTAINERAPPS_ENVIRONMENT `
46-
--resource-group $RESOURCE_GROUP `
47-
--location $LOCATION
43+
A Log Analytics workspace is required for the Container Apps environment. The following commands create a Log Analytics workspace and save the workspace ID and primary shared key to environment variables.
44+
45+
46+
```azurepowershell
47+
$WorkspaceArgs = @{
48+
Name = 'myworkspace'
49+
ResourceGroupName = $ResourceGroupName
50+
Location = $Location
51+
PublicNetworkAccessForIngestion = 'Enabled'
52+
PublicNetworkAccessForQuery = 'Enabled'
53+
}
54+
New-AzOperationalInsightsWorkspace @WorkspaceArgs
55+
$WorkspaceId = (Get-AzOperationalInsightsWorkspace -ResourceGroupName $ResourceGroupName -Name $WorkspaceArgs.Name).CustomerId
56+
$WorkspaceSharedKey = (Get-AzOperationalInsightsWorkspaceSharedKey -ResourceGroupName $ResourceGroupName -Name $WorkspaceArgs.Name).PrimarySharedKey
57+
```
58+
59+
To create the environment, run the following command:
60+
61+
```azurepowershell
62+
$EnvArgs = @{
63+
EnvName = $ContainerAppsEnvironment
64+
ResourceGroupName = $ResourceGroupName
65+
Location = $Location
66+
AppLogConfigurationDestination = 'log-analytics'
67+
LogAnalyticConfigurationCustomerId = $WorkspaceId
68+
LogAnalyticConfigurationSharedKey = $WorkspaceSharedKey
69+
}
70+
71+
New-AzContainerAppManagedEnv @EnvArgs
4872
```
4973

5074
---
5175

5276
## Set up a storage queue
5377

54-
Choose a name for `STORAGE_ACCOUNT`. Storage account names must be *unique within Azure* and be from 3 to 24 characters in length containing numbers and lowercase letters only.
78+
Begin by defining a name for the storage account. Storage account names must be *unique within Azure* and be from 3 to 24 characters in length containing numbers and lowercase letters only.
5579

5680
# [Bash](#tab/bash)
5781

5882
```bash
59-
STORAGE_ACCOUNT="<storage account name>"
83+
STORAGE_ACCOUNT_NAME="<STORAGE_ACCOUNT_NAME>"
6084
```
6185

62-
# [PowerShell](#tab/powershell)
86+
# [Azure PowerShell](#tab/azure-powershell)
6387

64-
```powershell
65-
$STORAGE_ACCOUNT="<storage account name>"
88+
```azurepowershell
89+
$StorageAcctName = "<StorageAccountName>"
6690
```
6791

6892
---
@@ -73,22 +97,24 @@ Create an Azure Storage account.
7397

7498
```azurecli
7599
az storage account create \
76-
--name $STORAGE_ACCOUNT \
100+
--name $STORAGE_ACCOUNT_NAME \
77101
--resource-group $RESOURCE_GROUP \
78102
--location "$LOCATION" \
79103
--sku Standard_RAGRS \
80104
--kind StorageV2
81105
```
82106

83-
# [PowerShell](#tab/powershell)
107+
# [Azure PowerShell](#tab/azure-powershell)
84108

85-
```powershell
86-
$STORAGE_ACCOUNT = New-AzStorageAccount `
87-
-Name $STORAGE_ACCOUNT_NAME `
88-
-ResourceGroupName $RESOURCE_GROUP `
89-
-Location $LOCATION `
90-
-SkuName Standard_RAGRS `
91-
-Kind StorageV2
109+
```azurepowershell
110+
$StorageAcctArgs = @{
111+
Name = $StorageAcctName
112+
ResourceGroupName = $ResourceGroupName
113+
Location = $location
114+
SkuName = 'Standard_RAGRS'
115+
Kind = 'StorageV2'
116+
}
117+
$StorageAcct = New-AzStorageAccount @StorageAcctArgs
92118
```
93119

94120
---
@@ -98,14 +124,20 @@ Next, get the connection string for the queue.
98124
# [Bash](#tab/bash)
99125

100126
```azurecli
101-
QUEUE_CONNECTION_STRING=`az storage account show-connection-string -g $RESOURCE_GROUP --name $STORAGE_ACCOUNT --query connectionString --out json | tr -d '"'`
127+
QUEUE_CONNECTION_STRING=`az storage account show-connection-string -g $RESOURCE_GROUP --name $STORAGE_ACCOUNT_NAME --query connectionString --out json | tr -d '"'`
102128
```
103129

104-
# [PowerShell](#tab/powershell)
130+
# [Azure PowerShell](#tab/azure-powershell)
105131

106-
```azurecli
107-
$QUEUE_CONNECTION_STRING=(az storage account show-connection-string -g $RESOURCE_GROUP --name $STORAGE_ACCOUNT_NAME --query connectionString --out json) -replace '"',''
132+
Here we use Azure CLI as there isn't an equivalent PowerShell cmdlet to get the connection string for the storage account queue.
133+
134+
```azurepowershell
135+
$QueueConnectionString = (Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAcctName).Context.ConnectionString
108136
```
137+
<!--
138+
139+
$QueueConnectionString = (az storage account show-connection-string -g $ResourceGroupName --name $StorageAcctName --query connectionString --out json) -replace '"',''
140+
-->
109141

110142
---
111143

@@ -115,16 +147,15 @@ Now you can create the message queue.
115147

116148
```azurecli
117149
az storage queue create \
118-
--name "myqueue" \
119-
--account-name $STORAGE_ACCOUNT \
150+
--name 'myqueue" \
151+
--account-name $STORAGE_ACCOUNT_NAME \
120152
--connection-string $QUEUE_CONNECTION_STRING
121153
```
122154

123-
# [PowerShell](#tab/powershell)
155+
# [Azure PowerShell](#tab/azure-powershell)
124156

125-
```powershell
126-
$queue = New-AzStorageQueue –Name "myqueue" `
127-
-Context $STORAGE_ACCOUNT.Context
157+
```azurepowershell
158+
$Queue = New-AzStorageQueue -Name 'myqueue' -Context $StorageAcct.Context
128159
```
129160

130161
---
@@ -140,13 +171,15 @@ az storage message put \
140171
--connection-string $QUEUE_CONNECTION_STRING
141172
```
142173

143-
# [PowerShell](#tab/powershell)
174+
# [Azure PowerShell](#tab/azure-powershell)
144175

145-
```azurecli
146-
$queueMessage = [Microsoft.Azure.Storage.Queue.CloudQueueMessage]::new("Hello Queue Reader App")
147-
$queue.CloudQueue.AddMessageAsync($QueueMessage)
176+
```azurepowershell
177+
$QueueMessage = [Microsoft.Azure.Storage.Queue.CloudQueueMessage]::new("Hello Queue Reader App")
178+
$Queue.CloudQueue.AddMessageAsync($QueueMessage).GetAwaiter().GetResult()
148179
```
149180

181+
A result of `Microsoft.Azure.Storage.Core.NullType` is returned when the message is added to the queue.
182+
150183
---
151184

152185
## Deploy the background application
@@ -245,20 +278,22 @@ az deployment group create --resource-group "$RESOURCE_GROUP" \
245278
location="$LOCATION"
246279
```
247280

248-
# [PowerShell](#tab/powershell)
281+
# [Azure PowerShell](#tab/azure-powershell)
249282

250-
```powershell
251-
$params = @{
252-
environment_name = $CONTAINERAPPS_ENVIRONMENT
253-
location = $LOCATION
254-
queueconnection=$QUEUE_CONNECTION_STRING
283+
```azurepowershell
284+
$Params = @{
285+
environment_name = $ContainerAppsEnvironment
286+
location = $Location
287+
queueconnection = $QueueConnectionString
255288
}
256289
257-
New-AzResourceGroupDeployment `
258-
-ResourceGroupName $RESOURCE_GROUP `
259-
-TemplateParameterObject $params `
260-
-TemplateFile ./queue.json `
261-
-SkipTemplateParameterPrompt
290+
$DeploymentArgs = @{
291+
ResourceGroupName = $ResourceGroupName
292+
TemplateParameterObject = $Params
293+
TemplateFile = './queue.json'
294+
SkipTemplateParameterPrompt = $true
295+
}
296+
New-AzResourceGroupDeployment @DeploymentArgs
262297
```
263298

264299
---
@@ -269,7 +304,7 @@ The application scales out to 10 replicas based on the queue length as defined i
269304

270305
## Verify the result
271306

272-
The container app runs as a background process. As messages arrive from the Azure Storage Queue, the application creates log entries in Log analytics. You must wait a few minutes for the analytics to arrive for the first time before you are able to query the logged data.
307+
The container app runs as a background process. As messages arrive from the Azure Storage Queue, the application creates log entries in Log analytics. You must wait a few minutes for the analytics to arrive for the first time before you're able to query the logged data.
273308

274309
Run the following command to see logged messages. This command requires the Log analytics extension, so accept the prompt to install extension when requested.
275310

@@ -280,16 +315,14 @@ LOG_ANALYTICS_WORKSPACE_CLIENT_ID=`az containerapp env show --name $CONTAINERAPP
280315
281316
az monitor log-analytics query \
282317
--workspace $LOG_ANALYTICS_WORKSPACE_CLIENT_ID \
283-
--analytics-query "ContainerAppConsoleLogs_CL | where ContainerAppName_s == 'queuereader' and Log_s contains 'Message ID'" \
318+
--analytics-query "ContainerAppConsoleLogs_CL | where ContainerAppName_s == 'queuereader' and Log_s contains 'Message ID' | project Time=TimeGenerated, AppName=ContainerAppName_s, Revision=RevisionName_s, Container=ContainerName_s, Message=Log_s | take 5" \
284319
--out table
285320
```
286321

287-
# [PowerShell](#tab/powershell)
288-
289-
```powershell
290-
$LOG_ANALYTICS_WORKSPACE_CLIENT_ID=(az containerapp env show --name $CONTAINERAPPS_ENVIRONMENT --resource-group $RESOURCE_GROUP --query properties.appLogsConfiguration.logAnalyticsConfiguration.customerId --out tsv)
322+
# [Azure PowerShell](#tab/azure-powershell)
291323

292-
$queryResults = Invoke-AzOperationalInsightsQuery -WorkspaceId $LOG_ANALYTICS_WORKSPACE_CLIENT_ID -Query "ContainerAppConsoleLogs_CL | where ContainerAppName_s == 'queuereader' and Log_s contains 'Message ID'"
324+
```azurepowershell
325+
$queryResults = Invoke-AzOperationalInsightsQuery -WorkspaceId $WorkspaceId -Query "ContainerAppConsoleLogs_CL | where ContainerAppName_s == 'queuereader' and Log_s contains 'Message ID' | project Time=TimeGenerated, AppName=ContainerAppName_s, Revision=RevisionName_s, Container=ContainerName_s, Message=Log_s | take 5"
293326
$queryResults.Results
294327
```
295328

@@ -300,7 +333,10 @@ $queryResults.Results
300333
301334
## Clean up resources
302335

303-
Once you are done, run the following command to delete the resource group that contains your Container Apps resources.
336+
Once you're done, run the following command to delete the resource group that contains your Container Apps resources.
337+
338+
>[!CAUTION]
339+
> The following command deletes the specified resource group and all resources contained within it. If resources outside the scope of this tutorial exist in the specified resource group, they will also be deleted.
304340
305341
# [Bash](#tab/bash)
306342

@@ -309,12 +345,12 @@ az group delete \
309345
--resource-group $RESOURCE_GROUP
310346
```
311347

312-
# [PowerShell](#tab/powershell)
348+
# [Azure PowerShell](#tab/azure-powershell)
313349

314-
```powershell
315-
Remove-AzResourceGroup -Name $RESOURCE_GROUP -Force
350+
```azurepowershell
351+
Remove-AzResourceGroup -Name $ResourceGroupName -Force
316352
```
317353

318354
---
319355

320-
This command deletes the entire resource group including the Container Apps instance, storage account, Log Analytics workspace, and any other resources in the resource group.
356+

0 commit comments

Comments
 (0)