Skip to content

Commit 4f73e07

Browse files
committed
Learn Editor: Update quick-create-cli.md
1 parent be177ad commit 4f73e07

File tree

1 file changed

+86
-50
lines changed

1 file changed

+86
-50
lines changed

articles/batch/quick-create-cli.md

Lines changed: 86 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ title: 'Quickstart: Use the Azure CLI to create a Batch account and run a job'
33
description: Follow this quickstart to use the Azure CLI to create a Batch account, a pool of compute nodes, and a job that runs basic tasks on the pool.
44
ms.topic: quickstart
55
ms.date: 04/12/2023
6-
ms.custom: mvc, devx-track-azurecli, mode-api, linux-related-content
6+
ms.custom: mvc, devx-track-azurecli, mode-api, linux-related-content, innovation-engine
7+
author: (preserved)
8+
ms.author: (preserved)
79
---
810

911
# Quickstart: Use the Azure CLI to create a Batch account and run a job
@@ -20,63 +22,91 @@ After you complete this quickstart, you understand the [key concepts of the Batc
2022

2123
You can run the Azure CLI commands in this quickstart interactively in Azure Cloud Shell. To run the commands in the Cloud Shell, select **Open Cloudshell** at the upper-right corner of a code block. Select **Copy** to copy the code, and paste it into Cloud Shell to run it. You can also [run Cloud Shell from within the Azure portal](https://shell.azure.com). Cloud Shell always uses the latest version of the Azure CLI.
2224

23-
Alternatively, you can [install Azure CLI locally](/cli/azure/install-azure-cli) to run the commands. The steps in this article require Azure CLI version 2.0.20 or later. Run [az version](/cli/azure/reference-index?#az-version) to see your installed version and dependent libraries, and run [az upgrade](/cli/azure/reference-index?#az-upgrade) to upgrade. If you use a local installation, sign in to Azure by using the [az login](/cli/azure/reference-index#az-login) command.
25+
Alternatively, you can [install Azure CLI locally](/cli/azure/install-azure-cli) to run the commands. The steps in this article require Azure CLI version 2.0.20 or later. Run [az version](/cli/azure/reference-index?#az-version) to see your installed version and dependent libraries, and run [az upgrade](/cli/azure/reference-index?#az-upgrade) to upgrade. If you use a local installation, sign in to Azure by using the appropriate command.
2426

2527
>[!NOTE]
2628
>For some regions and subscription types, quota restrictions might cause Batch account or node creation to fail or not complete. In this situation, you can request a quota increase at no charge. For more information, see [Batch service quotas and limits](batch-quota-limit.md).
2729
2830
## Create a resource group
2931

30-
Run the following [az group create](/cli/azure/group#az-group-create) command to create an Azure resource group named `qsBatch` in the `eastus2` Azure region. The resource group is a logical container that holds the Azure resources for this quickstart.
32+
Run the following [az group create](/cli/azure/group#az-group-create) command to create an Azure resource group. The resource group is a logical container that holds the Azure resources for this quickstart.
3133

3234
```azurecli-interactive
35+
export RANDOM_SUFFIX=$(openssl rand -hex 3)
36+
export REGION="canadacentral"
37+
export RESOURCE_GROUP="qsBatch$RANDOM_SUFFIX"
38+
3339
az group create \
34-
--name qsBatch \
35-
--location eastus2
40+
--name $RESOURCE_GROUP \
41+
--location $REGION
42+
```
43+
44+
Results:
45+
46+
<!-- expected_similarity=0.3 -->
47+
48+
```JSON
49+
{
50+
"id": "/subscriptions/xxxxx/resourceGroups/qsBatchxxx",
51+
"location": "eastus2",
52+
"managedBy": null,
53+
"name": "qsBatchxxx",
54+
"properties": {
55+
"provisioningState": "Succeeded"
56+
},
57+
"tags": null,
58+
"type": "Microsoft.Resources/resourceGroups"
59+
}
3660
```
3761

3862
## Create a storage account
3963

4064
Use the [az storage account create](/cli/azure/storage/account#az-storage-account-create) command to create an Azure Storage account to link to your Batch account. Although this quickstart doesn't use the storage account, most real-world Batch workloads use a linked storage account to deploy applications and store input and output data.
4165

42-
Run the following command to create a Standard_LRS SKU storage account named `mybatchstorage` in your resource group:
66+
Run the following command to create a Standard_LRS SKU storage account in your resource group:
4367

4468
```azurecli-interactive
69+
export STORAGE_ACCOUNT="mybatchstorage$RANDOM_SUFFIX"
70+
4571
az storage account create \
46-
--resource-group qsBatch \
47-
--name mybatchstorage \
48-
--location eastus2 \
72+
--resource-group $RESOURCE_GROUP \
73+
--name $STORAGE_ACCOUNT \
74+
--location $REGION \
4975
--sku Standard_LRS
5076
```
5177

5278
## Create a Batch account
5379

54-
Run the following [az batch account create](/cli/azure/batch/account#az-batch-account-create) command to create a Batch account named `mybatchaccount` in your resource group and link it with the `mybatchstorage` storage account.
80+
Run the following [az batch account create](/cli/azure/batch/account#az-batch-account-create) command to create a Batch account in your resource group and link it with the storage account.
5581

5682
```azurecli-interactive
83+
export BATCH_ACCOUNT="mybatchaccount$RANDOM_SUFFIX"
84+
5785
az batch account create \
58-
--name mybatchaccount \
59-
--storage-account mybatchstorage \
60-
--resource-group qsBatch \
61-
--location eastus2
86+
--name $BATCH_ACCOUNT \
87+
--storage-account $STORAGE_ACCOUNT \
88+
--resource-group $RESOURCE_GROUP \
89+
--location $REGION
6290
```
6391

6492
Sign in to the new Batch account by running the [az batch account login](/cli/azure/batch/account#az-batch-account-login) command. Once you authenticate your account with Batch, subsequent `az batch` commands in this session use this account context.
6593

6694
```azurecli-interactive
6795
az batch account login \
68-
--name mybatchaccount \
69-
--resource-group qsBatch \
96+
--name $BATCH_ACCOUNT \
97+
--resource-group $RESOURCE_GROUP \
7098
--shared-key-auth
7199
```
72100

73101
## Create a pool of compute nodes
74102

75-
Run the [az batch pool create](/cli/azure/batch/pool#az-batch-pool-create) command to create a pool of Linux compute nodes in your Batch account. The following example creates a pool named `myPool` that consists of two Standard_A1_v2 size VMs running Ubuntu 20.04 LTS OS. This node size offers a good balance of performance versus cost for this quickstart example.
103+
Run the [az batch pool create](/cli/azure/batch/pool#az-batch-pool-create) command to create a pool of Linux compute nodes in your Batch account. The following example creates a pool that consists of two Standard_A1_v2 size VMs running Ubuntu 20.04 LTS OS. This node size offers a good balance of performance versus cost for this quickstart example.
76104

77105
```azurecli-interactive
106+
export POOL_ID="myPool$RANDOM_SUFFIX"
107+
78108
az batch pool create \
79-
--id myPool \
109+
--id $POOL_ID \
80110
--image canonical:0001-com-ubuntu-server-focal:20_04-lts \
81111
--node-agent-sku-id "batch.node.ubuntu 20.04" \
82112
--target-dedicated-nodes 2 \
@@ -86,20 +116,32 @@ az batch pool create \
86116
Batch creates the pool immediately, but takes a few minutes to allocate and start the compute nodes. To see the pool status, use the [az batch pool show](/cli/azure/batch/pool#az-batch-pool-show) command. This command shows all the properties of the pool, and you can query for specific properties. The following command queries for the pool allocation state:
87117

88118
```azurecli-interactive
89-
az batch pool show --pool-id myPool \
90-
--query "allocationState"
119+
az batch pool show --pool-id $POOL_ID \
120+
--query "{allocationState: allocationState}"
121+
```
122+
123+
Results:
124+
125+
<!-- expected_similarity=0.3 -->
126+
127+
```JSON
128+
{
129+
"allocationState": "resizing"
130+
}
91131
```
92132

93133
While Batch allocates and starts the nodes, the pool is in the `resizing` state. You can create a job and tasks while the pool state is still `resizing`. The pool is ready to run tasks when the allocation state is `steady` and all the nodes are running.
94134

95135
## Create a job
96136

97-
Use the [az batch job create](/cli/azure/batch/job#az-batch-job-create) command to create a Batch job to run on your pool. A Batch job is a logical group of one or more tasks. The job includes settings common to the tasks, such as the pool to run on. The following example creates a job called `myJob` on `myPool` that initially has no tasks.
137+
Use the [az batch job create](/cli/azure/batch/job#az-batch-job-create) command to create a Batch job to run on your pool. A Batch job is a logical group of one or more tasks. The job includes settings common to the tasks, such as the pool to run on. The following example creates a job that initially has no tasks.
98138

99139
```azurecli-interactive
140+
export JOB_ID="myJob$RANDOM_SUFFIX"
141+
100142
az batch job create \
101-
--id myJob \
102-
--pool-id myPool
143+
--id $JOB_ID \
144+
--pool-id $POOL_ID
103145
```
104146

105147
## Create job tasks
@@ -113,22 +155,22 @@ for i in {1..4}
113155
do
114156
az batch task create \
115157
--task-id myTask$i \
116-
--job-id myJob \
158+
--job-id $JOB_ID \
117159
--command-line "/bin/bash -c 'printenv | grep AZ_BATCH; sleep 90s'"
118160
done
119161
```
120162

121-
The command output shows the settings for each task. Batch distributes the tasks to the compute nodes.
163+
Batch distributes the tasks to the compute nodes.
122164

123165
## View task status
124166

125-
After you create the task, Batch queues the task to run on the pool. Once a node is available, the task runs on the node.
167+
After you create the tasks, Batch queues them to run on the pool. Once a node is available, a task runs on the node.
126168

127169
Use the [az batch task show](/cli/azure/batch/task#az-batch-task-show) command to view the status of Batch tasks. The following example shows details about the status of `myTask1`:
128170

129171
```azurecli-interactive
130172
az batch task show \
131-
--job-id myJob \
173+
--job-id $JOB_ID \
132174
--task-id myTask1
133175
```
134176

@@ -139,29 +181,39 @@ The command output includes many details. For example, an `exitCode` of `0` indi
139181
Use the [az batch task file list](/cli/azure/batch/task#az-batch-task-file-show) command to list the files a task created on a node. The following command lists the files that `myTask1` created:
140182

141183
```azurecli-interactive
142-
az batch task file list \
143-
--job-id myJob \
144-
--task-id myTask1 \
145-
--output table
184+
# Wait for task to complete before downloading output
185+
echo "Waiting for task to complete..."
186+
while true; do
187+
STATUS=$(az batch task show --job-id $JOB_ID --task-id myTask1 --query "state" -o tsv)
188+
if [ "$STATUS" == "running" ]; then
189+
break
190+
fi
191+
sleep 10
192+
done
193+
194+
az batch task file list --job-id $JOB_ID --task-id myTask1 --output table
146195
```
147196

148197
Results are similar to the following output:
149198

199+
Results:
200+
201+
<!-- expected_similarity=0.3 -->
202+
150203
```output
151204
Name URL Is Directory Content Length
152205
---------- ---------------------------------------------------------------------------------------- -------------- ----------------
153206
stdout.txt https://mybatchaccount.eastus2.batch.azure.com/jobs/myJob/tasks/myTask1/files/stdout.txt False 695
154207
certs https://mybatchaccount.eastus2.batch.azure.com/jobs/myJob/tasks/myTask1/files/certs True
155208
wd https://mybatchaccount.eastus2.batch.azure.com/jobs/myJob/tasks/myTask1/files/wd True
156209
stderr.txt https://mybatchaccount.eastus2.batch.azure.com/jobs/myJob/tasks/myTask1/files/stderr.txt False 0
157-
158210
```
159211

160212
The [az batch task file download](/cli/azure/batch/task#az-batch-task-file-download) command downloads output files to a local directory. Run the following example to download the *stdout.txt* file:
161213

162214
```azurecli-interactive
163215
az batch task file download \
164-
--job-id myJob \
216+
--job-id $JOB_ID \
165217
--task-id myTask1 \
166218
--file-path stdout.txt \
167219
--destination ./stdout.txt
@@ -187,25 +239,9 @@ AZ_BATCH_ACCOUNT_NAME=mybatchaccount
187239
AZ_BATCH_TASK_USER_IDENTITY=PoolNonAdmin
188240
```
189241

190-
## Clean up resources
191-
192-
If you want to continue with Batch tutorials and samples, you can use the Batch account and linked storage account that you created in this quickstart. There's no charge for the Batch account itself.
193-
194-
Pools and nodes incur charges while the nodes are running, even if they aren't running jobs. When you no longer need a pool, use the [az batch pool delete](/cli/azure/batch/pool#az-batch-pool-delete) command to delete it. Deleting a pool deletes all task output on the nodes, and the nodes themselves.
195-
196-
```azurecli-interactive
197-
az batch pool delete --pool-id myPool
198-
```
199-
200-
When you no longer need any of the resources you created for this quickstart, you can use the [az group delete](/cli/azure/group#az-group-delete) command to delete the resource group and all its resources. To delete the resource group and the storage account, Batch account, node pools, and all related resources, run the following command:
201-
202-
```azurecli-interactive
203-
az group delete --name qsBatch
204-
```
205-
206242
## Next steps
207243

208244
In this quickstart, you created a Batch account and pool, created and ran a Batch job and tasks, and viewed task output from the nodes. Now that you understand the key concepts of the Batch service, you're ready to use Batch with more realistic, larger scale workloads. To learn more about Azure Batch, continue to the Azure Batch tutorials.
209245

210246
> [!div class="nextstepaction"]
211-
> [Tutorial: Run a parallel workload with Azure Batch](./tutorial-parallel-python.md)
247+
> [Tutorial: Run a parallel workload with Azure Batch](./tutorial-parallel-python.md)

0 commit comments

Comments
 (0)