Share via


Azure CLI を使用して、Azure Cosmos DB for Table のサーバーレス アカウントとテーブルを作成する

適用対象: Table

この記事のスクリプトでは、Azure Cosmos DB for Table のサーバーレス アカウントとテーブルが作成されます。

前提条件

  • Azure サブスクリプションをお持ちでない場合は、開始する前に Azure 無料アカウントを作成してください。

  • このスクリプトでは、Azure CLI バージョン 2.12.1 以降が必要となります。

    • このスクリプトは、Azure Cloud Shell の Bash 環境で実行できます。 Cloud Shell が開いたら、シェル ウィンドウの左上にある環境フィールドで Bash が表示されていることを確認択してください。 Cloud Shell には常に Azure CLI の最新バージョンがあります。

      Cloud Shell は、Azure portal へのサインインに使用したアカウントで自動的に認証されます。 az account set を使用して、別のサブスクリプションでサインインすることができ、<subscriptionId> をご使用の Azure サブスクリプション ID に置き換えます。

      subscription="<subscriptionId>" # add subscription here
      
      az account set -s $subscription # ...or use 'az login'
      
    • 必要に応じて、Azure CLI をインストールしてスクリプトをローカルで実行できます。 az version を実行して、Azure CLI のバージョンと、インストールされている依存ライブラリを確認し、アップグレードする必要がある場合は az upgrade を実行します。 メッセージが表示されたら、Azure CLI 拡張機能をインストールします。 Windows または macOS を実行している場合は、Docker コンテナーで Azure CLI を実行することを検討してください。

      ローカル インストールを使用する場合は、az login を実行して Azure にサインインし、プロンプトに従います。 その他のサインイン オプションについては、Azure CLI でのサインインに関するページを参照してください。

サンプル スクリプト

次のスクリプトを実行して、Azure リソース グループ、Azure Cosmos DB for Table サーバーレス アカウント、および Table 用 API テーブルを作成します。 リソースの作成には、時間がかかる場合があります。

# Create a Table API serverless account and table

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-cosmosdb-rg-$randomIdentifier"
tag="serverless-table-cosmosdb"
account="msdocs-account-cosmos-$randomIdentifier" #needs to be lower case
table="msdocs-table-cosmos-$randomIdentifier"

# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $tag

# Create a Cosmos account for Table API
echo "Creating $account"
az cosmosdb create --name $account --resource-group $resourceGroup --capabilities EnableTable EnableServerless --default-consistency-level Eventual --locations regionName="$location" failoverPriority=0 isZoneRedundant=False \

# Create a Table API Table
az cosmosdb table create --account-name $account --resource-group $resourceGroup --name $table

このスクリプトでは以下のコマンドを使用します。

  • az group create: すべてのリソースを格納するリソース グループが作成されます。
  • az cosmosdb create--capabilities EnableTable EnableServerless を追加: Table 用 API の Azure Cosmos DB サーバーレス アカウントを作成します。
  • az cosmosdb table create: Azure Cosmos DB for Table のテーブルを作成します。

リソースをクリーンアップする

作成したリソースが必要ない場合は、az group delete コマンドを使用して、リソース グループとそれに含まれるすべてのリソースを削除します。 これらのリソースには、Azure Cosmos DB アカウントとテーブルが含まれます。 リソースの削除には、時間がかかる場合があります。

az group delete --name $resourceGroup

次の手順

Azure Cosmos DB CLI のドキュメント