次の方法で共有


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

適用対象: NoSQL MongoDB カサンドラ グレムリン

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

前提条件

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

Azure ポータルの使用

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

ID メニューの項目

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

システム割り当て ID の有効化

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

システム割り当て ID のオブジェクト ID を取得する

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 アカウントが作成または更新されたら、 az cosmosdb identity show コマンドを使用して、割り当てられた ID をフェッチできます。

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 ] を選択します。

ID メニューの項目

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

ユーザー割り当て ID の有効化

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

割り当てる ID をすべて選択する

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 アカウントが作成または更新されたら、 az cosmosdb identity show コマンドを使用して、割り当てられた ID をフェッチできます。

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 アカウントから削除するには、type プロパティの identityNone に設定します。

"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

次のステップ