{"id":60922,"date":"2024-03-27T11:43:21","date_gmt":"2024-03-27T06:13:21","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=60922"},"modified":"2024-03-28T11:48:36","modified_gmt":"2024-03-28T06:18:36","slug":"bicep-language-for-deploying-azure-resources","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/bicep-language-for-deploying-azure-resources\/","title":{"rendered":"Bicep Language For Deploying Azure Resources"},"content":{"rendered":"<h1><b>Introduction<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><\/h1>\n<p><span style=\"font-weight: 400;\">Bicep is a domain-specific language (DSL) that deploys Azure resources with declarative syntax. <\/span><span style=\"font-weight: 400;\">With a flexible syntax designed especially for Azure Resource Manager (ARM) templates, the Bicep language offers a useful way to manage Azure infrastructure installations.Bicep provides an attractive solution for environments where enterprises want to apply Infrastructure as Code (IaC) principles. It allows application code to be automated, version-controlled, and defined alongside infrastructure specifications. <\/span><span style=\"font-weight: 400;\">The language makes it easier to use agile development techniques like Continuous Integration\/Continuous Deployment (CI\/CD) thanks to its smooth interaction with Azure services and tools like Azure CLI and Azure DevOps. Additionally, Bicep acts as a link for companies moving away from ARM templates, facilitating a more seamless transition as they update their infrastructure provisioning procedures.<\/span><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<h1><b>Scenario\u00a0<\/b><\/h1>\n<p><span style=\"font-weight: 400;\">In this blog, we will see how we can utilise Bicep as Infrastructure as Code (IaC) principles to ensure consistency, repeatability, and version control. Whether provisioning virtual machines, storage accounts, or Azure App Services, Bicep empowers you to define your infrastructure requirements efficiently in a meaningful manner.<\/span><span style=\"font-weight: 400;\"><br \/>\n<\/span><\/p>\n<h1><b>Bicep Installation\u00a0<\/b><\/h1>\n<p><span style=\"font-weight: 400;\">There are two types available for Bicep installation :\u00a0<\/span><\/p>\n<ul>\n<li><b>Visual Studio Code and Bicep extension : <\/b>Language support and resource auto completion are available in Visual Studio Code when the Bicep plugin is installed. The extension facilitates Bicep file creation and validation.<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">To install the extension, search for <\/span><b><i>bicep<\/i><\/b><span style=\"font-weight: 400;\"> in the Extensions tab in VS Code and select <\/span><b>Install<\/b><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-60923 size-full\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/Screenshot-from-2024-03-22-15-47-54.png\" alt=\"\" width=\"553\" height=\"131\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/Screenshot-from-2024-03-22-15-47-54.png 553w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/Screenshot-from-2024-03-22-15-47-54-300x71.png 300w\" sizes=\"(max-width: 553px) 100vw, 553px\" \/><\/p>\n<ul>\n<li><b>Azure CLI\u00a0 : <\/b>You have all you need to <b>deploy<\/b> and <b>decompile<\/b> Bicep files when you utilise the Azure CLI with Bicep. When a command that requires the Bicep CLI is run using Azure CLI, it installs it automatically.<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">\u00a0Azure CLI version 2.20.0 or later must be installed.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Install Azure CLI with one command :\u00a0<\/span><\/p>\n<pre>curl -sL https:\/\/aka.ms\/InstallAzureCLIDeb | sudo bash<\/pre>\n<p><span style=\"font-weight: 400;\">\u00a0Install Azure Bicep :\u00a0<\/span><\/p>\n<pre>az bicep install<\/pre>\n<p><span style=\"font-weight: 400;\">To validate your Bicep CLI and Azure CLI installation :\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span><\/p>\n<pre>az --version \r\naz bicep version<\/pre>\n<h2><b>Parameters and Variables in Bicep<\/b><\/h2>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Resource Manager settles the parameter values. Resource Manager substitutes the resolved value for the parameter whenever it is used.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">One of the <\/span><b>data types<\/b><span style=\"font-weight: 400;\"> must be selected for each parameter.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">In a Bicep file, you are restricted to 256 parameters.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">A parameter cannot share a name with another parameter in the same scope, nor with a variable, resource, or output.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Decorators are used by parameters to store metadata or constraints.\u00a0\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The decorators are positioned above the parameter&#8217;s declaration and have the <\/span><b>@expression<\/b><span style=\"font-weight: 400;\"> format.\u00a0<\/span><\/li>\n<\/ul>\n<h4><b>Declaration :\u00a0<\/b><\/h4>\n<pre>param &lt;parameter-name&gt; &lt;parameter-data-type&gt; = &lt;default-value&gt;<\/pre>\n<pre>@secure() \r\n\r\nparam password string\u00a0 \r\n\r\n@description('description here') \r\n\r\nparam size string<\/pre>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">To make developing your Bicep file easier, you can use variables.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">There cannot be a variable named the same as a resource, module, or parameter.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">the variable doesn&#8217;t have a <\/span><b>data type<\/b><span style=\"font-weight: 400;\"> specified.<\/span><\/li>\n<\/ul>\n<p><b>Declaration<\/b><\/p>\n<pre>var &lt;variable-name&gt; = &lt;default-value&gt;<\/pre>\n<h2><b>Resource Declaration in Bicep<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Azure resources within your infrastructure are defined and provisioned using resource declarations in Bicep. Use the <\/span><b>resource<\/b><span style=\"font-weight: 400;\"> keyword to add a resource declaration. The resource has a symbolic name that you have set. Names with symbols have case-sensitivity. They might have underscores (_), digits, and letters in them. Numbers are not where they can begin. A parameter, variable, or module cannot have the same name as a resource.<\/span><\/p>\n<p><b>Declaration<\/b><span style=\"font-weight: 400;\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Use the <\/span><b>for syntax<\/b><span style=\"font-weight: 400;\"> to deploy multiple instances of a resource :\u00a0\u00a0<\/span><\/p>\n<pre>resource &lt;symbolic-name&gt; '&lt;full-type-name&gt;@&lt;api-version&gt;' = [for &lt;item&gt; in &lt;collection&gt;: { \u00a0\u00a0\r\n&lt;properties-to-repeat&gt; \r\n}]<\/pre>\n<h3><b>Example of storage account in Bicep ( main.bicep file )\u00a0<\/b><\/h3>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>param name string = 'examplestorage'\r\n\r\nparam location string =\u00a0 'westus3'\r\n\r\nresource stg 'Microsoft.Storage\/storageAccounts@2019-06-01' = {\r\n\r\n\u00a0\u00a0name: name\u00a0\r\n\r\n\u00a0\u00a0location: location\u00a0\r\n\r\n\u00a0\u00a0sku: {\r\n\r\n\u00a0\u00a0\u00a0\u00a0name: 'Standard_LRS'\r\n\r\n\u00a0\u00a0\u00a0\u00a0tier: 'Standard'\r\n\r\n\u00a0\u00a0}\r\n\r\n\u00a0\u00a0kind: 'StorageV2'\r\n\r\n\u00a0\u00a0properties: {\r\n\r\n\u00a0\u00a0\u00a0\u00a0accessTier: 'Hot'\r\n\r\n\u00a0\u00a0}\r\n\r\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><b>To run the above code use the below az cli command\u00a0<\/b><\/p>\n<pre>az deployment group create -f &lt;bicep-file-name&gt;\u00a0 -g &lt;resource-goup-name&gt;<\/pre>\n<p><b>Use existing resources in Bicep<\/b><\/p>\n<p><span style=\"font-weight: 400;\">When deploying a resource that requires a value to be obtained from an existing resource, use the <\/span><b>existing<\/b><span style=\"font-weight: 400;\"> keyword.The same scope attribute is utilized if your resources are already specified in the same resource group as the current deployment and you wish to access or extract data from those resources.Use the various scope property if you wish to access or extract resources that have already been produced in different resource groups for your current deployment.<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>resource stg 'Microsoft.Storage\/storageAccounts@2022-09-01' existing = {\r\n\r\n\u00a0\u00a0name: 'examplestorage'\r\n\r\n\u00a0\u00a0scope: resourceGroup(\u2018exampleRG\u2019)\r\n\r\n}\r\n\r\noutput blobEndpoint string = stg.properties.primaryEndpoints.blob<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><b>Modules In Bicep<\/b><\/p>\n<p><span style=\"font-weight: 400;\">Azure Bicep modules encapsulate resources and configurations into modular components, allowing you to organize and reuse your Bicep code.This encourages the ability to share infrastructure patterns across other projects or environments, as well as the reuse and maintainability of code<\/span><span style=\"font-weight: 400;\">.\u00a0<\/span><\/p>\n<h3><b>Definition Syntax<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">The basic syntax for defining a module is:<\/span><\/p>\n<table style=\"height: 263px;\" width=\"391\">\n<tbody>\n<tr>\n<td>\n<pre>module &lt;symbolic-name&gt; '&lt;path-to-file&gt;' = {\r\n\r\nname: '&lt;linked-deployment-name&gt;'\r\n\r\nparams: {\r\n\r\n&lt;parameter-names-and-values&gt;\r\n\r\n}\r\n\r\n}<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">Let\u2019s talk about Advantages and Disadvantages of Bicep :\u00a0<\/span><\/p>\n<h3><b>Bicep Advantages<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\"><b>Readability:<\/b><span style=\"font-weight: 400;\"> Bicep code is written in a declarative language that is easy to read and understand. This makes it easier to manage and maintain your infrastructure.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Maintainability:<\/b><span style=\"font-weight: 400;\"> Bicep code is organised into modules, which makes it easy to reuse code and keep your infrastructure organised.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Infrastructure as code (IaC):<\/b><span style=\"font-weight: 400;\"> Bicep allows you to manage your infrastructure as code, which gives you more control and flexibility.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Cross-platform:<\/b><span style=\"font-weight: 400;\"> Bicep is a cross-platform tool that can be used on Windows, Mac, and Linux.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Open source:<\/b><span style=\"font-weight: 400;\"> Bicep is an open source tool that is supported by Microsoft.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Orchestration:<\/b><span style=\"font-weight: 400;\"> Resource Manager orchestrates the deployment of interdependent resources so they&#8217;re created in the correct order. When possible, Resource Manager deploys resources in parallel so your deployments finish faster than serial deployments.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Integration with Azure services:<\/b><span style=\"font-weight: 400;\"> Bicep is integrated with Azure services such as Azure Policy, template specs, and Blueprints.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Preview changes:<\/b><span style=\"font-weight: 400;\"> You can use the <\/span><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/azure-resource-manager\/bicep\/deploy-what-if\"><b>what-if operation<\/b><\/a><span style=\"font-weight: 400;\"> to get a preview of changes before deploying the Bicep file. With what-if, you see which resources will be created, updated, or deleted, and any resource properties that will be changed. The what-if operation checks the current state of your environment and eliminates the need to manage state.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>No state or state files to manage:<\/b><span style=\"font-weight: 400;\"> All state is stored in Azure. Users can collaborate and have confidence their updates are handled as expected.<\/span><\/li>\n<\/ul>\n<h3><b>Bicep Disadvantages<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The language is domain specific, meaning that only Microsoft Azure is supported. You are not using BICEP if you are using AWS.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Obtaining relatively new tools and examples could be challenging.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Dependency on the Azure Resource Manager (ARM).\u00a0\u00a0<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">We can compare Bicep with the terraform.<\/span><\/p>\n<h3><b>Bicep vs Terraform<\/b><\/h3>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">While Terraform requires you to maintain state files, Bicep does not contain any state files. Errors arise from any corruption or deletion of the state file<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">.Bicep is compatible with all Azure resources and is built using ARM templates. Terraform, being developed by a third party, requires time for implementation for every Azure or AWS resource.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Bicep offers a more efficient deployment experience for Azure-centric setups because it is designed exclusively for the purpose of deploying Azure resources. However, Terraform provides support for several clouds and a larger ecosystem.<\/span><\/li>\n<\/ul>\n<h2><b>Conclusion<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">This blog allows us to comprehend the fundamental logic and syntax of the Bicep language, which is used to deploy Azure resources. Just outline the main benefits and fundamental syntax of the Bicep language in this blog.\u00a0<\/span><\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Introduction\u00a0 Bicep is a domain-specific language (DSL) that deploys Azure resources with declarative syntax. With a flexible syntax designed especially for Azure Resource Manager (ARM) templates, the Bicep language offers a useful way to manage Azure infrastructure installations.Bicep provides an attractive solution for environments where enterprises want to apply Infrastructure as Code (IaC) principles. It [&hellip;]<\/p>\n","protected":false},"author":1747,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":62},"categories":[4308,2348],"tags":[5725,1892],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60922"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1747"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=60922"}],"version-history":[{"count":8,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60922\/revisions"}],"predecessor-version":[{"id":61027,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60922\/revisions\/61027"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=60922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=60922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=60922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}