Create and deploy a web app on Azure, using Azure bicep step by step





¡Hola!
I am sure you must have gone through my earlier post that housed an introduction to Azure Bicep and how we can easily manage mass deployment of resources, dynamically using the same: https://subsd365.blogspot.com/2024/05/manage-your-azure-resource-deployments.html
This blog itself gave you an insight of deploying Azure resources and resource groups using Azure biceps. However in my next blog, I will take a deeper look at Azure bicep, giving you a feel of creating and managing and deploying a bit more complex resources which have dependencies and a lot of overheads to consider.
One of the shortfalls of the ARM template was: your ARM template is always needed to be tagged to a subscription or a resource group. Consequently it's very difficult to create a resource group and then add resources to that resource group. What you used to do as a workaround was: to create two independent templates: one for resource group and then another for resources, and then execute them sequentially.  
Bicep has an even simpler solution of leveragin an 'rg-code' snippet to your scripts. Here goes a demo for that: 
So, yeah -- go ahead and open your Visual Studio code, as an admin:
Step-1: Create a Folder before hand. We are calling it: Example-2. And then we are creating a new bicep file inside it, as we choose Bicep template from the prompt:

Do save your file with the name: .bicep
Step-2: We would be creating a webapp now. Let us name it as 'web-app.bicep'. Start by typing:
resource web-app 'Microsoft.Web/sites@':
And then you can type out all the code all by yourself. But here you have type everything on your own. And chances are there, if you miss anything, there could be unwanted compilation issues, complicacies.
Why don't you use VS code app snippet by just typing: rs- and allow VS code to pull up all the code you need, automatically, including a number of essential properties. Come back to the part of the code trailing after '@' sign and select the updated API version from the list:

We will start by creating a new resource group, using the 'res-rg' code snippet.
So my web ap code looks like:
param location string
targetScope = 'subscription'

resource resourceGroup 'Microsoft.Resources/resourceGroups@2024-03-01' = {
  name: 'rs-webapp-rg-subha'
  location: location
}



I have kept a parameter as location, which is of type string. The target scope as subscription, which it gets during deployment from the default subscription.

Step-3: Create a new file with the name: 'rs-webapp-subha.bicep' --> this is for the webapp, our main objective.
Start by typing 'res-web-app' and let VSCode to fill out the necessary code snippet to create your web-app.
param location string
param appServicePlan string
resource webApplication 'Microsoft.Web/sites@2023-12-01' = {
  name: 'rs-webapp-subha'
  location: location
    properties: {
    serverFarmId: appServicePlan
  }
}
Note that, we are also inputting a location as a string type parameter. We are also creating a parameter called 'appServicePlan' as another string type parameter. And using this as a server farm Id. Don't forget to select the latest API version, as described above.

Step-4: Create a new file called: 'rs-webapp-plan-subha.bicep' which is actually the webapp plan associated with our web app, much like the way we described above:
param location string = deployment().location
resource appServicePlan 'Microsoft.Web/serverfarms@2023-12-01' = {
  name: 'rs-webapp-plan-subha'
  location: location
  sku: {
    name: 'F1'
    capacity: 1
  }
}

Step-5: Come back at our 'main.bicep' file and include a module, one each for our appServicePlan and appService. Include a module and then followed by the resoure name and choose from '.\<file name' which you have created in the previous steps.

param location string
targetScope = 'subscription'

resource resourceGroup 'Microsoft.Resources/resourceGroups@2024-03-01' = {
  name: 'rs-webapp-rg-subha'
  location: location
}

module appServicePlan './rs-webapp-plan-subha.bicep' = {
  scope:resourceGroup
  name:'rs-webapp-plans-subha'
  params:{
    location:resourceGroup.location
  }
}

module appService './rs-webapp-subha.bicep' = {
  scope:resourceGroup
  name:'rs-webapp-subha'
  params:{
    location:resourceGroup.location
appServicePlan: appServicePlan.outputs.appServicePlanId   
  }

}


You will get an error now. The appServicePlanId -- you need to expose this as an output from the appServicePlan file object as a string:
output appServicePlanId string = appServicePlan.id

Yes, you need to add this to your 'rs-webapp-plan-subha.bicep' file at the end.
You can also take a look at the graphical represntation of your app by clicking on the hook icon at the top right corner of your dev window


Which shows you the app details: showing how one app is dependent on the other (we application is depedning on both an appservicePlan and a resource group; whereas the app service plan is depednent on the resource group). 
 

This is Bicep's answer to the issue faced by ARM templates, where we have a dependency of sequential deployments.
Pretty cool, eh? 

Step-6: We need to build the bicep. You can connect to your instance by opening Azure CLI from File >> Terminal>> and then the following 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.

You can build your code by simply: az bicep build --file main.bicep
And then you can start your deployment:
az deployment sub create --location WestUS --template-file "main.bicep"
Wait and watch for couple of minutes, till the deployment finishes. It will show as under your subscription >> Deployment >> the name of the file.
Which we can take a look, getting inside:

So much for today, will come back soon with more such Azure hacks and tricks, soon. 
Stay in touch -- much Love  to you all💓💓💓

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