Create your first Bicep file
Microsoft - Fundamentals of Bicep

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..."

No alt text provided for this image

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.

No alt text provided for this image

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:

  • resource - stipulates that we are creating or referencing an existing resource such as a SQL Server or Storage Account.
  • storageAccount - this is just a variable name, can be named whatever you would like it to be.
  • 'Microsoft.Storage/storageAccounts@2021-09-01' - This is telling Bicep what resource you are using where after the @ is the API Version.

Add a space after the "=" and you will see a popup as below:

No alt text provided for this image

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.

No alt text provided for this image

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:

No alt text provided for this image

You will want to ensure that you are using a Powershell terminal as shown below:

No alt text provided for this image

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:

  • az login - This will open a page for you to log in using your Microsoft account. Once you have logged in, you can close the tab and return to your terminal.
  • az account list - This will list any available subscriptions that you have on your account that you can use.
  • az account set --subscription {id} - This will set your current subscription, copy the id of an account listed from the previous command and replace {id} with the id.
  • az account show - This will show the account that you have connected to in the terminal.
  • az configure --defaults group={Resource Group Name} - This will set the default Resource Group for all scripts in this session without having to set it at every execution from the terminal. Replace {Resource Group Name} with your Resource Group name.
  • az deployment group create --name Deployment01 --template-file main.bicep - This will deploy the Bicep file that you have created with the deployment name Deployment01. If you do not provide --name Deployment01 then it will default to main within the Azure Portal, you can of course provide any name you wish for the name parameter. Once you have hit enter, then you will see a Running... message appear. Once the deployment has been completed, you will get the information on the created/modified resources in your terminal.

You have now deployed your first Bicep file! Keep an eye out for new tutorials to expand your Bicep knowledge!

要查看或添加评论,请登录

Declan Taggart的更多文章

  • Bicep - Parameter Decorators

    Bicep - Parameter Decorators

    In my previous article, I covered Parameters and Variables and how to use them. This article is a continuation of my…

  • Bicep - Parameters and Variables

    Bicep - Parameters and Variables

    In my previous article, I covered the creation of a basic Bicep file with the commands that are required and a…

  • Basic unit testing with xUnit

    Basic unit testing with xUnit

    Unit testing has many benefits and will help you identify bugs and problem areas in your code. If we look at the…

  • Dependency injection in Azure Function v3

    Dependency injection in Azure Function v3

    In this article, I am going to show how DI (Dependency Injection) can easily be achieved within an Azure Function…

    2 条评论
  • My basic wireframe for Azure Function v3

    My basic wireframe for Azure Function v3

    In this article, I will show you the wireframe I use for every Azure Function that I create and use. This demo will be…

    2 条评论

社区洞察

其他会员也浏览了