PowerShell を使用して Azure Cosmos DB アカウントのリージョンを更新する

適用対象: NoSQL MongoDB Cassandra Gremlin Table

この PowerShell スクリプトにより、Azure Cosmos DB アカウントが使用する Azure リージョンが更新されます。 このスクリプトを使用して、Azure リージョンを追加したり、リージョンのフェールオーバー順序を変更したりできます。

注意

Azure を操作するには、Azure Az PowerShell モジュールを使用することをお勧めします。 作業を開始するには、Azure PowerShell のインストールに関する記事を参照してください。 Az PowerShell モジュールに移行する方法については、「AzureRM から Az への Azure PowerShell の移行」を参照してください。

前提条件

  • Azure リソース グループに既存の Azure Cosmos DB アカウントが必要です。

  • このスクリプトには、Azure PowerShell Az 5.4.0 以降が必要です。 インストールされているバージョンを一覧表示するには、Get-Module -ListAvailable Az を実行します。 PowerShell をインストールする必要がある場合は、Azure PowerShell モジュールのインストールに関するページを参照してください。

  • Connect-AzAccount を実行して Azure にサインインします。

サンプル スクリプト

Update-AzCosmosDBAccountRegion コマンドにより、Azure Cosmos DB アカウントの Azure リージョンが更新されます。 このコマンドには、リソース グループ名、Azure Cosmos DB アカウント名、および必要なフェールオーバー順序での Azure リージョンの一覧が必要です。

このスクリプトでは、Get-AzCosmosDBAccount コマンドにより、指定した Azure Cosmos DB アカウントを取得します。 New-AzCosmosDBLocationObject により、PSLocation 型のオブジェクトが作成されます。 Update-AzCosmosDBAccountRegion では、PSLocation パラメーターを使用してアカウント リージョンを更新します。

  • リージョンを追加する場合は、同じ操作内で最初のフェールオーバー リージョンを変更しないでください。 別の操作でフェールオーバーの優先順位を変更します。
  • 他の Azure Cosmos DB アカウントのプロパティを変更するのと同じ操作でリージョンを変更することはできません。 これらの操作は個別に行います。

このサンプルは、NoSQL 用 API アカウントを使用します。 このサンプルを他の API で使用する場合は、関連するプロパティをコピーし、お使いの API 固有のスクリプトに適用してください。

# Reference: Az.CosmosDB | https://docs.microsoft.com/powershell/module/az.cosmosdb
# --------------------------------------------------
# Purpose
# Update Cosmos DB account: Add an Azure region (location)
# NOTE: if adding a region to a single master account, do not change the first 
# region in the same operation. Change failover priority order in a
# separate operation.
# NOTE: this operation will return, but account updates may still be
# occurring. Check the account or Resource Group's activity log for status.
# --------------------------------------------------
# Variables - ***** SUBSTITUTE YOUR VALUES *****
$resourceGroupName = "myResourceGroup" # Resource Group must already exist
$accountName = "myaccount" # Must be all lower case

# Regions ordered by failover priority, with each indicating whether AZ-enabled
# Region AZ status can be checked at https://azure.microsoft.com/global-infrastructure/regions/
$locations = @(
    @{name = "East US"; azEnabled = $true};
    @{name = "West US"; azEnabled = $false};
)
# --------------------------------------------------

Write-Host "Get Cosmos DB account"
$account = Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName -Name $accountName

$i = 0
$locationObjects = @()

ForEach ($location in $locations) {
    $locationObjects += New-AzCosmosDBLocationObject -LocationName $location.name -IsZoneRedundant $location.azEnabled -FailoverPriority ($i++)
}

Write-Host "Update Cosmos DB account region(s)"
Update-AzCosmosDBAccountRegion -InputObject $account -LocationObject $locationObjects

スクリプトは結果を返しますが、更新操作は完了しない可能性があります。 Azure Cosmos DB アカウントのアクティビティ ログを使用して、Azure portal の操作状態を確認します。

Azure リソース グループを削除する

Azure Cosmos DB アカウントを削除する場合は、Remove-AzResourceGroup PowerShell コマンドを使用して、そのリソース グループを削除できます。 このコマンドでは、Azure リソース グループとそれに含まれているすべてのリソース (Azure Cosmos DB アカウントとそのコンテナーおよびデータベースを含む) が削除されます。

Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"

次のステップ