Manage your Azure resource deployments smartly using Biceps: step by step

 

In a world where we live by action, automation and speed, Azure Biceps are an awesome way to expeidate your deployments. Biceps are new normal and monograms of infrastructure-As-Code, where you can manipulate your resource creation by feeding in parameters, inputs and varying conditions through Azure CLIs.

Confused?

Let me bring up the definition of Azure Biceps, from Microsoft's definition: Bicep is a domain-specific language (DSL) that uses 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

You cane create any resource, resources -- any number of them, simply mainitaining their defintions on simple module based files and invoking their deployments through Azure CLI based commmands. As compared ARM templates: Bicep syntax reduces that complexity and improves the development experience

Let me give you few examples by which you can use your deployments, wisely using Bicep.

But first, I would like to let you know that throughout, I would be using Visual Studio codes. To do this, go ahead and install Bicep extension on your Visual Studio Code:


Also make sure you install Azure CLI:


ENsure that you have installed/enabled the Azure CLI by visiting File >> View >> Teminal (this will open the Powershell terminal) and then you install CLI by running the following command:

Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .\AzureCLI.msi 

Don't forget to restart the VS once you have done this. 

Next you need to install/enable Azure Bicep by the following command:

az bicep install

This will install Azure Bicep in a single user mode.

All right, next let us connect you Azure. For this, evidently you oughta have a subscription, you have enough of necessary permissions to create/manangae/deploy azure components. I prefer to do the connection by typing the command:

az login --use-device-code

This will ask you to open the microsoft login site, by inputting the one time password as given in the screenshot:


Control + click on the hhyperlink and paste the OTP given. It will redicet you a prompt where you can choose from user ID and password to connect to your available/resired extension.

Ok, we are all set now. Now time for real action. Buckle your seat-belt Dorothy, 'cause Kansas is saying bye-bye

Let me create an azure resource group for you. Start by clicking on New File >> select Bicep from the prompt and give a name like 'resourcegroup.bicep':


Cope and past the following code in your file.

targetScope = 'subscription'
param resourceGroupName string

resource subsRG 'Microsoft.Resources/resourceGroups@2024-03-01' = {
  name: resourceGroupName
  location: 'eastus'
}

Whew!!! It's a very simple file that sayes, it will create a resource group, as per any input parameter you will key in, right in the location: East US. 

N.B. note the @2024-03-01. It's actually an API that gets updated with time. For me, right now this is what the latest that I am getting. You must update it to the latest one available:


 Also note the subscrition as target scope. This will abide your deployment right under the subscription, when you selected the Az-login, as decribed above.

Done? Now let us deploy this. Come down to your Terminal command prompt and type:

az deployment sub create --name demoSubDeployment --location eastus --template-file ".\<Give the path of your file>" --parameters resourceGroupName=demoResourceGroupSubs

Ok, let me  try to explain here, what's happening. The resourceGroupName parameter is the name with which you will create your resource. Template file is the name and path of the file you have created.

When you run this, it will take a while to first validate if your inputs are correct. And then it will run the deployment and create a deployable ARM template for you. Which in turn will deploy the same in your Azure Portal. This you can check by going to your Azure subscription >> deployments >> list of the deployments that had happened:


You would see the following resource group to have got created:



Ok. Now let us create a resource under this resource group. Let us create a storage account under this resource group. Again create a new file called: 'resourcestoragedemo.bicep' and you can write the follwoing lines of code to create a new storage account:

var storageacname = 'storagevsn' 


resource storageaccDemo 'Microsoft.Storage/storageAccounts@2023-04-01' = {

  name: storageacname

  location: region

  sku:{

    name: 'Standard_LRS'    

  }

  kind:'StorageV2'

  properties:{

    accessTier:'Hot'

  }

}

Let me explain what I am trying to acheive:

I am declaring a variable called: storageacname and initializing it to 'storagevsn'. This is the name with which your storage acount will be created. Alternately, you can create a parameter of type string, and wait for the the user to prompt the name.

I am create a resource variable storageaccDemo  and using 2023-04-01 API (please change it as per your avalable latest API) and then inside it I am dfefining a JSON, se3tting the name, and other properties like Tier, kind and SKU details. Once you have saved the file, come down and run the following command:

az deployment group create --name ExampleDeployment --resource-group demoResourceGroupsubs --template-file "<File-path-and-name>"

Alternately you can supply the name of the resource group as parameters, like this: --parameters storageAcName=storageac001, where storageAcName is the name of the parameter you have defined in your file like this:

param storageAcName string

Now let us flex our Bicep muscles a bit more. Suppose we need to create and deploy our storage account resources accross a number of regions, using Bicep file. This following example could help you -- where you are making the Bicep work like your any conventional programming language, with a for loop and string manipulation syntax:

var storageacname = 'storageshv'

var regions = [
  'eastus'
  'southeastasia'
  'northeurope'
  'centralus'
  'southcentralus'
  'northcentralus'
]

resource storageaccDemo 'Microsoft.Storage/storageAccounts@2023-04-01' = [for region in regions:{
  name: '${storageacname}${region}'
  location: region
  sku:{
    name: 'Standard_LRS'    
  }
  kind:'StorageV2'
  properties:{
    accessTier:'Hot'
  }
}]


Let me explain here the code:

a. Defining the list of regions/locations as a container/array (don't use commas)

b. Loop through the array elements in a for loop, and then appending the regions along with the sortage name, to keep an unique flavour (the $ command actuall works as string.format syntax).

Let us run it now (the syntax is the same as explained above for reource). Once the deployment is over, let us go back to Azure portal and see what has happened: 

And heck yes, it has created the resource groups, as under the given user group: demoResourceGroupsubs .



Cool, eh? 

That's enough for today -- will come back again with many such cool features on Azure Biceps, soon. Till then yake care and have a nice weekend 👊👊👊



Comments

Popular posts from this blog

X++ : mistakes which developers commit the most

Make your menu items visible on main menu, conditionally,, using this cool feature of D365FO

Speed up your execution performance by using SysGlobalCaches