💻

Azure CLI (azコマンド) の基本と主要リソースの操作方法まとめ

に公開

概要

Azure Portal (Web UI) での操作は直感的で分かりやすいですが、定型的な作業の自動化や、多数のリソースの一括管理にはコマンドラインツール (CLI) が非常に強力です。Azure では az というコマンドで操作できる Azure CLI が提供されています。

この記事では、Azure CLI の基本的な使い方から、主要なリソース(リソースグループ, VM, ストレージなど)を操作するための基本的なコマンドまでを網羅的にまとめます。

実行環境とインストール

Azure CLI は Windows, macOS, Linux で利用できます。各環境へのインストールは公式ドキュメントを参照してください。
Azure CLI のインストール

macOS の場合は Homebrew で簡単にインストールできます。

brew update && brew install azure-cli

また、ブラウザ上で利用できる Azure Cloud Shell を使えば、インストール不要ですぐに試すこともできます。

基本的な使い方

1. ログイン

まず最初に、Azure アカウントにログインする必要があります。

az login

このコマンドを実行するとブラウザが起動し、認証を求められます。認証が完了すると、ターミナルにサブスクリプションの情報が JSON 形式で表示されます。

2. アカウントとサブスクリプションの管理

複数のサブスクリプションを利用している場合は、操作対象のサブスクリプションを正しく選択することが重要です。

# ログインしているアカウント情報を表示
az account show

# 利用可能なサブスクリプションの一覧を表示
az account list --output table

# 操作対象のサブスクリプションを設定
az account set --subscription "サブスクリプション名またはID"

3. コマンドの構造

az コマンドは az <group> <subgroup> <command> [parameters] という階層的な構造になっています。
例えば、仮想マシン (vm) を起動 (start) する場合は以下のようになります。

az vm start --name MyVM --resource-group MyResourceGroup

4. 出力形式の変更とクエリ

--output (または -o) オプションで、出力形式を json (デフォルト), table, tsv, yaml などに変更できます。
また、--query オプションで、JMESPath というクエリ言語を使って、出力結果から必要な情報だけを抽出できます。

# リソースグループの一覧をテーブル形式で表示
az group list -o table

# 'japaneast' にあるリソースグループの名前だけを抽出
az group list --query "[?location=='japaneast'].name" -o tsv

主要リソースの操作コマンド例

リソースグループ (group)

Azure のすべてのリソースが属する論理的なコンテナです。

# リソースグループを作成
az group create --name MyResourceGroup --location japaneast

# リソースグループの一覧
az group list -o table

# リソースグループを削除 (中のリソースもすべて削除されるので注意)
az group delete --name MyResourceGroup --yes

仮想マシン (VM) (vm)

# Ubuntu LTS のVMを作成 (SSHキーは自動生成)
az vm create \
  --resource-group MyResourceGroup \
  --name MyVM \
  --image UbuntuLTS \
  --admin-username azureuser \
  --generate-ssh-keys

# VMの一覧を表示
az vm list --resource-group MyResourceGroup -o table

# VMを停止
az vm stop --name MyVM --resource-group MyResourceGroup

# VMを起動
az vm start --name MyVM --resource-group MyResourceGroup

# VMを削除
az vm delete --name MyVM --resource-group MyResourceGroup --yes

ストレージアカウント (storage account)

Blob, File, Queue, Table などのストレージサービスを利用するためのアカウント。

# ストレージアカウント名 (小文字英数字のみ、グローバルで一意)
STORAGE_ACCOUNT_NAME="mystorageaccount$(openssl rand -hex 3)"

# ストレージアカウントを作成
az storage account create \
  --name $STORAGE_ACCOUNT_NAME \
  --resource-group MyResourceGroup \
  --location japaneast \
  --sku Standard_LRS

# ストレージアカウントの接続文字列を取得
az storage account show-connection-string --name $STORAGE_ACCOUNT_NAME

BLOB ストレージ (storage blob)

S3 のようなオブジェクトストレージ。

# コンテナを作成
az storage container create --name mycontainer --account-name $STORAGE_ACCOUNT_NAME

# ファイルをアップロード
az storage blob upload \
  --account-name $STORAGE_ACCOUNT_NAME \
  --container-name mycontainer \
  --name my-file.txt \
  --file ./my-file.txt

# BLOBの一覧を表示
az storage blob list --account-name $STORAGE_ACCOUNT_NAME --container-name mycontainer -o table

App Service (appservicewebapp)

Web アプリケーションをホストするための PaaS。

# App Service プランを作成 (コンピューティングリソース)
az appservice plan create --name MyPlan --resource-group MyResourceGroup --is-linux

# Webアプリを作成
az webapp create \
  --name MyUniqueWebAppName \
  --resource-group MyResourceGroup \
  --plan MyPlan \
  --runtime "NODE:16-lts"

Azure Kubernetes Service (AKS) (aks)

マネージド Kubernetes サービス。

# AKSクラスタを作成
az aks create \
  --resource-group MyResourceGroup \
  --name MyAKSCluster \
  --node-count 1 \
  --enable-addons monitoring \
  --generate-ssh-keys

# kubectl をインストール
az aks install-cli

# AKSクラスタに接続するための認証情報を取得
az aks get-credentials --resource-group MyResourceGroup --name MyAKSCluster

# kubectl でノードを確認
kubectl get nodes

まとめ

Azure CLI は、Azure のリソースを効率的かつ再現性高く管理するための必須ツールです。最初は覚えることが多いように感じるかもしれませんが、基本的なコマンド構造と主要なリソースの操作方法をいくつか覚えておくだけで、日々の運用が格段に楽になります。

az find "キーワード" コマンドを使うと、AIが関連するコマンドを探してくれる機能もあるので、困ったときには活用してください。


この記事はAIによって修正・追記されました.

Discussion