Exercise - Publish a module to your registry

Completed

You've created a private registry for your toy company to use. In this exercise, you will:

  • Create a module for the website resources.
  • Create another module for the resources in the CDN.
  • Publish the modules to your registry.
  • List the modules in the registry.

This exercise uses the Bicep extension for Visual Studio Code. Be sure to install this extension in Visual Studio Code.

Create a module for a website

You previously created a module that deploys a website. Here, you save the module file so you can publish it.

  1. Open Visual Studio Code.

  2. Create a new file named website.bicep.

  3. Paste the following code into the website.bicep file:

    @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. Save the file.

    You can either select File > Save As or select Ctrl+S on Windows (⌘+S on macOS). Be sure to remember where you save the file. For example, you might want to create a templates folder to save it in.

Create a module for a CDN

Similar to the previous steps, you save a precreated module file so that you can publish it soon.

  1. Create a new file named cdn.bicep.

  2. Paste the following code into the cdn.bicep file:

    @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. Save the file.

Publish the modules to your registry

  1. In the Visual Studio Code terminal, run the following commands. Replace YOUR_CONTAINER_REGISTRY_NAME with the name of your private registry.

    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'
    

    Notice that you didn't need to sign in. Bicep uses the sign-in information from the Azure CLI to authenticate you to the registry.

  2. Run the following command to list the artifacts in your registry:

    az acr repository list \
      --name YOUR_CONTAINER_REGISTRY_NAME
    

    The output shows the names of your modules:

    [
      "cdn",
      "website"
    ]
    
  1. In the Visual Studio Code terminal, run the following commands. Replace YOUR_CONTAINER_REGISTRY_NAME with the name of your private registry.

    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'
    

    Notice that you didn't need to sign in. Bicep uses the sign-in information from Azure PowerShell to authenticate you to the registry.

  2. Run the following command to list the artifacts in your registry:

    Get-AzContainerRegistryRepository -RegistryName YOUR_CONTAINER_REGISTRY_NAME
    

    The output shows the names of your modules:

    cdn
    website
    

You can also use the Azure portal to list the modules in your registry. From your resource group's Overview tab, select YOUR_CONTAINER_REGISTRY_NAME and then select Repositories. You connect to the Azure portal later in this module.