演習 - モジュールをレジストリに発行する

完了

おもちゃ会社が使用するプライベート レジストリを作成しました。 この演習では、以下のことを行います。

  • Web サイト リソース用のモジュールを作成します。
  • CDN のリソース用に別のモジュールを作成します。
  • モジュールをレジストリに発行します。
  • レジストリ内のモジュールを一覧表示します。

この演習では、Visual Studio Code 用の Bicep 拡張機能を使用します。 この拡張機能を Visual Studio Code にインストールしてください。

Web サイト用のモジュールを作成する

Web サイトをデプロイするモジュールを前に作成しました。 ここでは、モジュール ファイルを発行できるように保存します。

  1. Visual Studio Code を開きます。

  2. website.bicep という名前の新しいファイルを作成します。

  3. website.bicep ファイルに以下のコードを貼り付けます。

    @description('The Azure region into which the resources should be deployed.')
    param location string
    
    @description('The name of the App Service app.')
    param appServiceAppName string
    
    @description('The name of the App Service plan.')
    param appServicePlanName string
    
    @description('The name of the App Service plan SKU.')
    param appServicePlanSkuName string
    
    resource appServicePlan 'Microsoft.Web/serverfarms@2022-03-01' = {
      name: appServicePlanName
      location: location
      sku: {
        name: appServicePlanSkuName
      }
    }
    
    resource appServiceApp 'Microsoft.Web/sites@2022-03-01' = {
      name: appServiceAppName
      location: location
      properties: {
        serverFarmId: appServicePlan.id
        httpsOnly: true
      }
    }
    
    @description('The default host name of the App Service app.')
    output appServiceAppHostName string = appServiceApp.properties.defaultHostName
    
  4. ファイルを保存します。

    [ファイル]>[名前を付けて保存] を選択するか、Windows で Ctrl+S (macOS では ⌘+S) を選択できます。 ファイルの保存場所を忘れないようにしてください。 たとえば、保存場所として、templates フォルダーを作成することをお勧めします。

CDN 用のモジュールを作成する

前の手順と同様に、事前に作成されたモジュール ファイルを保存して、すぐに公開できるようにします。

  1. cdn.bicep という名前の新しいファイルを作成します。

  2. cdn.bicep ファイルに以下のコードを貼り付けます。

    @description('The host name (address) of the origin server.')
    param originHostName string
    
    @description('The name of the CDN profile.')
    param profileName string = 'cdn-${uniqueString(resourceGroup().id)}'
    
    @description('The name of the CDN endpoint')
    param endpointName string = 'endpoint-${uniqueString(resourceGroup().id)}'
    
    @description('Indicates whether the CDN endpoint requires HTTPS connections.')
    param httpsOnly bool
    
    var originName = 'my-origin'
    
    resource cdnProfile 'Microsoft.Cdn/profiles@2022-11-01-preview' = {
      name: profileName
      location: 'global'
      sku: {
        name: 'Standard_Microsoft'
      }
    }
    
    resource endpoint 'Microsoft.Cdn/profiles/endpoints@2022-11-01-preview' = {
      parent: cdnProfile
      name: endpointName
      location: 'global'
      properties: {
        originHostHeader: originHostName
        isHttpAllowed: !httpsOnly
        isHttpsAllowed: true
        queryStringCachingBehavior: 'IgnoreQueryString'
        contentTypesToCompress: [
          'text/plain'
          'text/html'
          'text/css'
          'application/x-javascript'
          'text/javascript'
        ]
        isCompressionEnabled: true
        origins: [
          {
            name: originName
            properties: {
              hostName: originHostName
            }
          }
        ]
      }
    }
    
    @description('The host name of the CDN endpoint.')
    output endpointHostName string = endpoint.properties.hostName
    
  3. ファイルを保存します。

モジュールをレジストリに発行する

  1. Visual Studio Code ターミナルで、以下のコマンドを実行します。 YOUR_CONTAINER_REGISTRY_NAME をプライベート レジストリの名前に置き換えてください。

    az bicep publish \
      --file website.bicep \
      --target 'br:YOUR_CONTAINER_REGISTRY_NAME.azurecr.io/website:v1'
    
    az bicep publish \
      --file cdn.bicep \
      --target 'br:YOUR_CONTAINER_REGISTRY_NAME.azurecr.io/cdn:v1'
    

    サインインする必要がないことに注目してください。 Bicep では、Azure CLI のサインイン情報を使用して、レジストリに対してユーザーを認証します。

  2. 以下のコマンドを実行して、レジストリ内の成果物を一覧表示します。

    az acr repository list \
      --name YOUR_CONTAINER_REGISTRY_NAME
    

    出力には、モジュールの名前が表示されます。

    [
      "cdn",
      "website"
    ]
    
  1. Visual Studio Code ターミナルで、以下のコマンドを実行します。 YOUR_CONTAINER_REGISTRY_NAME をプライベート レジストリの名前に置き換えてください。

    bicep publish website.bicep `
      --target 'br:YOUR_CONTAINER_REGISTRY_NAME.azurecr.io/website:v1'
    
    bicep publish cdn.bicep `
      --target 'br:YOUR_CONTAINER_REGISTRY_NAME.azurecr.io/cdn:v1'
    

    サインインする必要がないことに注目してください。 Bicep では、Azure PowerShell のサインイン情報を使用して、レジストリに対してユーザーを認証します。

  2. 以下のコマンドを実行して、レジストリ内の成果物を一覧表示します。

    Get-AzContainerRegistryRepository -RegistryName YOUR_CONTAINER_REGISTRY_NAME
    

    出力には、モジュールの名前が表示されます。

    cdn
    website
    

また、Azure portal を使用して、レジストリ内のモジュールを一覧表示することもできます。 リソース グループの [概要] タブから YOUR_CONTAINER_REGISTRY_NAME を選択し、[リポジトリ] を選択します。 このモジュールの公判で Azure portal に接続します。