Mastering Azure CLI: Using Subscription Commands
The Azure Cloud Shell (Azure CLI) offers a powerful way to manage your Azure resources. One of its key features is the ability to manage Azure subscriptions, a crucial aspect for organizations with multiple Azure deployments. Let's dive into how you can efficiently use the Azure CLI to manage your subscriptions.
Before we start, ensure you've logged in to Azure CLI by running the command `az login`. This will grant you access to your Azure subscriptions.
Managing Azure Subscriptions
The Azure CLI provides several commands to manage your subscriptions. Let's explore some of the key operations.
First, you can list all your subscriptions with the `az account list` command. This will display a list of your subscriptions with their IDs, names, and statuses.
Selecting a Subscription
To work with a specific subscription, you need to set it as the current context. Use the `az account set` command followed by the subscription name or ID. For example, `az account set --subscription "MySubscription"`.
You can also set the default subscription for your current session with the `-d` flag, like so: `az account set -d "MySubscription"`. This will automatically use "MySubscription" for subsequent commands until you change it.
Switching Between Subscriptions
Once you've set a default subscription, you can switch between subscriptions using the `az account list` command and then setting the desired subscription with `az account set`.
For a more seamless switch, consider using an alias or exporting the subscription name or ID to your shell environment variable. This will allow you to quickly switch between subscriptions with just a simple command.
Handling Subscriptions in Azure CLI
Besides setting subscriptions, the Azure CLI offers commands to list, create, update, and delete subscriptions. Let's briefly explore these operations.
To create a new subscription, use the `az account subscription create` command. This will prompt you to fill in the required information. Similarly, you can update a subscription's details with `az account subscription update` and delete a subscription with `az account subscription delete`.
Using Azure CLI with Multiple Subscriptions
When working with multiple subscriptions, it's crucial to understand how the Azure CLI manages them. By default, the Azure CLI uses the subscription specified in the Azure portal's context. You can view this with the `az account show` command.
If you want to use a different subscription, you must set it as the current context using the `az account set` command. This ensures that the commands you run will use the specified subscription.
In conclusion, the Azure CLI provides a robust set of commands for managing Azure subscriptions. By mastering these commands, you can streamline your workflow and increase your productivity. Happy coding!