adaptive.run TECH BLOG

Cloud can be tricky sometimes. Find out what scenarios we've ran into that are worth being mentioned and explained.

Get started with Azure Bicep templates by creating your first one

Level: 200
Publishing date: 27-Dec-2022
Author: Catalin Popa

Microsoft introduced Azure Bicep, a Domain-Specific Language (DSL) in 2020 as an Infrastructure as Code (IaC) language to facilitate resource deployment on Azure. The language is designed to be modular, allowing reusability of work across different templates or deployments.

Since its launch, Bicep has gained popularity within the IT community, with numerous blog posts, tweets, and conference sessions on the official Bicep GitHub space. Bicep became production ready at v0.3 and is now supported by Microsoft Support Plans.

This article aims to guide you through creating your first Bicep template.

Tools you need to get started:
• Visual Studio Code
• Bicep extension for Visual Studio Code
• Azure PowerShell.

How to create the Bicep template

This post provides a step-by-step guide on how to create your first Bicep template. Prior experience with ARM templates or similar tools is assumed. The tutorial will demonstrate how to create an Azure App Service Plan and Azure Linux Web App.

To take advantage of formatting and syntax features, I recommend using Visual Studio Code with the Bicep extension to develop your templates.

To begin, you'll need to declare the parameters and variables that your template will use. 

Here is an example of the basic syntax:

param webAppName string
param appServicePlanName string

var appServicePlanSku = 'B1'
var location = 'westus2' 

After declaring the necessary parameters and variables, the next step is to define how you want to deploy the Azure App Service Plan within the Bicep template.
Here is an example of the code you can use to create an App Service Plan:

resource appServicePlan 'Microsoft.Web/serverfarms@2021-01-15' = {
name: appServicePlanName
location: location
properties: {
name: appServicePlanName
workerSizeId: appServicePlanSku
}

• In this code snippet, you use the resource keyword to define a new Azure resource. The appServicePlan is the name of the resource you are creating, and the Microsoft.Web/serverfarms@2021-01-15 string represents the resource type.
• Within the properties section of the code, you can specify various options for the resource, such as the name and workerSizeId. In this case, appServicePlanSku is used to define the size of the worker instances.
• By defining the App Service Plan as a resource in your Bicep template, you can ensure that it will be created in the same way every time the template is deployed.

Once you have defined the App Service Plan in your Bicep template, you can proceed with defining the Azure Web App.

Here is an example of how to do this:

resource webApp 'Microsoft.Web/sites@2021-01-15' = {
name: '${webAppName}-web-app'
location: location
properties: {
name: '${webAppName}-web-app'
serverFarmId: appServicePlan.id
siteConfig: {
linuxFxVersion: 'DOCKER|nginx'
}
}

• In this code, you use the resource keyword again to define a new Azure resource. The webApp is the name of the resource you are creating, and the Microsoft.Web/sites@2021-01-15 string represents the resource type.
• Within the properties section of the code, you can specify various options for the resource, such as the name, serverFarmId, and siteConfig. In this case, the serverFarmId property is set to the ID of the App Service Plan you created earlier.
• The siteConfig section is used to configure the settings for the web app. In this example, the linuxFxVersion property is set to DOCKER|nginx, which tells Azure to use a Docker container running the Nginx web server for this app.
• With these two resources defined in your Bicep template, you can deploy both the App Service Plan and the Web App in a single deployment, ensuring that they are set up correctly and consistently every time.

Azure Bicep template deployment

Once you have created your Bicep template for deploying Azure resources, the next step is to deploy it. There are several ways to deploy Azure Bicep templates, such as through Azure CLI, Azure PowerShell, Azure DevOps, and GitHub Actions.

If you prefer to use Azure PowerShell, you can follow these steps to deploy your Bicep template:

1. First, you need to make sure that the resource group where you want to deploy your resources is created. You can create a new resource group using the following PowerShell command:

New-AzResourceGroup - Name<resourse-group-name> -Location <location>

2. Next, you need to use the following PowerShell command to deploy the Bicep template: 

New-AzResourceGroupDeployment -Name <deployment-name> -ResourceGroupName <resource-group-name> -TemplateFile <path-to-bicep-template

In this command, you need to provide the name for your deployment, the name of the resource group where you want to deploy the resources, and the path to your Bicep template.

For example, if your Bicep template is located in a file named azuredeploy.bicep in the current directory, and you want to deploy it to a resource group named my-resource-group in the eastus region, you can use the following command:

New-AzResourceGroupDeployment -Name my-deployment -ResourceGroupName my-resource-group -TemplateFile azuredeploy.bicep

This command will deploy the resources defined in your Bicep template to the specified resource group.

By following these steps, you can easily deploy your Bicep template and create the necessary Azure resources in a consistent and repeatable manner.

Note: It's important to say that there are other methods available for deploying Bicep templates, such as Azure CLI, Azure DevOps, and GitHub Actions.

Conclusion

Azure Bicep it offers a more intuitive syntax and modular structure, making it easier to manage and reuse templates across different deployments. By using Bicep, engineers can more easily automate their infrastructure deployment and management, and reduce the risk of configuration errors.
Overall, Bicep is an important tool for anyone working with Azure infrastructure, and it's worth investing time to learn how to use it effectively.

adaptive.run

Transform your business.
Run adaptive.

Contact

Phone: +40 73 523 0005
Email: hello@adaptive.run

© Copyright  2019-2024 adaptive.run- All Rights Reserved