Getting Started With Azure Bicep
Infrastructure as Code (IaC): This is just a practice where we do all the Infrastructure management through code.(Code-based tools or Software development).
It is an ability to deal the infrastructure by calling an API call or via code, which gives us environment to manage your all infrastrucre via code.
Some of the benefits are It’s easy to use, Reusable, Support version control.
What is Azure Bicep?
Azure Bicep is Microsoft’s new declarative language for creating and managing Azure resources.
It is a new domain specific language or DSL used to create and deploy resources in Azure. In simple word it’s a language which enables you for easier deployment of Azure resources.(infrastructure-as-code language for Azure).
What is ARM templates?
Azure Resource Manager(ARM) Templates are IaC solution where we can use them to deploy, update, delete and manage resources in Azure.
In simple wards, ARM templates are a declarative description, where we don’t have to think about what the action is, only about defining what we want.
Resources will automatically deployed in the correct order based upon the dependencies.
ARM templates are mostly based upon JSON language.
FAQ for Azure Bicep:
What are the benefits of Azure Bicep?
1. Simple syntax
2. Modularity
3. Easy Integration with Azure services
4. Open source
5. No state management
6. Faster resource support
Is it alternative to ARM template?
Yes, As ARM templates are widely used but at the mean time, they’re very hard to learn, especially for beginners.
Is it hard learn?
No Not really, If the person getting started and doesn’t have a programming background. As we know ARM template are mostly json templates and
need more effort for formatting issues.
Can we convert a bicep file into ARM template or vice-versa?
Yes
Is it production ready?
Yes, From version 0.3 onwards It is supported by Microsoft support plans.
Environment Setup for Azure Bicep:
You can setup bicep templates using any of your favourite tool or IDE of your choice.
Some of the tools are given below.
1. Bicep Extensions for VS Code
2. Bicep CLI
3. Azure CLI
4. Azure Powershell >= 5.6.0
- Bicep Extensions for VS Code:
In VS Code, select the Extension icon on the left hand side, Search “bicep” in the marketplace and Click Install.
It Support .bicep templates and built-in support for IntelliSense code completion like syntax bracket-matching, highlighting, auto-indentation, snippets, and many more.
2. Bicep CLI:
This is mainly used for compile or decompile bicep to ARM template or vice-versa.
Manually download from: https://docs.microsoft.com/en-us/azure/azure-resource-manager/bicep/install?tabs=azure-powershell&WT.mc_id=AZ-MVP-5004159#install-manually
3. Azure CLI:
Manually Install from: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?WT.mc_id=AZ-MVP-5004159
4. Azure Powershell:
You need version 5.6.0 or later installed in your system.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — -
Manually install/upgrade the Bicep CLI in Azure CLI:
Install : az bicep install
Update: az bicep upgrade
Chocolatey:
choco install bicep -y
Install with PowerShell.exe
https://docs.chocolatey.org/en-us/choco/setup#install-with-cmd.exe
Azure Bicep Parameters:
Parameters make templates much more dynamic, as you can use the same template multiple times and customize it depending upon the requirements.
parameters define using a keywords called : param .
Parameters can have default values or assign some values using equal(=) sign.
The syntex for declaring a parameters are keyword param, followed by the parameter name and the type.
Parameter types can be string, boolean, integer, array, object etc..
Examples: param environment string
You can follow these arcticles for more information.
https://docs.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-functions
Azure Bicep Variables:
As we know in general terms variables are used to store information. Unlike constant, variables are changeable,
we can change value of a variable during execution of a program.
The same way bicep variables are used to store informations which are used multiple times in a template.
Basically to declare a variable,we use the var keyword followed by the variable name.
Example:
var primaryRGName = ‘${d.PrimaryRegionVal}-${resourceGroupName}’
Creating Resources:
Starting with the keyword resource followed by symbolic name and resource type + version.
These are the primary things needed while creating bicep.
Basic structure for an a Resource:
resource <symbolic name> <resource type>@<api version> = {
name: '<actual azure resource name>'
sku: {
name:'<sku name>'
}
kind:'<storage kind>l'
location: <Location or region>
}
Deployment Azure Bicep using Powershell:
- Verify you logged into the proper subscription
- Create a new Resource group using New-AzResource group command.
- Deploy the storage account using New-AzResourceGroupDeployment command
New-AzResourceGroupDeployment -Name InitialTest -ResourceGroupName BicepPocResourceGroup -TemplateFile .\fkwebapp.bicep -environment "prod" -location "CentralUS"
Deployment using Command Line Interface of Azure CLI:
Create the new storage account If not exist.
az deployment group create --resource-group BicepTestPocResourceGroup --template-file .\fkwebapp.bicep --parameters environment=prod location=CentralUS
Note:
Deploying the “raw” .bicep file directly:
az deployment sub create — template-file .\fkwebapp.bicep — parameters env=dev — location ‘Central US’ -c
New-AzDeployment -TemplateFile “.\fkwebapp.bicep” -DeploymentName “vm-example” -Location “CentralUS”
Convert ARM templates to Bicep:
bicep decompile command using to convert an existing ARM templates to bicep.
bicep decompile fkwebapp.json
Convert bicep to ARM templates:
bicep build command using to convert an bicep file to ARM template.
bicep build fkwebapp.bicep
Conclusion:
In this post I showed how to use bicep based templates and how to automatically build/deploy a bicep solution using Workflows. For more Information follow Project Bicep.