LetsDevOps: Azure Bicep EP05-Modules, How to create and implement in bicep template.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to Let's devops I am  Sumit and today we are going to discuss about   Azure bicep modules in this tutorial first we'll  understand what is azure bicep module then we'll   see the benefit of bicep module and how it works  and after that we'll see the demo so let's start   a module is just a bicep file which is deployed  from another bicep file it means that modules are   also Independent by zip file which contains  set of resources that deployed together   now let's talk about the benefit of bicep  modules bicep modules helps to split complex   template in a smaller part so it is reusable  shareable and also it focus on a specific task now let's discuss the bicep model syntax so  here we will see how we can declare a module   so for that we need to first use the module  keyword after that the symbolic name and then the   path of the bicep file because module is nothing  but a bicep file after that we need to provide the   name that will be linked deployment name that we  can discuss later also and after that we need to   provide the parameters so parameter that needs to  be passed to that specific bicep file so here we   have some consideration so first like we need to  make sure that model name can't have the same as   a parameter and a variable or a resource name so  it's it must be unique and also when we provide   the path we can provide local path or registry  part in this demo we'll see the local path itself   and after that we have to make sure that the  module name is not static like if you want to   keep a static that is fine but you will have  a conflict when there is a multiple call to   the same module so it's better to keep the  dynamic name that we'll also see in the demo now let's see how module works it is not required  to understand how module works but this will   definitely help in troubleshooting  if you face any unexpected Behavior   so first we'll see how the deployment works so  when we deploy a bicep file using Azure CLI or a   power cell we optionally specify deployment name  if no name given then it will pick the name from   the file itself the name of the deployment  will be the file name assume that you are   using a module in a bicep file it means that for  each module it will create a separate deployment   so if you notice here assume that if I have  created a module called my app under the   main dot bicep file and under the app.biza  file I have created app service plan and app   Service app also in this case when I run this  main.bicep file it will create a two deployment   first with the name of the main and  the second with the name of my app   now let's understand how this generate the Json  file because when we run our bicep file it will   transform all of the file to a main.json file and  that's the process itself we call is a transpile now coming to the parameters and output  in module so as we know it is best to   use the parameters in file so it is also  recommended to use parameters in module   but it is not advisable to use default value in  a module because assume that if you are calling   from the main and you have defined some default  value in main also and in in the bicep module also   then there will be conflict n inconsistency  so it's better to declare the parameter but   do not provide any default value in module  file now let's also talk about the condition we can add a condition in module or any  bicep file which gives us the flexibility   to run or deploy the resource when there  is a certain condition made as for example   assume that I want to enable the diagnostic  static for only the production environment   right I don't want to enable it for the dev and  test so in this case I can set a condition when   there is a enable diagnostic setting is true then  I need to deploy that resources and that can only   happen on a production so this is something that  we declare in a model but we can also Define the   condition so that when the condition met it will  create the resource let's talk about the model   output module also can Define the output which  helps to use in a parent bicep file assume that   if I want to create some resource through the  bicep module and I want that name because the   resource is not it created and I want to use  that resource in a next bicep file or bicep   module then I can use the module output and this  is how we can declare it so we have options to   use output keyword and then there is a storage  account assume that if I want to get the storage   account ID this is how I have to declare but there  is options to get the output from the module file   now talking about the chain module together chain  model together is nothing but a single bicep file   can call multiple modules in a same file so that's  called The Chain modules together so assume that   if I have uh I have to create a VM it means  that first I'll create a more virtual machine   so that I can maybe call that module to create  a virtual Network and then I can use or I can   create a virtual machine so if you notice here  we are using the output of the first module here   if you notice this is the virtual Network that  is the symbolic name of module then outputs and   after that we have provided the subnet resource  ID so this will basically helps to create a VM   so now coming to the demo in this Demo  First we'll try to create a main bicep file   and after that we'll create a module for an  application and then we'll add a model to a   main bicep file bicep template then we'll also  create a more dual for a CDN to deploy content   delivery Network and after that we'll add the  CDN to the bicep template so this example I   have taken from this Powershell sorry this  Microsoft document you can also follow that   and I have attached the code to this GitHub so you  can also follow that so let's start with the demo now let's discuss this scenario so assume that  I want to create a web app and also I want to   create a CDN the content delivery Network but  I want to only enable the CDN for a specific   environment like assume that if I want to  enable the CDN only for production or only   I want to enable that for a specific web apps so  first in this case what we'll do first we'll try   to create a module for application then we'll  create a module for CDN and from the main main   bicep file we'll call this model we'll also  add a condition in the CDN so whenever there   is a condition met then only it will run or  deploy the CDL so let's see that in a demo   so now I have opened the visual studio  code first here I'll create a folder called   not here so basically here and then I will create  a folder called modules and here I will Define   all the modules and to to use the best practice  we need to declare the model with the name of a   resource type so in that if I want to create  a web app I'll just give that app dot bicep   information and since I need to also  create a the module file for the CDN   so let's create a new file called CDN dot bicep  okay so I have uh put the code in GitHub so from   there we'll pick it pick it up and also  attach the link in the video description   so let me pick it up from here okay so under the  module I have this so I'll just copy from here okay and then I'll paste it here so this  is basically if you notice here this is for   creating a app service plan and then web app right  and here if you notice we are not defining any   default value so as for the best practices  in the model we do not need to define the   the default value for the parameter okay so  here we are creating the the app service plan   and the web app now let's take the the code  from the CDN so I'll just use this foreign so that's the reason I'm just  kind of using the existing code   and uh yeah so we created the modules uh with the  name app.bicep and then cdn.bicep and kept it in   the module folder okay so let's create a main  bicep file from where we will call all this to   okay now in the main dot bicep file I have  already declared the parameter that is required   to call those modules so in the app.bicep file  if you notice we need to declare the location   app service name app service plan and SQ and for  the CDN we just need a hostname right so let's go   there and as you know that the host name will only  we get to know once the the web app is created   so here we have defined the parameter with the  default value let's call those modules one by   one so first we need to call the app module  so for that I need to use the module keyword   and after that the symbolic name and then  I have a modules so here I have to provide   the path so since I am going to call the  app.bicep I'll just point that and then here   okay so here after that we need to uh provide  the name which will be used in deployment name   so maybe launch the knife and app and after that  I have to provide the parameters right so since   we already defined the parameters here you just  need to call one by one so app Service app name   with the app Service app name I have  service planning with app service app service [Music] so if you notice  here still we have something we need   to provide like we have the parameter  is missing so this error will also give   you that what is missing so right now  location so let me add the location it's already defined right so that's all for this  particular module let's call the CDN now right   for the CDN I need to declare this app and  I have declared the symbolic name as a CDN   here I need to call the CDN here we need  to put the condition right we don't want to   create a CDN for all of the websites so assume  that if the condition is the plus idiom is   assume that if it is true then only it will run  that right so now I have to provide the name   so it will be long maybe this is the name of  the CDN you can always also give that the name   with some Dynamic but yeah as of now it's just  fine and after that I need to put the parameters   in the parameters I need to Define two parameter  one is just DPS only so it is true and the next   is uh the four stars oh yeah original host name so  as of now this host name will only come from the   app right because we don't know that name is what  is the name because we are using the unique strict   so for that I need to call by the name right  this symbolic name the app dot outputs dot apps   okay so for this demo I've just made it as  a fault because it will take a lot of time   to create a CDN so but as in that in your  case if you wanted to deploy the CDN also   you can just enable so let's run it for  that I need to First connect to azure foreign [Music] and then I will just provide the template file template so yeah I need to just call the  main dot bicep because that is   also calling the another module so  I'll just do it like that and then that's all right so maybe I need to provide  the resource Group and then I'll just say sorry now I'll run it so deployment got  succeeded let's go to the Azure portal   and since I've applied to this rsrg so I'll just  go to the deployment and under here I see the two   deployment is created one is for the main that is  uh basically it is calling the the modules that is   the main and then another is a launch app and this  main is basically picked up from the uh because   we did not provide the deployment name and the  launch app is picked it from the app right so okay   so let's go to the resource current here you see  this resource is like app service plan and the app   Services got created so right now we haven't  enabled the CDN but you just make it as true   and then you can run it on your case that's  all for this demo and this tutorial so today   what we learn we first learn the Azure bicep  modules what are the benefit of bicep module   how we can declare a bicep and then we saw the  demo for creating a bicep module for app and the   CDN and then how we call it from the main dot  bicep file I hope this was helpful if you have   any issue or concern you can write in the comment  box I will be happy to help I have attached all   the link of the GitHub code and my blog in a  video description you can follow from there
Info
Channel: Sumit Raj
Views: 165
Rating: undefined out of 5
Keywords: LetsDevOps
Id: I0ZS6qp18o8
Channel Id: undefined
Length: 14min 51sec (891 seconds)
Published: Mon Jul 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.