Azure CLI Set Subscription for Azure Management

Author

Reads 721

Men and Women Sitting in front of Computers and a Large Screen
Credit: pexels.com, Men and Women Sitting in front of Computers and a Large Screen

To set a subscription for Azure management using the Azure CLI, you need to use the `az account set` command. This command allows you to specify the Azure subscription you want to use for management tasks.

The subscription ID is a unique identifier for your Azure subscription, and you can find it in the Azure portal under the "Subscriptions" section. You can also use the `az account show` command to display the current subscription, which includes the subscription ID.

To set a specific subscription, you need to use the `az account set` command with the subscription ID. For example, if your subscription ID is "12345678-1234-1234-1234-123456789012", you would use the command `az account set --subscription "12345678-1234-1234-1234-123456789012"`.

Getting Started

To learn Azure CLI basics, try our onboarding tutorial that teaches you how to find and change your default subscription.

The tutorial also covers creating an Azure resource containing a random ID in the name, which is a useful skill to have.

Credit: youtube.com, Azure Cli Commands: Login, set default subscription, create resource group & storage account

You can learn syntax differences between Bash, PowerShell, and Cmd by following the link to the Microsoft documentation site.

To get the most out of Azure CLI, it's essential to understand how to set environment variables, which the tutorial will guide you through.

Debugging Azure CLI reference commands is another skill you'll learn in the onboarding tutorial.

Here are the key skills you'll acquire in the onboarding tutorial:

  • Find and change your default subscription.
  • Create an Azure resource containing a random ID in the name.
  • Set environment variables.
  • Learn syntax differences between Bash, PowerShell, and Cmd.
  • Debug Azure CLI reference commands.
  • Get properties from a local JSON file.
  • Delete multiple resources using a script.

Subscription Management

You can change the active subscription in Azure CLI by using the "az account set" command, specifying the desired subscription ID or name. This will switch you to the new subscription.

You can also change your subscription using a variable, which is a useful approach if you need to switch between subscriptions frequently.

If you change to a subscription that is in a different tenant, you will also be changing the active tenant.

To switch to a different subscription, you'll need to know the subscription ID or name, which you can find in the Azure portal or by using the "az account list" command.

You can add a new subscription to your Microsoft Entra tenant, but be aware that this will also change the active tenant.

Troubleshooting

Credit: youtube.com, How to set the current subscription if you have multiple ones under one account - using Azure Cli

If you're experiencing issues with Azure CLI, start by checking your subscription status. Ensure you're logged in to the correct Azure account.

Make sure your Azure CLI version is up to date, as outdated versions can cause compatibility issues. In the article, it was noted that Azure CLI version 2.0.74 was used for the examples.

If you're still having trouble, try resetting your Azure CLI environment variables. This can be done by running the command `az logout` followed by `az login` to re-authenticate.

Clear Your Cache

Clearing your cache is a crucial step in troubleshooting Azure CLI issues. This process is not the same as logging out of Azure.

You'll need to use the az account clear command to update your subscription list. Signing in again will allow you to see the updated list.

Be aware that clearing your cache will prevent you from running Azure CLI commands, including az account set, until you sign in again.

Az Billing Wait

Computer server in data center room
Credit: pexels.com, Computer server in data center room

Az Billing Wait can be a lifesaver when dealing with billing subscriptions that are taking longer than expected to create.

You can place the CLI in a waiting state until a condition of the billing subscription is met by using the 'az billing subscription wait' command.

This command can pause executing the next line of CLI script until the billing subscription is successfully created.

The wait condition can be specified using a custom JMESPath query, such as provisioningState!='InProgress', instanceView.statuses[?code=='PowerState/running'].

Alternatively, you can wait until the provisioningState is at 'Succeeded'.

Prerequisites and Parameters

To set your Azure CLI subscription, you'll need to meet some prerequisites. You'll need at least one Azure Cloud account, which can be done using the free account.

You'll also need to have the Azure CLI installed, and it's recommended to use version 2.20.0 or later. If you're using a Docker Container, the commands will work the same way as on other platforms.

Credit: youtube.com, How to manage multiple subscriptions with Azure CLI

To create resources within Azure, you'll need permissions, which can be granted using a Microsoft account or a service principal. If you're not sure how to create a service principal, you can check out the related article on Azure Service Principals.

Here are the specific requirements you'll need to meet:

  • Azure Cloud account (free account available)
  • Azure CLI installed (version 2.20.0 or later recommended)
  • Permissions to create resources within Azure (Microsoft account or service principal)

Prerequisites

To get started with this tutorial, you'll need a few things. You'll need the Azure CLI, which can be installed on various platforms, including Docker Container, Windows, Linux, and macOS.

The Azure CLI version used in this tutorial is v2.20.0, but the commands will work on other platforms as well.

You'll also need an Azure Cloud account, which can be a free account. This will allow you to create resources within Azure.

Additionally, you'll need permissions to create resources within Azure via a Microsoft account, possibly using a service principal.

Here's a quick rundown of the prerequisites:

  • Azure CLI (v2.20.0 or later)
  • Azure Cloud account (free account is sufficient)
  • Permissions to create resources within Azure via a Microsoft account

Required Parameters

Before you start making requests, you need to know the required parameters. The Request URL is a crucial one - if it doesn't start with a host, the CLI assumes it as an Azure resource ID and prefixes it with the ARM endpoint of the current cloud.

Credit: youtube.com, System of Systems 1.8 - Create Parameters For Requirements

The ARM endpoint is shown by az cloud show --query endpoints.resourceManager. This ensures that your requests are properly routed to the correct Azure resource.

The Request body also needs to be specified, and you can load it from a file using @{file}. This is especially useful when dealing with quoting issues in different terminals.

You can also include space-separated headers in KEY=VALUE format or JSON string, again using @{file} to load from a file if needed. Just remember not to auto-append the Authorization header.

Query parameters in the URL can be space-separated and in KEY=VALUE format or JSON string, and you can load them from a file using @{file} as well.

Commands and Options

The Azure CLI offers a range of commands for managing subscriptions, and understanding these options is crucial for setting up your Azure CLI environment.

To list the subscriptions for a billing account, you can use the `az billing subscription list` command, which is supported for billing accounts with agreement type Microsoft Customer Agreement or Microsoft Partner Agreement.

Credit: youtube.com, Azure CLI Tutorial

The `az billing subscription list` command is classified as a Core command, indicating its fundamental nature in the Azure CLI.

You can use the `az billing subscription show` command to get a subscription by its ID, which is supported for billing accounts with agreement type Microsoft Customer Agreement and Microsoft Partner Agreement.

Here are some key commands and their descriptions:

The `az billing subscription move` command allows you to move a subscription's charges to a new invoice section, but this operation is only supported for billing accounts with agreement type Microsoft Customer Agreement.

Frequently Asked Questions

How do I change my Azure subscription command?

To change your Azure subscription, use the Set-AzContext cmdlet with the Get-AzSubscription cmdlet to retrieve a list of your subscriptions. This allows you to easily switch between your Azure subscriptions.

Walter Brekke

Lead Writer

Walter Brekke is a seasoned writer with a passion for creating informative and engaging content. With a strong background in technology, Walter has established himself as a go-to expert in the field of cloud storage and collaboration. His articles have been widely read and respected, providing valuable insights and solutions to readers.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.