Last year Microsoft announced a new tool Bicep. Bicep is open-source declarative language DSL (Domain Specific Language) by which you can provision Azure resources.

GitHub - Azure/bicep: Bicep is a declarative language for describing and deploying Azure resources
Bicep is a declarative language for describing and deploying Azure resources - GitHub - Azure/bicep: Bicep is a declarative language for describing and deploying Azure resources
https://github.com/Azure/bicep/

If you already have experience with Terraform, Pulumi, or other infrastructure as code tools then you would have the same question as I had "One more DSL? Why?" 🤔. I can give you a reason why you have to take a look at Bicep and take it seriously.

Let`s imagine you following Azure announcements and Azure launch some great features like Azure Container App. Are these features supported in Terraform or Pulumi right after the announcement? The short answer is no. But using Bicep you can be provisioning those resources right after they will be available in Azure 🎉. Sounds great! 🌟 One thing you have to remember before starting using Bicep, it supports only Azure cloud resources. Using Bicep you can not be provisioning resources to other clouds AWS, Google, etc, only Azure.

Let`s try out to provision Azure Container App resources by using Bicep.

Steps:

  • Install prerequisite tools
  • Prepare Visual Studio Code
  • Describe Azure Container App using Bicep
  • Deploy resources

Prerequisites 🔨

Tools you have to install before starting using Bicep environments:

Install Bicep

To install Bicep you have to execute the following command by using the Windows command prompt or Windows Terminal.

az install bicep
Command to install Bicep CLI

After completing the installation verify if Bicep CLI works fine by executing the following command. On-time I wrote this post Bicep CLI had a version Bicep CLI v0.4.1272.

az bicep version
Verify Bicep CLI is installed correctly.

To install the latest version of Bicep CLI just execute az bicep upgrade and you will have the latest release features.

Bicep CLI check the version and upgrade.

Install Visual Studio Code extension

To work more comfortably with Bicep we have to install an extension that enables intellisense and validations in Visual Studio Code. Also, this extension can visualize in Bicep files described resources. Find and install the extension with the name "Bicep".

Bicep DSL support extension for Visual Studio Code.

Now we are ready to describe Azure Container App using Bicep DSL.

Source: https://docs.microsoft.com/en-us/azure/azure-resource-manager/bicep/overview?tabs=bicep

Common Bicep syntax

Bicep syntax is very simple and straightforward. All elements are self describable 👍, I`ll mention a few of them, param (defines parameter), resource (defines the resource), and module (load defined resources from another Bicep file).

// Change target scope. 
targetScope = '<scope>'

// Define parameter, type and default value
param <parameter-name> <parameter-data-type> = <default-value>

// Define resource
resource <resource-symbolic-name> '<resource-type>@<api-version>' = {
  <resource-properties>
}

// Define module and external Bicep file path
module <module-symbolic-name> '<path-to-file>' = {
  name: '<linked-deployment-name>'
  params: {
    <parameter-names-and-values>
  }
}
Common Bicep elements syntax.

It`s important to mention that resources types and configuration properties can be found in Microsoft Documentation. Sometimes it could be not a very easy task to find the right type of resource you would like to provision 🔎. If we try to find a Container App example you find nothing, only after deeper documentation research, you will find a Bicep example for Container App. But as we saw previously Bicep is still in the development phase and improving every day.

Bicep & ARM templates documentation does not contain Container App template.

Let`s move forward and define Azure Container App using Bicep.

Describe Azure Container App using Bicep

Azure Container App consists of the following Azure resources: Resource Group, Log Analytics Workspace, Container App Environment, and Container App.

Azure Container App resources.

To start describing Azure resources in Visual Studio Code create a file main.bicep for resource definition.

By default, Bicep works in Resource Group scope, which means that you have to use a resource group that already exists. Our goal is to describe Resource Group provisioning using Bicep. Bicep support to switch to different target scopes (Resource Group, Subscription, Tenant, Management Group). We are interested to use Subscription as the target scope.

// Change target scope from default (Resource Group) to Subscription
targetScope = 'subscription'
Change Bicep target scope to Subscription.

Describe Resource Group using Bicep

Documentation reference: https://docs.microsoft.com/en-us/azure/templates/microsoft.resources/resourcegroups?tabs=bicep.

// Define Resource Group with name and location
resource containerAppResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' = {
  name:     'craftbakery-test'
  location: 'northeurope'
}
Describe Resource Group

Describe Log Analytics Workspace using Bicep

Documentation reference: https://docs.microsoft.com/en-us/azure/templates/microsoft.operationalinsights/2020-03-01-preview/workspaces?tabs=bicep

// Describe Log Analytics Workspace
resource workspace 'Microsoft.OperationalInsights/workspaces@2020-08-01' = {
  name: 'log-workspace'
  location: 'northeurope'
  properties: {
    sku: {
      name: 'PerGB2018'
    }
    retentionInDays: 30
    workspaceCapping: {
      dailyQuotaGb: 1
    }
  }
}

Describe Container App Environment using Bicep

Documentation reference: https://docs.microsoft.com/en-us/azure/templates/microsoft.web/kubeenvironments?tabs=bicep

In this resource description, you can see a reference to the previously created resource workspace. We get the name of Log Analytics Workspace ${workspace.name}.

// Container App Environment
resource kubeEnvironment 'Microsoft.Web/kubeEnvironments@2021-03-01' = {
  name: 'kube-environment'
  location: 'northeurope'
  properties: {
    environmentType: 'managed'
    internalLoadBalancerEnabled: false
    appLogsConfiguration: {
      destination: 'log-analytics'
      logAnalyticsConfiguration: {
        customerId: reference('Microsoft.OperationalInsights/workspaces/${workspace.name}', '2020-08-01').customerId
        sharedKey: listKeys('Microsoft.OperationalInsights/workspaces/${workspace.name}', '2020-08-01').primarySharedKey
      }
    }
  }
}

Describe Container App using Bicep

Documentation reference: https://docs.microsoft.com/en-us/azure/container-apps/microservices-dapr-azure-resource-manager?tabs=bash&pivots=container-apps-bicep#create-azure-bicep-templates

In this resource description, you can see a reference to the previously created resource kubeEnvironment. We get the identifier of environment resource ${kubeEnvironment.id}.

// Container App
resource containerapps 'Microsoft.Web/containerapps@2021-03-01' = {
  name: 'container-apps'
  kind: 'containerapps'
  location: 'northeurope'
  properties: {
    kubeEnvironmentId: kubeEnvironment.id
    configuration: {
      ingress: {
        external: true
        targetPort: 80
      }
    }
    template: {
      containers: [
        {
          name: 'sample-container-test'
          image: 'mcr.microsoft.com/azuredocs/containerapps-helloworld:latest'
          resources: {
            cpu: '0.25'
            memory: '.5Gi'
          }
        }
      ]
    }
  }
}

Deploy resources

Before we provision Azure Container App resources we have to verify if all is done in a correct way. To check Bicep you have to execute the following command by using the Windows command prompt or Windows Terminal.

az bicep build --file main.bicep
Build Bicep files.

If you receive no red errors then you all are done correctly and you are ready to provision resources in Azure. 🎉

$location = 'northeurope'
$subscription = '*****'
$deploymentName = 'ContainerApp'

# Sign-in to Azure
az login

# Set subscription
az account set --subscription $subscription

# Create subscription deployment
az deployment sub create `
  --name $deploymentName `
  --location $location `
  --template-file ./main.bicep `
  --parameters ./test.parameters.json
Deploy resources to Azure.

After executing the deployment command you can check if in Azure Portal appears new resources.

Azure Resource Group.
Container App resources

Looks great! 🤩

If you are interested in how we can improve the structure main.bicep please visit my GitHub repo 😎. And also if you are interested in  Bicep follow Bicep Community Call videos on YouTube (channel Azure Deployments & Governance) 📽️.

Happy experiments! ❤️

[eof]