Azure CLI を使用して Azure サブスクリプションを管理する方法

Azure CLI を使用すると、Azure サブスクリプションの管理、管理グループの作成、サブスクリプションのロックを楽に行うことができます。Azure 内では複数のサブスクリプションを使用できます。 ユーザーは複数の組織に所属でき、また組織はグループ間で特定のリソースへのアクセスを分割できます。 Azure CLI では、サブスクリプションをグローバルに選択することも、コマンドごとに選択することもできます。

サブスクリプション、請求、およびコスト管理の詳細については、課金とコスト管理に関するドキュメントをご覧ください。

用語

テナントは、1 つの組織に関する情報が存在する Microsoft Entra ID のインスタンスです。 "マルチテナント組織" は、Microsoft Entra ID の複数のインスタンスを持つ組織です。 テナントには、1 つ以上の "サブスクリプション""ユーザー" があります。

ユーザーは、Azure にサインインしてリソースを作成、管理、および使用するアカウントです。 ユーザーは複数の "テナント" と "サブスクリプション" にアクセスできます。

"サブスクリプション" とは、Azure をはじめとするクラウド サービスを使用するための Microsoft との契約です。 各リソースは、サブスクリプションに関連付けられています。 サブスクリプションにはリソース グループが含まれます。

Azure "リソース グループ" は、Azure ソリューションの関連するリソースを保持するコンテナーです。 サブスクリプション内のリソース グループを管理する方法については、「Azure CLI を使用して Azure リソース グループを管理する方法」をご覧ください。

アクティブなテナントを取得する

az account tenant list または az account show を使用して、アクティブなテナント ID を取得します。

az account tenant list

az account show

アクティブなテナントを変更する

テナントを切り替えるには、2 つのオプションがあります。

  • アクティブなサブスクリプションを変更します。

  • 目的のテナント内のユーザーとしてサインインします。 az login を使用してアクティブなテナントを変更し、所属するサブスクリプションの一覧を更新します。

    # sign in as a different user
    az login --user <myAlias@myCompany.com> --password <myPassword>
    
    # sign in with a different tenant
    az login --tenant <myTenantID>
    

    組織で多要素認証が必要な場合は、az login --user を使用すると、こちらのエラーが発生することがあります。

    Due to a configuration change made by your administrator, or because you moved to a new location, you must use multi-factor authentication to access...
    

    代替の az login --tenant コマンドを使用すると、HTTPS ページを開いて指定されたコードを入力するように求められます。 その後、多要素認証を使用して正常にサインインできます。 Azure CLI を使用したサインイン オプションについて詳しくは、「Azure CLI を使用してサインインする」をご覧ください。

サブスクリプション情報を取得する

ほとんどの Azure CLI コマンドは、サブスクリプション内で動作します。 コマンドで --subscription パラメーターを使用して、どのサブスクリプションを使用するかを指定できます。 サブスクリプションを指定しない場合、コマンドでは、現在アクティブなサブスクリプションが使用されます。

現在使用しているサブスクリプションの確認、または使用可能なサブスクリプションの一覧の取得を行うには、az account show または az account list コマンドを実行します。 これらのコマンドのその他の使用例については、「Bash を Azure CLI で使用する方法」を参照してください。

サブスクリプション情報を取得する方法を示す例を次に示します。

# get the current default subscription using show
az account show --output table

# get the current default subscription using list
az account list --query "[?isDefault]"

# get a subscription that contains search words or phrases
az account list --query "[?contains(name,'search phrase')].{SubscriptionName:name, SubscriptionID:id, TenantID:tenantId}" --output table

スクリプト内で使用するために、サブスクリプション情報を変数に格納することもできます。

# store the default subscription in a variable
subscriptionId="$(az account list --query "[?isDefault].id" --output tsv)"
echo $subscriptionId

# store a subscription of certain name in a variable
subscriptionId="$(az account list --query "[?name=='my case sensitive subscription full name'].id" --output tsv)"
echo $subscriptionId

ヒント

--output パラメーターはグローバル パラメーターであり、すべてのコマンドで使用できます。 table 値を指定すると、出力がわかりやすい形式で表示されます。 詳細については、「Azure CLI コマンドの出力形式」をご覧ください。

アクティブなサブスクリプションを変更する

サブスクリプションには、名前と ID の両方があります。 別のサブスクリプションに切り替えるには、目的のサブスクリプション ID または名前を指定して az account set を使用できます。

# change the active subscription using the subscription name
az account set --subscription "My Demos"

# change the active subscription using the subscription ID
az account set --subscription "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

変数を使用してサブスクリプションを変更することもできます。 例を次に示します。

# change the active subscription using a variable
subscriptionId="$(az account list --query "[?name=='my case sensitive subscription full name'].id" --output tsv)"
az account set --subscription $subscriptionId

別のテナント内のサブスクリプションに変更すると、アクティブなテナントも変更されます。 Microsoft Entra テナントに新しいサブスクリプションを追加する方法については、「Azure サブスクリプションを Microsoft Entra テナントに関連付けるまたは追加する」をご覧ください。

"... のサブスクリプションは存在しません..." というエラーが発生した場合、可能な解決策については、「トラブルシューティング」を参照してください。

Azure 管理グループを作成する

Azure 管理グループにはサブスクリプションが含まれます。 管理グループは、それらのサブスクリプションのアクセス、ポリシー、コンプライアンスを管理するための手段を提供します。 詳細については、「Azure 管理グループとは」をご覧ください。

az account management-group コマンドを使用して、Azure 管理グループを作成および管理します。

az account management-group create コマンドを使用して、いくつかのサブスクリプションの管理グループを作成できます。

az account management-group create --name Contoso01

すべての管理グループを表示するには、az account management-group list コマンドを使用します。

az account management-group list

az account management-group subscription add コマンドを使用して、新しいグループにサブスクリプションを追加します。

az account management-group subscription add --name Contoso01 --subscription "My Demos"
az account management-group subscription add --name Contoso01 --subscription "My Second Demos"

サブスクリプションを削除するには、az account management-group subscription remove コマンドを使用します。

az account management-group subscription remove --name Contoso01 --subscription "My Demos"

管理グループを削除するには、az account management-group delete コマンドを実行します。

az account management-group delete --name Contoso01

サブスクリプションを削除しても、あるいは管理グループを削除しても、サブスクリプションは削除も非アクティブ化もされません。

Azure サブスクリプション ロックを設定する

管理者は、ユーザーがサブスクリプションを削除または変更できないようにするために、場合によってサブスクリプションをロックする必要があります。 詳細については、「リソースのロックによる予期せぬ変更の防止」を参照してください。

Azure CLI では、az account lock コマンドを使用します。 たとえば、az account lock create コマンドを実行すると、ユーザーはサブスクリプションを削除できなくなります。

az account lock create --name "Cannot delete subscription" --lock-type CanNotDelete

Note

ロックを contributor 作成または変更するには、サブスクリプションに対するアクセス許可が必要です。

サブスクリプションの現在のロックを確認するには、az account lock list コマンドを使用します。

az account lock list --output table

アカウントを読み取り専用にすると、結果として、閲覧者ロールのアクセス許可をすべてのユーザーに割り当てた場合と同様になります。 個々のユーザーとロールに対してアクセス許可を設定する方法については、「Azure CLI を使用して Azure でのロールの割り当てを追加または削除する」をご覧ください。

ロックの詳細を確認するには、az account lock show コマンドを使用します。

az account lock show --name "Cannot delete subscription"

ロックを削除するには、az account lock delete コマンドを使用します。

az account lock delete --name "Cannot delete subscription"

トラブルシューティング

サブスクリプションが存在しない

このエラーは、誤入力に加え、アクセス許可のタイミングの問題がある場合に発生する可能性があります。 たとえば、"現在のターミナル ウィンドウが開いている間" に新しいサブスクリプションに対するアクセス許可が付与された場合、このエラーが発生することがあります。 解決策としては、ターミナル ウィンドウを閉じて再度開くか、az logout の後に az login を使って、使用可能なサブスクリプションの一覧を更新します。

サブスクリプションの検索と変更に役立つスクリプトを次に示します。

# See what subscription you are currently using.
az account show

# Get a list of available subscriptions.
az account list --output table

# If the subscription you are seeking is not in the list
#   close and reopen your terminal window,
#   or logout and then sign in again.
az logout
az login

# Did your available subscription list change?
az account list --output table

# If the subscription you are seeking is still not in the list,
#    contact your system administrator. You cannot change your
#    subscription to an ID that is not in the list.

# If the subscription you are seeking is now in the list,
#   change your subscription.
az account set --subscription 00000000-0000-0000-0000-00000000000

関連項目