ARM Templates Parametrization | Expressions, Parameters and Variables

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Keep up the great work!

👍︎︎ 3 👤︎︎ u/WellYoureWrongThere 📅︎︎ Feb 26 2020 🗫︎ replies
Captions
hey guys this is Adam and I'm back with another resource management templates video this time I will show you how to parameterize your templates using parameters variables and expressions stay tuned our focus today will be on expressions parameters and variables while the parameterization topic is much bigger those three are the building blocks for all the additional features that you can use to parameterize your templates that's why they're our focus for today let's talk expression first if you have a template that deploys a storage account you can use expression to parameterize the template and in simple terms expression is just dynamically executed code which you can invoke by using square brackets and within those square brackets you can use functions to parameterize your templates there are a lot of functions that you can use with an arm templates but their general purpose is to calculate dynamic values for your template it is important to note that because those functions are being evaluated during the deployment and they will always compute some sort of value that will be replaced in the template during the deployment itself and there are many things you can actually permit right so let's grab few examples if you are deploying services that always require unique name within subscription region or maybe globally unique string generation is really good example you can use unique string function pass an entry string parameter and it will generate random string for you based on that input string it is important to note it will always return the same string the same unique string for the same input value but it is amazing when it comes to generating random names for your resources so you don't get collision during the deployment another great example is research group function allowing you to access information about research group that you are currently performing deployment into it will return in an object with all the properties describing the current research group and you can actually access properties of that object by typing dot and the name of the property which will return you the value of the property so in this case it would return north Europe very often during the deployment you will find yourself in a need to create a dynamic name resource and concatenate multiple strings to do that you can use concat function which grabs multiple parameters and concatenates them into a single string while there are many more functions one of the cool things is that he can actually nest functions and use them as a parameters to another function let's take this unique string as an example if you're gonna pass a parameter of 1 to 3 to that function it will return a random string you can then use unique string as a part of concatenation function to ensure that you actually create some sort of naming convention for your storage accounts and still give that unique part to the name to ensure the deployment will not fail because of the name collision for the storage account there are dozens of functions that you can actually use here and you can pretty much combine all of the things that I showed you right now to create any kind of urn templates to fit your business needs while there are many more possibilities with the expression itself one another key thing about parametrizations of the templates are parameters this is your way to provide input values for the template they are defined within their own section called parameters where you define a unique name for a parameter like in this case it's account name and each parameter definition is an object with only one mandatory property of type which defines the datatype for a parameter it can be a string integer boolean secure string which basically obfuscates the information from the template logs it can be an RI or even an object once the parameter is defined you can actually start referring it within your template from this point onward you can call it anywhere within a template using an expression and calling parameters function and passing the name of the defined parameter so that during the deployment if user passes a storage 1 to 3 a.m. as a parameter our resource manager will use that value and put it in the right place where ever you define it in your template as you see the idea is simple so what are the additional features that you get here first of all we can define default values so if user doesn't specify an account name during the deployment my storage 1-2-3 will be taken as a default value while the type is your first way of validating the input parameters allowed values is another way this allows you to create a static list of all the available options for des permit this is a great way to create an initial validation for all the options or allow only specific values for your templates this allows you to ensure the quality of the parameters that are being passed or maybe restrict some of the options available to your users and there are a lot of different types that you can use here like strings boolean values integers or even complex objects which are JSON defined objects you can use our eyes and our eyes can contain different types you can also get some additional features like min value and Max value for some integer validations you can use min length and Max lens for our eyes and strings or you can use metadata to describe some of the parameters there's a lot of options that you can actually play around to make sure the parameters for the templates are as generic as possible while maintaining the quality of the passed parameters which brings us to our last topic variables so if you already know how to use parameters and you know how to use expressions you can achieve pretty much anything within your templates so what is the variables imagine a scenario where you already use your parameters called account name you then use a concatenate function to add a backup suffix to this name and this is your name of the storage account you can actually move this into variable section which is your centralized place to define all the calculated values and reuse them across the template this allows you to have very clean templates very readable templates and still maintain all that flexibility by using parameters and expressions this is a highly recommended approach for building pretty much any arm template the most common examples of using variables is for instance randomizing names which allows you to create a unique names for your resources whenever deploying them from our template while still maintaining that flexibility and the second example that I find myself very often using is adding a prefix or a suffix to my environment or service names this allows me to ensure the proper quality and naming convention of my application resources across multiple environments which brings me to benefits there's a lot of benefits for parametrizing your templates for instance you will be able to reuse your templates multiple services multiple environments and multiple products you'll also eliminate human errors because it is almost inevitable that at some point a human who will need to perform the same appointment over and over will make a mistake that's it's just in our nature we'll also be able to model otherwise your templates you will have easy ER maintenance and have very clean very readable code that you can very easily see what are the components currently used within your application what are the typical scenarios oh it's easy for me to say but I would say always you should always build armed templates whenever you're developing another it will really go a long run automation is another option you can use it for DevOps you can do environment migrations and you can even create a reference architectures for your companies so that other projects can see what are the typical scenarios and typical configuration for the resources that they should be using within their projects it is really good way to apply some security measures also and ensure the quality of the configuration of your resources but with all that said let's get into demos I have a couple of demos for you today but most of all they are about expressions and testing of those expressions and then we are going to expand by using parameters and variables I'm gonna show you how to use all of those combined to get most out of your arm templates so let's go to the portal before we begin let's go to the create resource and type template to find the template deployment and let's actually turn it to dashboard so you can very quickly access it during the demos and we're going to be using that extensively today and let's go to the visual studio code where we're gonna start creating our template let's create a new file let's call it armed trace on and let's initialize new template by using scaffolding so type armed tab later and it's gonna create a new template I will actually revert a schema version to 2015 or 101 assets the recommended version for template deployment for the research groups and can actually spawn this a little bit and I'm gonna remove sections that we don't need right now so I'm gonna remove this function section and I'm gonna remove parameters and variables so let's just leave forces let's open this section and paste ed standard template for the storage account I'm actually gonna use the one that I prepared previously so I'm gonna use this so let's look at this template the type is Microsoft storage storage accounts so we're going to deploy the storage account it's gonna have a static name just to show you the principle and then we're gonna use the parameters to parameterize the template let's grab this template right now and let's go to the portal to the template deployment let's hit create hit built on your own template paste it in hit save and let's deploy that I'm gonna actually full-screen death so get more real estate let's expand let's use resource group called arm parameterization scroll down it I agree to the terms and hit purchase we've got some validation errors and that was expected if I'm gonna click on this I very quickly see the storage account with that name that I picked a.m. demo storage intro a 1 is already taken and I did that on purpose so we actually have a reason to permit rise this lets parameterize the name so that doesn't collide with already existing resources let's use the expression to do that so as I said invoke using square brackets and within square brackets we are ready to go and ready to use functions I'm gonna use unique string as you see intellisense will tell me there is a function called like that you can press stop and I need to pass any input parameter and there are a lot of ways to do that you can either press a static string like 1 2 3 but this will always return the same value so you can pass something more dynamic like accessing a resource group information and passing the ID since each resource group has its own unique ID this unique string will be different for each resource group that you're gonna perform the deployment to but it will always be the same for the same resource group allowing you to update the storage account since the name generated will always be the same within this resource group so let's grab this update the template and let's go back to the portal let's actually hit edit template let's paste a new one let's hit save leave everything as default scroll down again and agree the terms and hit purchase my wait for the deployment once the deployment finishes you can actually go to the resource group but first let's review the deployment itself by clicking this link you go to the deployment you can expand and notice first of all what is they uniquely generate that name of the storage account that we've got that it deployed successfully you can review the input as you see we have no input and no outputs and you can review that deployed template if you actually go to the offer view you can navigate to the resource group to see your newly created storage account as I said if you would deploy it a template again it would not create a new storage account with the new name with what actually update the existing one because this unique string will always be the same for this resource group if you're gonna pass an ID as a parameter so is there any way for you to know what is the name that will be picked up before you actually execute is there any way that you can actually see what the expressions are returning before you deploy anything into Azure and the answer is yes you can you can actually create a new file let me create an arm to the adjacent and create a template like this with deploys no resources at all but uses an output to return an expression in which case unique string of varys's group ID and if you try to deploy that you would be able to see in the outputs without actually impacting any resources in your outer environment so let's try this if we do it to enter roger portal and hit on the deployment hit create again build your own template paste it in it's safe notice actually nothing happened and purchase button is grayed out the reason for that is this custom deployment doesn't actually allow you to perform deployment with no resources but you can actually do it and execute it using cloud shell or power shell or CLI so let's go to the cloud shell and let's create a new file called armed dot Jason and within that file let's base the template once you have the template you can actually press ctrl Q to close it and let's paste a script to create the deployment so first of all we're going to create the research group called armed 0-3 in north Europe and then perform a Z group deployment create passing the research group name and the armed adjacent as a parameter after about 30 seconds the deployment is done you can either review outputs here in the JSON response finding the unique string and the value that it calculated or you can hide the cloud soul go to your resource groups find the r0 free resource group and open it open the deployments and find the arm deployment that we just created click on it and go to the outputs to review unique string and the string that we generated and this is pretty much the same as in template we defined a unique string output and PI pasted the expression there and you can use this method to review the results of the expressions if you are unsure what kind of results you're gonna get and you don't want to mess you with your resources in your art resource group so let's do one more quick demo using outputs I'm gonna paste a bit more complicated template where I'm gonna create four output parameters our g4 rhesus group I'm gonna put research group dot ID I'm gonna run unique string over that ID and I'm gonna do the same for the deployment dot name and then do the deployment unique string over that I want to show you how the scope of the unique string changes whenever the value changes so let's grab this template let's go again to the cloud shawl let's open the code editor let's place the template let's hit save ctrl Q to close and let's run the deployment again let's paste in the script that I prepared this one creates armed 0 for group again in north Europe and runs two deployments one thing to note this time I'm passing additional parameter called name and passing demo on this is not the parameter for the template this is just a name for the entire deployment so let's run both of those as the script is done I prepared a very small listing with query which will return that all the outputs from all their deployments that we just did so we don't actually have to go to the deployment of the group and check it ourselves so see see the research group was this as you see before because our research group name didn't change the ID didn't change the unique shrink also didn't change but because the second unique shrink was basing on a deployment name and we changed the deployment name across those two deployments the unique string deployment changed this is how you can create scopes using the unique string remember it's very valuable lesson depending on what kind of unique name you want to generate for your resources since we know how to use expressions we can actually go into parameterization using parameters so let's go to our basic arm template for the storage account and instead of using unique name generated here let's use parameters so let's create a new section called parameters and this is of course an object within that object let's start defining parameters I'm going to create a parameter called account name and this is also an object within that object as I said you always need to specify type this is the only mandatory property and as I said you see you have couple of things you can actually pass here secure strings and secure objects are basically just obfuscated objects and strings for now we're going to use a string since this is the name of the account and not that we define the parameter we can start using it go and remove that expression and start using function called parameters and you can now as you see intelligence actually picks up that you have parameter called account name which you can use here so let's press ENTER and the CSEC squiggly lines went away because you are now using it was just a warning that you defined a parameter that you never used which is another cool feature of using Visual Studio let's grab this template which is currently permit riced so let's go to the portal we can now close the cloud show let's go back to the dashboard and perform the new template deployment let's hit create build your own template and paste in the template notice that first of all it already organize the Dharan resources deployed and we have new parameters defined if you hit save you're gonna find the new parameters available for you to for the template deployment let's select the resource group arm power metallization and notice in the setting section it recognized that you have some parameters that you can use here and you can actually start using that in and pasting the values for instance am demo store or one AAA and I can hit agree and head purchase so let's wait for the deployment as the deployment is finished let's go to the resource group let's hit refresh always remember there's a slight delay but we already see our permit right storage account created let's go go back to visual studio so what else can we do here for instance we can create validations we can create a min length and specify that minimum length of the account name must be free characters we can also use marks length to define a maximum length is 24 characters additionally if we want we can add something called metadata and metadata will describe what is this parameter for so let's paste this and give it a unique dns name of your storage account so let's copy-paste a template and let's go back to the portal to the template deployment let's get safe and notice what's the difference first of all you have this eye icon here the information giving you the description from the metadata object about the parameter this is pretty cool if you're sharing the templates across your teams so they can actually very easily read what are those parameters for additionally if I'm gonna type only one character here notice that the validation works saying you that you need to provide at least three characters for the name so it is a validation before the deployment which is very neat let's go back to the template what else can we do we can for instance define a parameter for the SKU of the storage account by creating another object this will be also an object of type of string and we can for instance use allowed values to space if I order the allowed values for this storage SKU I'm gonna paste in the allowed values with two values standard alaura's and standard GRS and additional metadata about the approved SKS for the deployment notice that the squiggly line is there because we define the parameter we never used it which is very nice reminder from the visual studio intelligence so let's go to the SKU and use it written let's remove value open square brackets type parameters tab and then pick storage account SKU if everything is working we can actually copy paste this go back to the portal hit edit template paste the lightest template notice that it already recognizes two parameters it's safe there's a new parameter that you can now choose and because you specified allowed the values it actually gives you a drop-down then you can pick that you will use for the deployment that is pretty cool but before we deploy that let's maybe add one more parameter what else can we do with parameters let's create a parameter called location an allocation is also an object this is also string since location is a name of the location but this time we're gonna use a default value and we're gonna use expression to initialize the default value so we can use that default value for the location of deployed service will be the same as the location of our research group remember squiggly lines that means we never used it so let's not forget about it so let's remove the location here and paste entered parameters and use the parameters called location now you have very highly parametrized template you could probably parchment parameterize more but this is enough for our example so let's grab this let's go to the portal and the template page the latest template it's safe now we have three parameters so let's just give it a name am demo store or 1b I agree to the terms and hit purchase we didn't pick up the research group so we need to select arm parameterization and hit purchase now let's wait for the deployment once the deployment is finished you can go to the research group to find your storage account remember all the delay but what is most important is that you can actually go to the deployments tab to find your latest deployment open it and review what was deployed with the template including the input parameters that were passed during the deployment of the template and if you go back to the research group you will find your lighter storage account deployment there so let's go with the very last demo about using variables for the demo value of the variables I want to create a template parameter which will specify stage of the environment that we are currently deploying to like the velopment test qy production and to do that I'm just going to quickly copy/paste new parameter into the template this parameter will be called state will be type of a string the default value will be test I'm gonna allow only 4 values test def QA and prod and this is the description environment stage for our deployment now I need to actually start using this parameter I could either directly use it within our resources definition or I can create new section called variables this section is also an object and within this section you can create new variables I'm actually gonna create a new variable that is called the same as our parameter called account name and once I do that I will actually concatenate so I'm going to use expression to concatenate two strings I'm gonna make some space for to make this very visible and the first one is parameter and I'm gonna pass the account name parameter and I want it to be concatenated with also another parameter in this case it's gonna be stage so it will grab the name of the storage account that I passed as a parameter it will also grab the name of the stage that I passed it will concatenate the both and we're gonna use that as the name of our storage account to use that variable within the template it's super easy to change now to scrub the variables name and there's equivalent name of the function called variables which you can use so just by replacing the name of the function you can refer to variables in pretty much the same manner you see if you see that variable also has a different color coding so you can very easily recognize whenever you're using variables or parameters within your template if this is correctly done we can actually grab this template and go back once more to the template deployment and create new template deployment and paste in the script we have now four parameters which everything looks fine hit save and specify the new account name so I am the most or one one in this case I'm not gonna pass anything extra I'm gonna leave stand all our rest and make a stage called dev I hit agree and hit purchase I need to always pick the resource group and again purchase since the deployment has been finished we can go to the resource group and hit refresh as you see the new resource was deployed we have a suffix dev this allows me to consistently deploy the resources across environments and ensure that suffix about the environment name and environment stages always they're applied every single time and inside of the deployment you can also review the latest deployment review the input parameters and see that I deployed the account name what stage did I pick there were no outputs because we didn't define anything and always review the template always remember that since I'm deploying using portal or deploying using CLI remember to specify that unique deployment name or otherwise it's gonna overwrite the history of the deployments other than this you're pretty good to go and this is how you can actually use parameters expressions variables functions and all those kind of stuff to parameterize your templates parametrization of your arm templates doesn't stop here there's so much more that you can use you have user-defined functions references orchestration tools and much much more we're gonna learn all about those but that will be in another episode if you liked the video it funds up leave a comment and subscribe you front to see more and definitely see you next time [Music]
Info
Channel: Adam Marczak - Azure for Everyone
Views: 20,965
Rating: undefined out of 5
Keywords: Azure, ARM, Resource Manager, Infrastructure, IaC, Infrastructure as Code, DevOps, Deployment, Code, tutorial, Parametrization, Param, Params, Parameters, Variable, Expression, Function
Id: A3OtNevzXaA
Channel Id: undefined
Length: 27min 2sec (1622 seconds)
Published: Tue Feb 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.