Create your first Bicep file
What is Bicep?
Bicep is a domain-specific language (DSL) that uses a declarative syntax to deploy Azure Resources. In a Bicep file, you define the infrastructure you want to deploy to Azure, and then use that file throughout the development lifecycle to repeatedly deploy your infrastructure. Your resources are deployed in a consistent manner. Bicep provides concise syntax, reliable type safety, and support for code reuse. Bicep offers a first-class authoring experience for your Infrastructure as Code solutions in Azure.
Prerequisites
In order to be able to follow along or try it out for yourself, you will need the following tools:
Once you have downloaded VS Code, installed the Azure CLI, and installed the Bicep Extension, you are ready to proceed to create your first Bicep File.
Breakdown of the syntax
resource - This tells Azure that we are trying to create or access an existing resource such as SQL Servers or Storage Accounts and much more.
param - This tells Azure that this is a parameter that can be read from a file, set in the command line, or set within the bicep file. A param has a name and a type which have to be set in order to be used. The options for the type are bool, string, int, object, and array.
var - This tells Azure that you are creating a variable for use within the running of the file and it can be anything from string to bool to object and so on. You can use it to store your local variables required for execution.
module - This tells Azure that you are using a different bicep file so it will load in the file and execute the code that is contained within. This is useful for splitting your Bicep file into logical files. For example, you can create a Bicep file to create a new Function App that connects to existing AppInsights and Storage Accounts, and by calling the module, you can provide the parameters and the module will do the rest. Modules are reusable and should be designed with that in mind.
Create a basic Bicep file
Create a folder in your location of choice, the name of the folder does not matter but I will be naming mine: Bicep Basics
Open Visual Studio Code, navigate to "File" in the top right, and click on "Open Folder..."
Navigate to your newly created folder and click "Select Folder". Once you have done this, hover over your folder name and click the "New File" button that appears.
Name the file main.bicep and save the file to load in the Bicep Extension installed earlier. Once you have saved the file, you can start typing code to create new resources, reference existing resources, and more.
The first thing I would like to show you that will make it a lot easier when creating new resources is to make use of the tools offered to you by Bicep. Bicep offers code completion and IntelliSense which makes the development process a lot easier and faster. Type the following into your bicep file:
领英推荐
resource storageAccount 'Microsoft.Storage/storageAccounts@2021-09-01' =
A breakdown of the above:
Add a space after the "=" and you will see a popup as below:
If you press the tab key, you will see that the required properties are generated but are empty. This is extremely useful as it prevents you from guessing what is required to create a new resource.
Fill out the values that are required using this code snippet as a reference:
resource storageAccount 'Microsoft.Storage/storageAccounts@2021-09-01' = {
? name: 'myuniquestorageaccount'
? location: 'westeurope'
? sku: {
? ? name: 'Standard_LRS'
? }
? kind: 'StorageV2'
}
Your first Bicep file has now been created and can be deployed which is covered below. The Bicep file will create a new Storage Account within the resource group that will be specified below.
How to deploy a Bicep file
In order to deploy a Bicep file, you will need to open a new Terminal in Visual Studio Code shown below:
You will want to ensure that you are using a Powershell terminal as shown below:
If it's not Powershell, you can click the plus icon and select Powershell and it will create a new terminal for you. Now that you have the terminal open, you need to use the following commands in this order:
You have now deployed your first Bicep file! Keep an eye out for new tutorials to expand your Bicep knowledge!