Microsoft Entra ID を使用して Azure Cosmos DB アカウントのマネージド ID を構成する

適用対象: NoSQL MongoDB Cassandra Gremlin Table

Azure リソースのマネージド ID には、Azure サービス向けの、Microsoft Entra ID で自動的に管理される ID が用意されています。 この記事では、Azure Cosmos DB アカウントのマネージド ID を作成する方法について説明します。

前提条件

システム割り当て ID を追加する

Azure ポータルの使用

既存の Azure Cosmos DB アカウントでシステム割り当てマネージド ID を有効にするには、Azure portal でアカウントに移動し、左側のメニューから [ID] を選択します。

The Identity menu entry

[システム割り当て済み] セクションで、[状態] をオンに切り替えて、[保存] を選択します。 システム割り当てマネージド ID の作成を確認するメッセージが表示されます。

Enabling a system-assigned identity

ID の作成と割り当てが完了すると、そのオブジェクト (プリンシパル) ID を取得できます。

Retrieving the object ID of a system-assigned identity

Azure Resource Manager (ARM) テンプレートの使用

重要

マネージド ID を操作するときは、apiVersion2021-03-15 以降を使用してください。

新規または既存の Azure Cosmos DB アカウントでシステム割り当て ID を有効にするには、リソース定義に次のプロパティを含めます。

"identity": {
    "type": "SystemAssigned"
}

ARM テンプレートの resources セクションは、次のようになります。

"resources": [
    {
        "type": " Microsoft.DocumentDB/databaseAccounts",
        "identity": {
            "type": "SystemAssigned"
        },
        // ...
    },
    // ...
]

Azure Cosmos DB アカウントが作成または更新されると、次のプロパティが表示されます。

"identity": {
    "type": "SystemAssigned",
    "tenantId": "<azure-ad-tenant-id>",
    "principalId": "<azure-ad-principal-id>"
}

Azure CLI の使用

新しい Azure Cosmos DB アカウントの作成中にシステム割り当て ID を有効にするには、--assign-identity オプションを追加します。

resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'

az cosmosdb create \
    -n $accountName \
    -g $resourceGroupName \
    --locations regionName='West US 2' failoverPriority=0 isZoneRedundant=False \
    --assign-identity

az cosmosdb identity assign コマンドを使用して、システム割り当て ID を既存のアカウントに追加することもできます。

resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'

az cosmosdb identity assign \
    -n $accountName \
    -g $resourceGroupName

Azure Cosmos DB アカウントが作成または更新されると、割り当てられた ID を az cosmosdb identity show コマンドで取得できます。

resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'

az cosmosdb identity show \
    -n $accountName \
    -g $resourceGroupName
{
    "type": "SystemAssigned",
    "tenantId": "<azure-ad-tenant-id>",
    "principalId": "<azure-ad-principal-id>"
}

ユーザー割り当て ID を追加する

Azure ポータルの使用

既存の Azure Cosmos DB アカウントでユーザー割り当てマネージド ID を有効にするには、Azure portal でアカウントに移動し、左側のメニューから [ID] を選択します。

The Identity menu entry

[ユーザー割り当て済み] セクションで、[追加] を選択します。

Enabling a user-assigned identity

Azure Cosmos DB アカウントに割り当てる ID をすべて検索して選択し、[追加] を選択します。

Selecting all the identities to assign

Azure Resource Manager (ARM) テンプレートの使用

重要

マネージド ID を操作するときは、apiVersion2021-03-15 以降を使用してください。

新規または既存の Azure Cosmos DB アカウントでユーザー割り当て ID を有効にするには、リソース定義に次のプロパティを含めます。

"identity": {
    "type": "UserAssigned",
    "userAssignedIdentities": {
        "<identity-resource-id>": {}
    }
}

ARM テンプレートの resources セクションは、次のようになります。

"resources": [
    {
        "type": " Microsoft.DocumentDB/databaseAccounts",
        "identity": {
            "type": "UserAssigned",
            "userAssignedIdentities": {
                "<identity-resource-id>": {}
            }
        },
        // ...
    },
    // ...
]

Azure Cosmos DB アカウントが作成または更新されると、次のプロパティが表示されます。

"identity": {
    "type": "UserAssigned",
    "tenantId": "<azure-ad-tenant-id>",
    "principalId": "<azure-ad-principal-id>"
}

Azure CLI の使用

Azure Cosmos DB の新規アカウントを作成する際にユーザー割り当て ID を有効にするには、--assign-identity オプションを追加し、割り当てる ID のリソース ID を渡します。

resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'

az cosmosdb create \
    -n $accountName \
    -g $resourceGroupName \
    --locations regionName='West US 2' failoverPriority=0 isZoneRedundant=False \
    --assign-identity <identity-resource-id>

az cosmosdb identity assign コマンドを使用して、ユーザー割り当て ID を既存のアカウントに追加することもできます。

resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'

az cosmosdb identity assign \
    -n $accountName \
    -g $resourceGroupName
    --identities <identity-resource-id>

Azure Cosmos DB アカウントが作成または更新された後、割り当てられた ID を az cosmosdb identity show コマンドで取得できます。

resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'

az cosmosdb identity show \
    -n $accountName \
    -g $resourceGroupName
{
    "type": "UserAssigned",
    "tenantId": "<azure-ad-tenant-id>",
    "principalId": "<azure-ad-principal-id>"
}

システム割り当て ID またはユーザー割り当て ID を削除する

Azure Resource Manager (ARM) テンプレートの使用

重要

マネージド ID を操作するときは、apiVersion2021-03-15 以降を使用してください。

システム割り当て ID を Azure Cosmos DB アカウントから削除するには、identity プロパティの typeNone に設定します。

"identity": {
    "type": "None"
}

Azure CLI の使用

すべてのマネージド ID を Azure Cosmos DB アカウントから削除するには、az cosmosdb identity remove コマンドを使用します。

resourceGroupName='myResourceGroup'
accountName='mycosmosaccount'

az cosmosdb identity remove \
    -n $accountName \
    -g $resourceGroupName

次のステップ