Azure Bicep Variables and Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we review azure bicep variables and functions [Music] hello everyone i'm travis and this is cerraltos this is a continuation in the series on azure bicep if you haven't checked them out already take a look at the playlist for videos on setting up the environment and using parameters in this video we're going over bicep variables and functions before that please take a second to subscribe like share and click the bell icon for notifications of new content also if you'd like to learn more about windows virtual desktop now known as azure virtual desktop check out my course zero to hero with windows virtual desktop on udemy.com when i started working with arm templates i had a little trouble understanding the difference between parameters and variables parameters like what we went over in the last video are exposed outside of the template we can modify parameters when we deploy the template variables on the other hand are internal to the template we can't change a variable during deployment like we can a parameter static variables alone don't have a lot of use in most templates but they become incredibly useful when paired with functions a function modifies data in a way specific to the function i know i just used the same word i'm trying to define in the definition i'm hoping you have an idea of what a function does it may be better to review the example we'll use in the demo coming up a storage account needs a globally unique name it has to be between 3 and 24 characters long and can be made up of numbers or lowercase letters in the previous video we used a parameter for the storage account name and added a few random characters at the end to make sure it's unique but there's a better way instead of a storage account name we could use a storage account prefix for the parameter then use that parameter with a function to get a unique value we can then combine them into a unique storage account name and assign that to the variable that way we use a storage account prefix and have a unique value appended at each deployment making the name unique well kind of but we'll get into the limitations in the demo there are a lot of really useful functions available for bicep files i'll leave a link to the microsoft documentation below there are too many functions to cover in this video the goal here is to show you how to use them in the bicep file with an understanding of how they work in a bicep template in the microsoft documentation you'll be unstoppable in the demo we're going to update the storage account name parameter to a prefix then create a new variable with a unique id we'll use a couple functions to accomplish that we're also going to update the location to use a function based off the resource group location the demo starts where the last one left off i have a link to the starter file below if you didn't follow along with the previous videos let's get started in vs code here we are in vs code let's get started by changing the storage account name parameter to storage account name prefix we'll append five unique characters to the prefix to create a unique name in the end the account name can't be longer than 24 characters let's change the max length from 24 to 19 that will leave space for the five unique characters we'll use the unique string function to create a unique variable unique however is not random the string returned from the unique function is based on the subscription resource group or deployment the value returned is repeatable so for example we'll use the deployment name for this variable if we run a deployment twice with all of the same values the storage account name will not be unique it won't error however azure resource manager will see that the storage account is there and complete without changes so we'll change the deployment name to make sure the storage account name is different each time it's ran it's important to understand that unique is not random let's go to the space between the last parameter and the resource that's the space for variables to create a variable start with var and the variable name unique id for this example next we'll add an equal sign and the function for the unique string we'll pass in the resource group id and the deployment name that will return a unique id based on those two parameters so resource group dot id comma deployment dot name that will return a unique id based on those two parameters the unique id returned will be longer than five characters we're going to use the take function next to take five characters from the unique id create a new variable named unique id short next we'll use the take function passing in the unique id from the previous step along with the parameter 5 so the first five characters of the unique string are taken we'll use take the unique id comma five now that we have the storage prefix and a five digit unique id let's create a new variable based on the two create a new variable called storage account name or sdg act name passing in the storage account name prefix and the short unique id we'll use a single quote dollar sign and squiggly brackets that indicates biceps should use the literal value of that variable or parameter we'll start with storage account name prefix and right after that with no spaces another dollar sign squiggly brackets unique id short and make sure that's all in between a single quote that will assign the value of the storage account name prefix and the short unique id to the storage account name variable let's go to the end we did this in three steps first getting the unique id then taking five digits from that then combining the name prefix parameter with a storage id we could have done that with one step here's what it would look like in a single line this is simply nesting the parameter and the functions within the value of the variable you can use the one liner or the three steps either way you'll get the same result we can comment out the one we don't want to use with two forward slashes let's move on to the resources for the storage account name it references stgact name it doesn't indicate if that's a variable or parameter it was a parameter in the previous videos now it's a variable this will now use the value of the storage account name variable we also have a location we entered that manually in the last videos let's use a function for this example create a new variable called location we'll add a function that returns the deployment resource group location so this will use the location of whatever resource group you're deploying the storage account to we can reference that with resource group dot location we also have to update the resource to use the variable location instead of manually entering it in with this configuration the storage account will always be deployed to the resource group location we can leave this as is but sometimes we may want the option to change the location when we deploy a resource we can also use a function for a default value of a parameter that way it'll always be deployed to the resource group location unless we specify otherwise we can't do that with a variable because we can't change the value of a variable at the time of deployment so let's comment out or delete the location variable and let's add a parameter for location we'll use pram location that's a string we'll add the equal sign to give it a default value and add the resource group location resource group dot location let's deploy the template next save the file and make sure you're logged into azure create a new resource group with the new az resource group command you can name it anything you'd like for this example i'll use test rg01 give it a location this example will use central us that looks good next run the deployment with the new az resource group deployment command passing in the name of the deployment and the resource group we just created specify the name of the bicep file we just updated and saved you may need to indicate a path of working from a different directory next we need to specify the storage account prefix it might take a second but autocomplete should work if everything is saved correctly and here i'll specify a storage account name prefix everything else either has a default value or a variable and don't need to be specified let's run the command this should just take a minute to finish i'll pause here and come back once it's done no red that looks good let's run the get az storage account command with the resource group to verify it worked there it is the name is a combination of the prefix and five unique characters it was also deployed to central us the same location as the resource group we created if we run the command again i'll pause here until it's finished there it is let's view the storage accounts now and there's still only one that's because we didn't change anything with the resource group id or the deployment name if we change the deployment name run that and again i'll pause here until it's finished that finished let's view our storage accounts again now we have a second storage account and that's because that unique id is based off the resource group id and the deployment name we change the deployment name and that creates a new unique id that's appended to the end of the storage account name that storage account didn't exist so it created it that's how we use variables and functions in a bicep template i hope you found that helpful don't forget to like and subscribe and thanks for watching
Info
Channel: Travis Roberts
Views: 691
Rating: undefined out of 5
Keywords: Azure, Bicep, Functions, Variables, Bicep Function, Azure Bicep, VSCode, VS Code, extension, Azure CLI, Bicep CLI, storage, azure storage account, Bicep tutorial, azure tutorial, training, free training, PowerShell, .bicep, azure bicep tutorial, azure bicep vs arm, templates, data types, allowed values, parameters, secure value, default value, arm templates, infrastructure as code
Id: XytdllxF7vg
Channel Id: undefined
Length: 14min 0sec (840 seconds)
Published: Tue Jun 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.