Azure CLI: Specify Subscription
The Azure Command-Line Interface (CLI) is a powerful tool for managing Azure resources, enabling you to work efficiently with Azure from the command line or in scripts. A crucial aspect of using the Azure CLI is knowing how to specify the subscription you want to work with, as each user can be associated with multiple Azure subscriptions. This article will guide you through the process of specifying an Azure subscription using the CLI, ensuring you're always working with the intended resources.
Before diving into the commands, it's essential to understand that the Azure CLI automatically selects the subscription based on your Azure account's default settings. However, situations may arise when you need to work with a specific subscription, making it crucial to know how to switch between them.
Viewing Your Azure Subscriptions
Before specifying a subscription, you should familiarize yourself with the list of subscriptions available to you. The Azure CLI allows you to view all your subscriptions with a single command.
The `az account list` command displays a list of all your Azure subscriptions. Here's a sample output:
```bash az account list [ { "cloudName": "AzureCloud", "id": "/subscriptions/12345678-1234-1234-1234-123456789012", "name": "Subscription 1", "state": "Enabled", "subscriptionId": "12345678-1234-1234-1234-123456789012", "tenantId": "09876543-4321-4321-4321-987654321098" }, { "cloudName": "AzureCloud", "id": "/subscriptions/98765432-3210-3210-3210-987654321098", "name": "Subscription 2", "state": "Enabled", "subscriptionId": "98765432-3210-3210-3210-987654321098", "tenantId": "09876543-4321-4321-4321-987654321098" } ] ```Filtering Subscription List
To filter the list of subscriptions to display only specific details, you can use the `--query` parameter. For instance, to view only the subscription names, use:
Setting an Azure Subscription as Default
After viewing your subscriptions, you might want to set a specific subscription as the default for your current session. To do this, use the `az account set` command, specifying the subscription ID.
```bash az account set --subscription 12345678-1234-1234-1234-123456789012 ```Switching Between Azure Subscriptions
In scenarios where you need to work with a different subscription temporarily, you can switch the active subscription without changing the default for your current session.
To switch to a specific subscription, use the `az account set` command with the `--antage` parameter followed by the subscription name.
Listing Subscriptions in Use
To view the subscriptions you've used recently, you can list them using the `az account list-used` command. This command helps you keep track of your activities across different subscriptions.
Specifying Subscription in Azure CLI Commands
When running Azure CLI commands, you can always specify the subscription using the `--subscription` parameter. This approach ensures that the command is executed within the intended subscription.
For example, to create a new web application in a specific subscription, use:
Using Subscription Aliases
For convenience, you can create aliases for your subscriptions, making it easier to work with them. To create an alias, use the `az account alias` command with the `--name` and `--subscription` parameters:
```bash az account alias --name myAlias --subscription 12345678-1234-1234-1234-123456789012 ```Once the alias is created, you can use it to switch to the subscription or execute commands:
```bash az account set --antage myAlias az webapp create --resource-group myResourceGroup --name myApp --plan Free --antage myAlias ```Understanding how to specify an Azure subscription using the Azure CLI is crucial for managing your resources effectively. By mastering the commands discussed in this article, you'll be able to work efficiently across multiple subscriptions, ensuring that your Azure resources are well-organized and managed appropriately.
As you explore Azure's ecosystem, continue learning and leveraging the power of the Azure CLI to automate tasks, accelerate development, and maximize productivity. Happy coding!