Azure CLIをインストールする手順まとめ

AzureCLI AzureCLI

[az vm create] などのazコマンドを利用してAzureをコマンドで操作することができるようにAzure CLIを自分の端末にインストールする方法をまとめました。

Azure CLIのほかにも、古くから存在するAzure powershellもありますが、azコマンドのほうが構文が短いですし、windowsだとコマンドプロンプト上でも利用できるのでお勧めです。

今回はwindowsでのイントールを前提にしています。

広告

Azure CLI のダウンロード

以下のMicrosoft公式ドキュメントから、青いボタンの「Azure CLIの最新のリリース」を押してインストーラーをダウンロードします。

Windows 用 Azure CLI をインストールする
Windows に Azure CLI をインストールするには、PowerShell または MSI インストーラーを使用する必要があります。これにより、Windows コマンドプロンプト (CMD) を使用して CLI にアクセスできるよ...
Microsoft公式ドキュメントからAzureCLIのインストーラーをダウンロード

Azure CLI のインストール

ダウンロードが完了したら [azure-cli-xx.xx.xx.msi](今回は[azure-cli-2.32.0.msi]でした)を起動させてインストールしていきます。

AzureCLIインストーラーを起動して表示されるインストール画面
AzureCLIインストーラーでインストール中の画面
AzureCLIインストーラーでインストールが完了した画面

これでAzureCLIのインストールは完了です。

azコマンドでバージョン確認

インストールが完了したら、azコマンドを利用できるか確認します。

powershellまたは、コマンドプロンプトを開き、[az –version] を入力します。

以下、コマンドプロンプトの実行結果です。

コマンドプロンプトでの[az --version]入力結果

powershellでも実行できます。

powershellで[az --version]を入力した結果

Azure環境へのログイン

最後にAzure環境にログインしてみます。

Azure環境へのログインには [az login] を利用します。

以下、コマンドプロンプトでの実行結果です。

C:\>az login
The default web browser has been opened at https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.

[az login] を実行すると以下の通りブラウザで認証を求められるので、Azureを利用可能なAzureADのユーザもしくはMicrosoftアカウントでサインインします。

[az login] 入力後にブラウザで表示されるサインイン画面

ログインが成功するとブラウザでは以下の画面が表示され、

サインイン成功後にブラウザで表示される画面

コマンドプロンプト上ではサインインしたサブスクリプションの情報が表示されます。

C:\>az login
The default web browser has been opened at https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.
[
  {
    "cloudName": "AzureCloud",
    "homeTenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "isDefault": true,
    "managedByTenants": [
      {
        "tenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
      }
    ],
    "name": "XXXX",
    "state": "Enabled",
    "tenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "user": {
      "name": "xxxxxxxxxx@xxxxxxxxxxxxxxxxx.onmicrosoft.com",
      "type": "user"
    }
  }
]

C:\>

[参考] サブスクリプションの変更方法

AzureCLI でAzureを操作する際には、1つのサブスクリプションが選択された状態での操作しか対応していません。

なのでもし複数のサブスクリプションを利用している場合にはサブスクリプション切り替える必要があります。

サブスクリプションを変更する方法については以下の記事でまとめているので参考にしてください。

AzureCLIでサブスクリプションを変更する
AzureCLI では1つのサブスクリプションが選択された状態での操作しか対応していません。 なので複数のサブスクリプションを利用している場合には、ログイン後にデフォルトで選択されるサブスクリプションがリソースを作成したいサブスクリプション...

[参考] ヘルプコマンドを実行する

ここからは参考としての情報ですが、AzureCLIでazコマンドのオプションなどが分からなくなったときに利用するヘルプコマンドを紹介します。

azコマンドに慣れないうちは一番利用するコマンドだと思います。

[az -h] を実行する

以下では [az -h] を実行してazに続くコマンドとしてどのようなコマンドがあるのかを確認します。

PS C:\> az -h

Group
    az

Subgroups:
    account                                   : Manage Azure subscription information.
    acr                                       : Manage private registries with Azure Container
                                                Registries.
    ad                                        : Manage Azure Active Directory Graph entities needed
                                                for Role Based Access Control.
    advisor                                   : Manage Azure Advisor.
      ・・・・・[中略]・・・・・
    vm                                        : Manage Linux or Windows virtual machines.
    vmss                                      : Manage groupings of virtual machines in an Azure
                                                Virtual Machine Scale Set (VMSS).
    webapp                                    : Manage web apps.

Commands:
    configure                                 : Manage Azure CLI configuration. This command is
                                                interactive.
    feedback                                  : Send feedback to the Azure CLI Team.
    find                                      : I'm an AI robot, my advice is based on our Azure
                                                documentation as well as the usage patterns of Azure
                                                CLI and Azure ARM users. Using me improves Azure
                                                products and documentation.
    interactive                     [Preview] : Start interactive mode. Installs the
                                                Interactive extension if not installed already.
    login                                     : Log in to Azure.
    logout                                    : Log out to remove access to Azure subscriptions.
    rest                                      : Invoke a custom request.
    upgrade                         [Preview] : Upgrade Azure CLI and extensions.
    version                                   : Show the versions of Azure CLI modules and
                                                extensions in JSON format by default or format
                                                configured by --output.

コマンドの例文の確認も可能

「az vm create」などのリソース作成コマンドの後にヘルプコマンドを利用すると例文も表示してくれます。

ここでは「NSGを作成する際にどのようなオプションが必要となるのか?利用できるのか?」についてヘルプコマンドを利用して調べてみます。

以下の「Examples」部分が例文です。

PS C:\> az network nsg create -h

Command
    az network nsg create : Create a network security group.

Arguments
    --name -n           [Required] : Name of the network security group.
    --resource-group -g [Required] : Name of resource group. You can configure the default group
                                     using `az configure --defaults group=<name>`.
    --location -l                  : Location. Values from: `az account list-locations`. You can
                                     configure the default location using `az configure --defaults
                                     location=<location>`.
    --tags                         : Space-separated tags: key[=value] [key[=value] ...]. Use "" to
                                     clear existing tags.

Global Arguments
    --debug                        : Increase logging verbosity to show all debug logs.
    --help -h                      : Show this help message and exit.
    --only-show-errors             : Only show errors, suppressing warnings.
    --output -o                    : Output format.  Allowed values: json, jsonc, none, table, tsv,
                                     yaml, yamlc.  Default: json.
    --query                        : JMESPath query string. See http://jmespath.org/ for more
                                     information and examples.
    --subscription                 : Name or ID of subscription. You can configure the default
                                     subscription using `az account set -s NAME_OR_ID`.
    --verbose                      : Increase logging verbosity. Use --debug for full debug logs.

Examples
    Create an NSG in a resource group within a region with tags.
        az network nsg create -g MyResourceGroup -n MyNsg --tags super_secure no_80 no_22

To search AI knowledge base for examples, use: az find "az network nsg create"

Please let us know how we are doing: https://aka.ms/azureclihats
PS C:\>

さいごに

AzurePowershellよりも入力するコマンドが短いのでAzureCLIはその部分が非常に良いところだと思います。

AzurePowershellのインストールについては以下の記事でも紹介しています。

Azure PowerShell をインストールしてみた
Powershell上でazモジュールを利用してAzureを操作する必要が出てきたため、Azure Powershellモジュールをインストールしてみました。 インストールするときに利用したコマンドや注意点などをインストールの流れに沿ってご...

また、私もAzurCLIのコマンドに慣れないうちは、上記でも紹介したようなヘルプコマンドを多用していました。
ヘルプコマンドは覚えておくと本当に役に立つと思います。

コメント

タイトルとURLをコピーしました