マネージドIDにCosmosDBへのアクセス権限を付与する方法です。
CosmosDBの管理プレーンへのアクセス権限はAzureポータルからRBACで付与できますが、データプレーンへのアクセス権限はCLIやPowerShellを利用する必要があります。
マネージド ID を使用して Azure 仮想マシンから Azure Cosmos DB に接続する方法
今回はBicepで付与する方法を残します。
- 例としてAzure Container AppsのマネージドIDに付与します
- まずは以下のようにACAのシステムマネージドIDを有効にします
ACAのマネージドID設定(一部抜粋)
resource containerApp 'Microsoft.App/containerApps@2023-05-01' = {
name: appName
location: location
identity: {
type: 'SystemAssigned'
}
properties: {
managedEnvironmentId: environment.id
configuration: {
ingress: {
targetPort: 80
external: true
ipSecurityRestrictions: [ for list in ipAllowList : {
name: list.name
action: list.action
ipAddressRange: list.ipAddressRange
}]
}
}
template: {
containers: [
{
image: 'nginx:latest'
name: 'sample-apps'
resources: {
cpu: json('${cappDedicatedCpu}')
memory: '${cappDedicatedMemory}Gi'
}
}
]
scale: {
minReplicas: 1
}
}
workloadProfileName: 'Consumption'
}
}
output acaPrincipalId string = containerApp.identity.principalId
- ACAのマネージドIDプリンシパルIDに「Cosmos DB 組み込みデータ共同作成者」ロールを付与します
resource cosmosdbRoleDefinition 'Microsoft.DocumentDB/databaseAccounts/sqlRoleDefinitions@2023-04-15' existing = {
parent: cosmosDb
name: '00000000-0000-0000-0000-000000000002'
}
resource roleAssignment 'Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments@2023-04-15' = {
parent: cosmosDb
name: guid(cosmosDb.id, acaPrincipalId, cosmosdbRoleDefinition.id)
properties: {
roleDefinitionId: cosmosdbRoleDefinition.id
principalId: acaPrincipalId
scope: cosmosDb.id
}
}
以上です。