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.
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:
- Create Azure free account or use existing - https://azure.microsoft.com/en-us/free/
- Install Azure CLI - https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli
- Visual Studio Code - https://code.visualstudio.com/
Install Bicep
To install Bicep you have to execute the following command by using the Windows command prompt or Windows Terminal.
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
.
To install the latest version of Bicep CLI just execute az bicep upgrade
and you will have the latest release features.
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".
Now we are ready to describe Azure Container App using Bicep DSL.
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).
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.
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.
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.
Describe Resource Group using Bicep
Documentation reference: https://docs.microsoft.com/en-us/azure/templates/microsoft.resources/resourcegroups?tabs=bicep.
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.
If you receive no red errors then you all are done correctly and you are ready to provision resources in Azure. 🎉
After executing the deployment command you can check if in Azure Portal appears new 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]