Logo

dev-resources.site

for different kinds of informations.

Getting Started with Azure Bicep

Published at
7/14/2024
Categories
azure
bicep
Author
raulnq
Categories
2 categories in total
azure
open
bicep
open
Author
6 person written this
raulnq
open
Getting Started with Azure Bicep

Azure Bicep is a domain-specific language (DSL) designed for deploying and managing Azure resources. It offers several advantages, including a more concise syntax compared to ARM templates, improved readability, enhanced maintainability, and a gentler learning curve.

How Does it Work?

A Bicep file is automatically converted into an ARM template (JSON) when a deployment is submitted to Azure.

How Does it Work?

Pre-requisites

Resources

The syntax for defining a resource is:

resource <symbolic-name> '<full-type-name>@<api-version>' = {
  <resource-properties>
}
Enter fullscreen mode Exit fullscreen mode

To define a resource, use the resource keyword followed by:

  • <symbolic-name>: The symbolic name isn't the same as the resource name, and it is used to reference the resource in other sections of your Bicep file.

  • <full-type-name> and <api-version>: For the available resource types and versions, see Bicep resource reference.

  • <resource-properties>: name and location are properties that can be found in nearly every resource. After setting those values, we need to set the properties that are specific to the resource type we are using.

Create a main.bicep file with the following content:

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
  name: 'myuniquestorage001'
  location: resourceGroup().location
  kind: 'StorageV2'
  sku: {
    name: 'Standard_GRS'
  }
}
Enter fullscreen mode Exit fullscreen mode

Execute the command az group create -l eastus -n MyResourceGroup to create the resource group where our resources will be stored. Afterward, run the following command:

az deployment group create --resource-group MyResourceGroup --template-file .\main.bicep
Enter fullscreen mode Exit fullscreen mode

Parameters

The syntax for defining a parameter is:

@<decorator-name>
param <parameter-name> <parameter-data-type> = <default-value>
Enter fullscreen mode Exit fullscreen mode

To define a parameter, use the param keyword followed by:

  • <parameter-name>: The name of the parameter. It cannot have the same name as a variable, resource, output, or another parameter in the same scope.

  • <parameter-type>: We can find the complete list of data types here.

  • <default-value>: The default value is used when a value is not supplied during deployment.

  • <decorator-name>: Parameters use decorators for constraints or metadata. The list of decorators can be found here.

Update the main.bicep file with the following content:

@description('storage account name')
param storageAccountName string = 'myuniquestorage001'

@description('storage account location')
param location string = resourceGroup().location

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
  name: storageAccountName
  location: location
  kind: 'StorageV2'
  sku: {
    name: 'Standard_GRS'
  }
}
Enter fullscreen mode Exit fullscreen mode

To pass inline parameters, you can execute a command like this:

az deployment group create --resource-group MyResourceGroup --template-file .\main.bicep --parameters storageAccountName='myuniquestorage002'
Enter fullscreen mode Exit fullscreen mode

Variables

The syntax for defining a variable is:

var <variable-name> = <variable-value>
Enter fullscreen mode Exit fullscreen mode

To define a variable, use the var keyword followed by:

  • <variable-name>: A variable cannot share the same name as a parameter, module, or resource.

  • <variable-value>: Note that we do not need to specify a data type for the variable. The type is inferred from the value provided.

Update the main.bicep file using the following content:

@description('storage account prefix')
param storageAccountPrefix string

@description('storage account location')
param location string = resourceGroup().location

var storageAccountName = '${toLower(storageAccountPrefix)}${uniqueString(resourceGroup().id)}'

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
  name: storageAccountName
  location: location
  kind: 'StorageV2'
  sku: {
    name: 'Standard_GRS'
  }
}
Enter fullscreen mode Exit fullscreen mode

Execute the following command:

az deployment group create --resource-group MyResourceGroup --template-file .\main.bicep --parameters storageAccountPrefix='mystorage'
Enter fullscreen mode Exit fullscreen mode

Outputs

The syntax for defining an output is:

output <output-name> <output-data-type> = <output-value>
Enter fullscreen mode Exit fullscreen mode

To define a variable, use the output keyword followed by:

  • <output-name>: An output can have the same name as a parameter, variable, module, or resource.

  • <output-data-type>: Can use the same data types of the parameters.

  • <output-value>: The value we want to expose.

Update the main.bicep file with the content provided below:

@description('storage account prefix')
param storageAccountPrefix string

@description('storage account location')
param location string = resourceGroup().location

var storageAccountName = '${toLower(storageAccountPrefix)}${uniqueString(resourceGroup().id)}'

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
  name: storageAccountName
  location: location
  kind: 'StorageV2'
  sku: {
    name: 'Standard_GRS'
  }
}

output endpoints object = storageAccount.properties.primaryEndpoints
Enter fullscreen mode Exit fullscreen mode

Execute the following command to deploy the resource and filter the output (How to query Azure CLI command output using a JMESPath query):

az deployment group create --resource-group MyResourceGroup --template-file .\main.bicep --parameters storageAccountPrefix='mystorage' --query "properties.outputs"
Enter fullscreen mode Exit fullscreen mode

In conclusion, by understanding the core concepts of resources, parameters, variables, and outputs, you can effectively leverage Bicep to streamline your Azure deployments and make your infrastructure as code more accessible and maintainable. We encourage you to check the official documentation here to explore the full potential of this tool. Thanks, and happy coding.

bicep Article's
30 articles in total
Favicon
Deploying and Configuring a Hybrid Identity Lab Using Bicep - Part 1: Active Directory Setup and Sync
Favicon
How to setup an Azure Machine Learning Workspace securelyπŸ›‘οΈπŸ”’πŸ”‘
Favicon
Creating a Custom Role for Secure Bicep Deployments in Azure
Favicon
Create a GitHub pipeline to test, review, and deploy a Bicep template.
Favicon
A KeyVault for the Power Platform
Favicon
Kickstart projects with azd Templates
Favicon
Conditional deployment in Azure Bicep
Favicon
Rush configuration
Favicon
user-defined type in Azure Bicep, an introduction
Favicon
Set version numbers in Bicep templates
Favicon
Securing your Azure deployments with PSRule
Favicon
Versioned Bicep templates- Deployment
Favicon
Change Management in Infrastructure as a Code (IaC)
Favicon
Azure Verified Modules: Consolidated Standards for a Good IaC
Favicon
Getting Started with Azure Bicep
Favicon
Using Azure Bicep to deploy MS Graph resources
Favicon
Deploying static webs apps with the Azure cli and bicep
Favicon
Azure API Management: Harnessing Bicep for Effortless User and Subscription Creation
Favicon
User-defined function in Azure Bicep
Favicon
Expose your Open API specs with Azure API management
Favicon
Deploy multiple APIs in Azure API management, hosted in the same App service.
Favicon
Add Azure Developer CLI deployment ID and UTC timestamp to Bicep files
Favicon
🦾 Top 5 Azure Bicep tips & tricks to get started πŸš€
Favicon
Exploring the awesome Bicep Test Framework πŸ§ͺ
Favicon
The issue of recursive module calls in declarative infrastructure-as-code
Favicon
Azure Bicep - Finally functions to manipulate CIDRs
Favicon
Multi Scopes Deployment with Azure Bicep
Favicon
Azure Deployment Stacks, deploy and manage a landing zone with Bicep
Favicon
Azure Open AI: handling capacity and quota limits with Bicep
Favicon
Learn bicep based on the GUI of Azure Portal

Featured ones: