演習 - テンプレート スペックを更新してバージョン管理する

完了

これで、Azure Cosmos DB テンプレート の仕様が組織全体で使用され、多数の新しい Azure Cosmos DB アカウントがプロビジョニングされるようになりました。 したがって、それらのすべてが連続バックアップを使用するように構成されます。

セキュリティ チームは最近、Azure Cosmos DB のセキュリティ機能を確認しました。 新しいアカウントでは、Microsoft Entra 認証と Azure Cosmos DB ロールベースのアクセス制御を使用する必要があると判断しました。

この演習では、更新された認証構成を含む新しいバージョンでテンプレート スペックを更新します。

このプロセスでは、次のことを行います。

  • テンプレートを更新して、バックアップ ポリシーを再構成します。
  • テンプレート スペックの新しいバージョンを発行します。
  • テンプレート スペックが更新されたことを確認します。
  • 別の Azure Cosmos DB アカウントをデプロイして、テンプレート スペックの新しいバージョンをテストします。

テンプレートを更新する

  1. Visual Studio Code で、 azuredeploy.json ファイルを開きます。

  2. azuredeploy.json ファイルを更新して、次の変更を含めます。

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "location": {
          "type": "string",
          "defaultValue": "[resourceGroup().location]",
          "metadata": {
            "description": "The Azure region into which the Cosmos DB resources should be deployed."
          }
        },
        "cosmosDBAccountName": {
          "type": "string",
          "defaultValue": "[concat('toy-', uniqueString(resourceGroup().id))]",
          "maxLength": 44,
          "minLength": 3,
          "metadata": {
            "description": "The name of the Cosmos DB account. This name must be globally unique, and it must only include lowercase letters, numbers, and hyphens."
          }
        },
        "roleDefinitionFriendlyName": {
          "type": "string",
          "defaultValue": "Read and Write",
          "metadata": {
            "description": "A descriptive name for the role definition."
          }
        },
        "roleDefinitionDataActions": {
          "type": "array",
          "defaultValue": [
            "Microsoft.DocumentDB/databaseAccounts/readMetadata",
            "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/*"
          ],
          "metadata": {
            "description": "The list of actions that the role definition permits."
          }
        },
        "roleAssignmentPrincipalId": {
          "type": "string",
          "metadata": {
            "description": "The object ID of the Azure AD principal that should be granted access using the role definition."
          }
        }
      },
      "variables": {
        "roleDefinitionName": "[guid('sql-role-definition', resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDBAccountName')))]",
        "roleAssignmentName": "[guid('sql-role-assignment', resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDBAccountName')))]"
      },
      "resources": [
        {
          "type": "Microsoft.DocumentDB/databaseAccounts",
          "apiVersion": "2021-04-15",
          "name": "[parameters('cosmosDBAccountName')]",
          "kind": "GlobalDocumentDB",
          "location": "[parameters('location')]",
          "properties": {
            "consistencyPolicy": {
              "defaultConsistencyLevel": "Session"
            },
            "locations": [
              {
                "locationName": "[parameters('location')]",
                "failoverPriority": 0,
                "isZoneRedundant": false
              }
            ],
            "databaseAccountOfferType": "Standard",
            "enableAutomaticFailover": false,
            "enableMultipleWriteLocations": false,
            "backupPolicy": {
              "type": "Continuous"
            }
          }
        },
        {
          "type": "Microsoft.DocumentDB/databaseAccounts/sqlRoleDefinitions",
          "apiVersion": "2021-04-15",
          "name": "[format('{0}/{1}', parameters('cosmosDBAccountName'), variables('roleDefinitionName'))]",
          "properties": {
            "roleName": "[parameters('roleDefinitionFriendlyName')]",
            "type": "CustomRole",
            "assignableScopes": [
              "[resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDBAccountName'))]"
            ],
            "permissions": [
              {
                "dataActions": "[parameters('roleDefinitionDataActions')]"
              }
            ]
          },
          "dependsOn": [
            "[resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDBAccountName'))]"
          ]
        },
        {
          "type": "Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments",
          "apiVersion": "2021-04-15",
          "name": "[format('{0}/{1}', parameters('cosmosDBAccountName'), variables('roleAssignmentName'))]",
          "properties": {
            "roleDefinitionId": "[resourceId('Microsoft.DocumentDB/databaseAccounts/sqlRoleDefinitions', parameters('cosmosDBAccountName'), variables('roleDefinitionName'))]",
            "principalId": "[parameters('roleAssignmentPrincipalId')]",
            "scope": "[resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDBAccountName'))]"
          },
          "dependsOn": [
            "[resourceId('Microsoft.DocumentDB/databaseAccounts', parameters('cosmosDBAccountName'))]",
            "[resourceId('Microsoft.DocumentDB/databaseAccounts/sqlRoleDefinitions', parameters('cosmosDBAccountName'), variables('roleDefinitionName'))]"
          ]
        }
      ]
    }
    
  3. ファイルを保存します。

  1. Visual Studio Code で main.bicep ファイルを開きます。

  2. main.bicep ファイルを更新して、次の変更を含めます。

    @description('The Azure region into which the Cosmos DB resources should be deployed.')
    param location string = resourceGroup().location
    
    @description('The name of the Cosmos DB account. This name must be globally unique, and it must only include lowercase letters, numbers, and hyphens.')
    @minLength(3)
    @maxLength(44)
    param cosmosDBAccountName string = 'toy-${uniqueString(resourceGroup().id)}'
    
    @description('A descriptive name for the role definition.')
    param roleDefinitionFriendlyName string = 'Read and Write'
    
    @description('The list of actions that the role definition permits.')
    param roleDefinitionDataActions array = [
      'Microsoft.DocumentDB/databaseAccounts/readMetadata'
      'Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/*'
    ]
    
    @description('The object ID of the Azure AD principal that should be granted access using the role definition.')
    param roleAssignmentPrincipalId string
    
    var roleDefinitionName = guid('sql-role-definition', cosmosDBAccount.id)
    var roleAssignmentName = guid('sql-role-assignment', cosmosDBAccount.id)
    
    resource cosmosDBAccount 'Microsoft.DocumentDB/databaseAccounts@2021-04-15' = {
      name: cosmosDBAccountName
      kind: 'GlobalDocumentDB'
      location: location
      properties: {
        consistencyPolicy: {
          defaultConsistencyLevel: 'Session'
        }
        locations: [
          {
            locationName: location
            failoverPriority: 0
            isZoneRedundant: false
          }
        ]
        databaseAccountOfferType: 'Standard'
        enableAutomaticFailover: false
        enableMultipleWriteLocations: false
      }
    }
    
    resource roleDefinition 'Microsoft.DocumentDB/databaseAccounts/sqlRoleDefinitions@2021-04-15' = {
      parent: cosmosDBAccount
      name: roleDefinitionName
      properties: {
        roleName: roleDefinitionFriendlyName
        type: 'CustomRole'
        assignableScopes: [
          cosmosDBAccount.id
        ]
        permissions: [
          {
            dataActions: roleDefinitionDataActions
          }
        ]
      }
    }
    
    resource roleAssignment 'Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments@2021-04-15' = {
      parent: cosmosDBAccount
      name: roleAssignmentName
      properties: {
        roleDefinitionId: roleDefinition.id
        principalId: roleAssignmentPrincipalId
        scope: cosmosDBAccount.id
      }
    }
    
  3. ファイルを保存します。

テンプレート スペックの新しいバージョンを発行する

Visual Studio Code ターミナルから、この Azure PowerShell コマンドレットを使用してテンプレート スペックを発行します。

New-AzTemplateSpec `
  -ResourceGroupName <rgn>[sandbox resource group name]</rgn> `
  -Name ToyCosmosDBAccount `
  -Version '2.0' `
  -VersionDescription 'Adds Cosmos DB role-based access control.' `
  -TemplateFile main.bicep
New-AzTemplateSpec `
  -ResourceGroupName <rgn>[sandbox resource group name]</rgn> `
  -Name ToyCosmosDBAccount `
  -Version '2.0' `
  -VersionDescription 'Adds Cosmos DB role-based access control.' `
  -TemplateFile azuredeploy.json

Visual Studio Code ターミナルから、この Azure CLI コマンドを使用してテンプレート スペックを発行します。

az ts create \
  --name ToyCosmosDBAccount \
  --version 2.0 \
  --version-description "Adds Cosmos DB role-based access control." \
  --template-file main.bicep
az ts create \
  --name ToyCosmosDBAccount \
  --version 2.0 \
  --version-description "Adds Cosmos DB role-based access control." \
  --template-file azuredeploy.json

テンプレート スペックを確認する

  1. ブラウザーで、Azure portal に移動します。 ご利用のリソース グループにアクセスします。

  2. テンプレート スペックを選択します。最新バージョンが 2.0 として一覧表示されていることに注意してください。

    テンプレート スペックの Azure portal インターフェイスのスクリーンショット。最新バージョンが 2.0 と表示されています。

  3. バージョンメニュー項目を選択します。 両方のバージョンが一覧表示されていることに注意してください。

    テンプレート スペックの Azure portal インターフェイスのスクリーンショット。バージョンの一覧が 1.0 および 2.0 として示されています。

    テンプレート スペックのバージョンを使用すると、必要に応じて以前のバージョンのテンプレート スペックに戻ることができます。

新しいテンプレート スペック バージョンをデプロイする

  1. 次の Azure PowerShell コマンドを実行して、新しいテンプレート スペック バージョンのリソース ID を取得します。

    $templateSpecVersionResourceId = ( `
       Get-AzTemplateSpec `
          -ResourceGroupName <rgn>[sandbox resource group name]</rgn> `
          -Name ToyCosmosDBAccount `
          -Version 2.0 `
       ).Versions[0].Id
    

    テンプレート スペック バージョンのリソース ID を取得するには、 Versions プロパティを使用します。

  2. 新しいテンプレート スペック バージョンには、ユーザー プリンシパル ID のパラメーターがあります。 次のコマンドを使用して、独自のユーザー アカウントのプリンシパル ID を取得します。

    $token = (Get-AzAccessToken -ResourceUrl "https://graph.windows.net/").Token
    $userObjectId = (Invoke-RestMethod -Uri 'https://graph.windows.net/me?api-version=1.6' -Headers @{ 'Authorization' = "Bearer $token"}).objectID
    

    このコマンドでは、Microsoft Graph API を使用して、独自のユーザー プロファイルに対してクエリを実行します。

  3. Visual Studio Code ターミナルから、この Azure PowerShell コマンドを使用してテンプレート スペックをデプロイします。

    New-AzResourceGroupDeployment `
      -TemplateSpecId $templateSpecVersionResourceId `
      -roleAssignmentPrincipalId $userObjectId
    
  1. 次の Azure CLI コマンドを実行して、テンプレート スペック バージョンのリソース ID を取得します。

    id=$(az ts show \
     --name ToyCosmosDBAccount \
     --resource-group "<rgn>[sandbox resource group name]</rgn>" \
     --version "2.0" \
     --query "id")
    
  2. Visual Studio Code ターミナルから、この Azure CLI コマンドを使用してテンプレート スペックをデプロイします。

    az deployment group create \
     --template-spec $id \
     --parameters roleAssignmentPrincipalId="d68d19b3-d7ef-4ae9-9ee4-90695a4e417d"
    

デプロイが完了するまでに 1、2 分かかる場合があります。

デプロイメントを確認する

  1. ブラウザーで、Azure portal に移動します。 ご利用のリソース グループにアクセスします。

  2. デプロイメントの横にある2 成功を選択します。

    リソース グループの概要の Azure portal インターフェイスのスクリーンショット。デプロイ セクションに 2 つの成功が示されています。

  3. 最新のデプロイを選択します。

    デプロイ用の Azure portal インターフェイスのスクリーンショット。2 つのデプロイが一覧表示されています。

  4. 展開の詳細を選択して展開します。 Azure Cosmos DB ロールベースのアクセス制御のリソースがデプロイされていることを確認します。

    Azure Cosmos DB リソースが一覧表示された、特定のデプロイの Azure portal インターフェイスのスクリーンショット。