Set Your Azure CLI Context with New Subscription
The Azure CLI is a powerful command-line interface for managing Azure resources. Understanding and effectively using `az cli subscription context` is crucial for streamlining your workflow and avoiding subscription mix-ups. Let's delve into this essential aspect of Azure CLI.
The `az cli subscription context` command helps you switch between Azure subscriptions, a vital function when you're managing multiple subscriptions in a single Azure account or a multi-subscription environment.
Setting the Subscription Context
Before you start working with subscriptions, you need to authenticate and set the subscription context.
You can sign in to Azure using the `az login` command:
```bash az login ```Once logged in, you can list all available subscriptions with:
To set the subscription context, use:
```bash az account set --subscription "subscription-name" ```Replace "subscription-name" with the name of the subscription you want to use.
Using Subscription IDs
Instead of using the subscription name, you can also use the subscription ID:
You can find the subscription ID in the Azure portal or by running `az account list` and matching the ID to the relevant subscription.
Switching Between Multiple Subscriptions
To switch between subscriptions swiftly, you can use the `az account set` command along with a script or a bash function. Here's an example of a bash function you can create:
```bash function switch-to-subscription() { az account set --subscription "$1" } ```This function allows you to switch subscriptions by simply calling `switch-to-subscription "subscription-name"`.
Verifying the Subscription Context
After setting the subscription context, you can verify it using:
```bash az account show ```This command displays the current subscription's details, confirming you're working in the correct subscription.
Checking the subscription context is good practice, especially when working in a multi-subscription environment, to prevent unintentional resource modifications in the wrong subscription.
view the Current Subscription
To quickly view the current subscription's name, you can use:
```bash az account show --query "name" -o tsv ```This command returns only the subscription's name, making it ideal for display in terminal outputs or scripts.
Checking the Global Az CLI Context
Besides the subscription context, you can also check the global Azure CLI context using:
```bash az account show --query "{login:subscriptionId, tenantId, user.name, user.objectId}" ```This command returns the current user's subscription ID, tenant ID, and Azure user name and object ID.
Regularly verifying and switching Azure CLI contexts ensures you're always working in the correct subscription, helping you maintain a clean and organized resource management workflow.
Remember, always be cautious when managing resources to avoid unintended modifications or deletions. Keeping a clear overview of your current subscription context contributes significantly to this. Now go and master your Azure CLI subscription management!