Azure part2 (How to create Azure resources using bicep files)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
mother mr automation is back today with some automation on azure infrastructure as code on azure how to manage resources with bicep files let's go to the demo for that or the presentation i meant so infrastructure is called on azure managing resources with bicep so what is bicep i'm going to explain that a little bit what's arm i'm going to explain that as well we're going to install bicep if needed i'm going to show you how that's done we're going to do a lot of demoing uh creating something from a chris storage account from scratch for instance and then we're going to modify that we re-run it so we can see the item potency as well in the picture so to speak there's a lot of demoing but first a why bicep you already have like the json approach to create resources inside of azure and perhaps it helps a little bit if i explain that in some more detail so imagine you're uh this is an azure resource for instance this sql server right this is a virtual machine this is a data factory i just gave a couple of examples of resources inside of azure the azure resource manager very applicable name is able to facilitate all resource automation and creation and validation and such and that's done by providing the arm with the json file javascript object notation file and perhaps i have an example here let me see if i have that i thought i had it yeah here for instance so this is an example and this is an example of a storage account for storage account in a json format this format is understood by the azure resource manager this guy there that accepts that json file here and that is able to deploy the specific resource inside of azure okay where does bicep come into this picture right so i create i created a little drawing here and so imagine you're sitting here with your visuals studio and you're creating json files here right so just add a json file there you can basically push the json files to the arm and it will deploy the resource as specified in your json file for instance this one is a storage account right i can deploy that but the syntax of json is it's not bad don't get me wrong here but it's a little bit cumbersome if you want to do some programming around it perhaps some looping you know for each or it's it's not that rich you can certainly provide external parameters with parameter files but there are some options to make it a little bit more dynamic but honestly this is where bicep comes into the picture it's item poland i'd important just like this one by the way because bicep basically lays on top all on the json basically what happens we create some bicep curl in a second and we run a command in azure to deploy that template that bicep template that's understood by uh by azure but what happens under the hood bicep translates to json file and then arm does his stuff basically we are extracting away the complexity of json like c there's a little bit overhead there you will see that in a second why a bicep is a lot more powerful and that's it's declarative as well idempotent let me go back to the presentation here so it's a little bit more powerful than json microsoft promotes bicep on all their resource automation by the way not json but biceps the wrapper around it so to speak it's early available basically everything that comes available inside of azure resource manager arm is instantly available as well inside of bicep and the json approach if you click around in the azure portal sometimes it can take a couple of months before that functionality is there available it's already available from an api perspective but not yet inside of the azure portal it's easier to maintain and read the bicep file you will see in a second why that is the looping capability syntax there's a lot of programming intelligence inside of it basically it's a sort of programming language it has very rich statements what is arm already i have a link here by the way to microsoft documentation about the arm and i'm quite sure that the documentation will give a better explanation than mine i am able to do so uh but this is in a nutshell what happens as your resource manager like i also showed you my picture is deploying all these resources here that you see here down it has an authentication layer there so and then you can talk the azure portal talks directly to the azure resource manager powershell uses an sdk and this one as well so that's the arm inside of this picture so let's do some demoing right enough talking i guess so let's uh close this let's go to here and create a new file we call it the part two dot bicep okay [Music] i also need to explain to you that you need to install bicep with i'm quite sure that you will be able to reach out to the internet and install bicep and if you for instance run an az command and that deploys in a bicep template that will automatically install or enable the bicep executable so to speak otherwise you need to install it and when you have it installed to have bicep available you see you can then run a bicep minus help for instance that means that you have that available uh without that you cannot start it's a little bit i don't need to explain how to search on the internet right to install bicep just install it but you need it to proceed for this demo then you create the bicep file like i did there and it's it's uh i'll start very simple so just create a resource i do tab completion yeah i love the intellisense by the way truly rich so i'll give it some name i call it my storage account my storage acc i press a space there again it's all intellisense so i do storage so i'm interested in a storage account so i do storage account then you specify the api version you can specify several i just take the latest one the newest and then you do an equal sign you can also do an existing if you already have an account but i don't have that and then you can do you can just open and close and start typing here you know and then you see what you need but what you can do as well i think you can do it here as well i'll show you the better way so i'll close this i'll remove this and when you open this required properties and i just do a tab here tap then i have everything required to create a storage account so basically we have a requirement that is a name for a storage account makes sense i guess so let's give this a name 2411 location you can of course specify west europe north europe or anything but you can also do some dynamics here so for instance what you also have this resource group dot dot location and that basically means the resource group you have inside of azure i have here a resource group that is a bound to a location you see here north europe my my resource group is running there and what i do here is basically inject some dynamics right the resource group dot location and we will see that in a couple of more examples what you can do here you have a sku and a name i don't know what kind of name there's a trick there so if you do control space you see all the available values right you don't need to guess so i'll take a local lrs local storage not redundant and the kind i don't know what kind control space so let's take storage v2 i don't need to block blob storage i take a storage v2 account and basically this is it already and we're going to advance this a little bit with some parameters and variables even but this is a basic example on how to create a storage account let's create this account so here i have a little script here perhaps i don't need to explain it basically the script connects to azure and let me run some azure commands right i need that to be able to do some automation against azure of course so basically what we do here is first signing in i'll show you where it comes from this connect ac account that is what you see here you don't see that by the way but it's popping up the signing window for for azure i cannot share that forgive me for that but basically we're logging in now in the back on azure and we're running this code and it will take some time you see it's connecting now it's trying to get some subscriptions i have some issues with subscriptions by the way that's besides the point then we get the resource group and then basically now we are connected to azure so i stopped the debugging here and what you get what you can do is a new az resource group deployment and then you feed it a bicep file which file we just created here right this part and that bicep and we're not going to run it yet i'll show you my resource group here and i'll refresh it and as you can see i have nothing inside of my resource group right demo rg i have nothing there currently storage account wise so now let's run this uh this file and let's see what happens if we get an error it could be i don't know so i press f8 on this line 24 and now we're executing that template file that we just created now let's wait a little bit for that i think you can also see that here in deployments and you see that part deploying you see that for four seconds now that's our uh new ac resource group deployment command that's running and you will see it here as well it's deploying something at least so we wait a little bit and we got some output put back here so yeah it succeeded it's pretty cool so we will see the same thing here i think if you refresh it's much probably you see it succeeded and if you now go back to my resource group you should have sometimes you need to refresh once more let's see then we have the storage account right this storage account we just created that from scratch and um imagine we want to uh change something on the on the storage account right at the property or something like that or a value perhaps we want to add some tags here that's a nice example we're going to add some text there let's go back to the bicep file and let's do it under location here i think tags yeah we can do that and i think it's a sort of an array i think you can then do tag one something like that i'm not 100 sure uh value one needs to be a string i think like this uh i don't know if it works we're just going to try it right and let's run this my demos don't need to be perfect you know i don't mind making mistakes and recording them as well and showing them to you as well now i don't like those made-up videos pre-baked and pretty fake as well copy and pasting code from the internet most of the time so i'm really legit so to speak right no i'm just kidding but as you can see here we now have a tag one value one right so let's go back to this uh bicep file again this one and let's add another tag but add some more dynamics okay so we call it tag two and let's create a parameter here right so you can do a parallel and then you give it to some some may someday and then you specify uh what it is and for now we use a string and we can assign that the default value uh value two right we already have that there as well to close that up yes like this uh yeah force of habit uh i think we need to use single quotes here for a string value let's see yeah works a little bit better and perhaps we call this tag two right it's a little bit more makes a little bit more sense what you can do then is here friends on tag two you can we can uh relate to that value we have inside of this parameter right and i'll show you when we run this that we should get a second we now have one tag here right you see that one tag inside of azure let's size it a little bit up one tag value one right on our storage account go back to this bicep file we just added the parameter down right some input with the value two and here we specified it by the name we gave it here right so we use that value and i saved it and i'm going back to this i will close this old one by the way now i will run this template again let's see if we get another tag right with the value it's already completed i wanted to pause the video but apparently it was not needed was quite quick so let's refresh this and there we have it value 2. now let's overwrite the value of tag 2 for now let's see the item policy here right in in work item potency i meant so we go to this file and let's uh change the value to value to new right we have a new value now okay and we're going to push that back again with this group deployment templar command that and we wait a little bit and again we deployed something and let's refresh this in the portal in azure and you will see this new value 2 also being reflected okay let's take it to another level a little bit more advanced so imagine now we have this parameter i'm hardcoding this value all the time here you know it's hard coding doesn't make much sense you can actually remove that as a whole right and then provide it at runtime but you can also leave a sort of default value right so we will call it value default for now default default yes and what you can do if we run this a z group deployment that this command that looks inside of your bicep file right it's it's really dynamic and it knows that we have a parameter tag too it knows it so what we can do then is during runtime we can do a minus tag to uh from command line okay so that's the tag we provide now to this and let's see if it overrides this default value okay so let's run this and wait for it to complete so and now we go back to azure and we refresh our portal and we will see that now from command line was added as a so you can see the power right from the on the parameters if if i now remove this tag 2 again and i run this deployment again it's using this value 2 default value right we just specified that and imagine the power you can specify more parameters here of course right tac to tag three location name school uh kind you know everything you want inside of there you can specify it as a parameter you can also specify a var if you do a little bit more hard coded but you can create a dynamic file as well right do some uh querying there perhaps on there to retrieve a value um let's go back to our storage account and as we can see we now have value to default back again um i think this is it for the simple example let me see so we created the storage account from a bicep file we modify the bicep fabric changing the value we did that we added the parameter or var let's use the far as well we're going to add the variable as well i did not show that yet so let's do that now create a far sku equals very simple i just take this name there just copy it's a little bit lame but you just need to know how it works so then you can provide that value there right without the quotes around it because it's now a value they are stored right that held this this value and that's then uh added there or for instance we could even do a far kind right and we specify storage v2 there you get the picture i guess okay so you can use parameters at runtime to provide value so you can wrap a script around it you know and provide some dynamics that's the best approach of course working with parameters not anything hardcoded here for instance i have the storage name hardcoded it's just horrible of course but for this example we could create a param and actually we're going to do the param param name string equals i will give it a default value again sorry for my typing mrb2411 again and now we can specify the name there right so this param name move it up a little bit we can use that now there and at runtime we can overwrite this value so let's do that as well so we'll go back to this part two and what you can do now is minus name right we gave it a name so what you can do here minus name uh mrb2412 right it's different name so basically what we're doing here is creating a whole new storage account again so let's try that so let's let's run this see if we get an error i hope not and like i told you i'm not gonna cheat failed it still used old value so i think perhaps a name i'll use st name for storage name and perhaps the name you know it makes sense that it's perhaps reserved for a special meaning oh no no i made a mistake there right i did not i don't know i did it correctly so let's let's run this again let me go back to here alright again let's see if we now get and there's now st name of course we change that we change that and let's run it again and see if we get a new storage account so there that completed now we go back to our demo resource group refresh it let's see we have nothing yet let's refresh it again brexit a little bit too quick sometimes it takes a little bit of time so let's wait a little bit more yet let's refresh it a couple of times there we have it north europe this default value so let's let's change this tag to for this system and then we're done with this simple example so we'll uh use that name but we could also specify uh i don't know tag two right so minus tag two tag two uh override value right we overwrite it's that value i'm going to make it a little bit more readable here by providing a backtick here it's horrible but it's quite needed now and uh let's run this commandlet again let's wait a little bit there we have our result back and there we have if we refresh this first a refresh then we see now the overwrited value so that was adding a parameter or a variable perhaps we can yeah we can also create a wrapper script that creates like 10 accounts perhaps that's a nice one to uh to add here so for instance we could do something like uh for i equals zero uh dollar i less than 10 right we keep it less than 10 and then we increment i said increment sure and let's see what's inside of i just otherwise like to troubleshoot a little bit so if we loop this let's see what we get okay you get one two three four five six seven eight nine so that's uh that's fine so we we can use this so what we basically can do is then move this part inside of there right and for instance we just uh append the idea right so what happens here is mrb two four one two and then it depends one two three four right seven eight nine ten and we also use the overrided value there overrided value and we specify some dynamics there okay so then this this i value comes inside of that storage account tag two right override value i that will be one two three four five six seven eight nine um let's run this and let's see if we get like uh a bunch of storage account right i run this and i'll i pause it as well a little bit otherwise it takes too much time so perhaps it's still nice to go back to this crappy uh paint drawing i created excuse me for that but i think the the picture should be sort of clear right i'm sitting here on this so let's take another caller this time i'm sitting here on this uh visual studio box i'm creating this bicep file right you saw that in my editor we added some dynamics there with uh with the palms you can externally manage that right you can feed that at the runtime you also had fars those live inside of the bicep file it could be that you have like a default variable that applies to every object you create you can stick that inside of affair there's no need to create a pattern for that this bicep file then gets transpiled to json that's invisible for us that's done by this by running this commander this new ac resource deployment right this guy transpiles this bicep file we have open here in this bicep file it translates to a json file to json file right and a json file i showed you an example already this is a json file example for a storage account you see the syntax is very different yeah so that's what happened so this is transpiled to a json file then the json file is handed over to arm by that command that we're running and then arm does some checking and validation on it if you used valid values for instance storage account have a sort of restriction if you make it too long storage account or too short it won't be accepted it will get a failure so that means that actually it will never deploy so friends we have here a storage account right if the validation here fails on the arm it will never even try to deploy that account so you never get like a half deployed count or something like that so it does some validation there if it fails it will communicate that back of course to my official studio there right that it failed and um let's go back here uh i think it's still running it's created some accounts already so perhaps you know there's no multi-threading so perhaps we can check the portal already and see if we have some new storage account already uh yes as you can see here right zero one two three and the four was now being created so that that piece of code is still running in the back and i showed you as well if you go to your deployments inside of your resource group you can see actually see those deployments running here and we need to wait for those 10 accounts so i'll pause the video a little bit more currently it's creating an account mrb this one 126. so we need to wait i think it went to 29 so we need to wait for three more accounts to be created count 28 being created i did created some videos in the past about multi-threading and you can apply that technique here as well you know creating the storage account with some multi-threading uh just check out my channel for that i have a couple of videos on multi-setting and what multithreading does is basically deploy them all at the same time right not doing storage account one then two then three then four it deploys them all at the same time so that's that's the the big difference there so imagine you have here 10 storage account and now we're looping through one two three four five you know and then every time we set up a connection we create that account get it back and we create the following account you can actually implement multi-threading and just send those 10 accounts at once to this arm with some multi-threading right so at once and then all those accounts get created uh simultaneously and then you get at some point you get the result back that you have your 10 accounts available will be a lot faster of course but for this demo i did not bother to implement any multi-trading let's see okay it also finished with the last account so let's go back to our demo group here and go to our demo group and let's see we should have a bunch of storage account we have them here as you can see they are all there and i don't need them of course i'm going to delete them in a second um let's go back uh export the whole resource group to json and recompile the to bicep file yeah that that could be very interesting let me uh let me focus on that a little bit yeah and then there's an export ac resource group command that accepts a resource group name as input and i have that have variable already available here so when i do that it exports everything inside of the resource group to a json file right the json file so i'll show that hang on let's wait for that now it's exporting this resource group this demo rg so basically all the storage accounts and let's see yeah we've got something back demo rg json so if we now go just do this json file this is an export of everything we created with the bicep file right all the storage account all these services and such and such and such and uh how do you convert that to a bicep file right you want to know how that is done and i will show you that so with bicep bicep installed you can do a decompile the compile asset of this demo demo to json right so we're decompiling the json to buy some file at least we try all right let's see what we get back there are also some warnings and on best effort and such and there are some read only properties that's all correct so we need to remove them from the file that's beside the point besides the scope of this demo i meant what you can see clearly here is that now you have you know parameters for the storage account name string see we have all these uh json objects now in a bicep format so if you ever are curious how to json file you need to convert that to bicep file you can do that with bicep space decompile and then you specify the json file you export it right with the from azure and then you get a bicep file which basically you can then use for deployment right you have a python file you need to modify some stuff here and there of course it's not perfect like this value it's not even allowed to set that i think outside of this so you can actually remove that as a whole uh that's beside the scope i just wanted to show you how to decompile a json file and you can also do a graphically not fully but you can certainly do some stuff there what you can do is uh go to your you see that export templates you are in your resource group and on the left side you can go to export template and what you get here is generating a template and now what you see here this you see all this stuff there is the same as you saw here right we already extracted that you see schema current version one current version one see all everything that you see here is here as well that's the second so uh you can also copy ctrl a ctrl c and create a json file and then decompile it of course and then you do it from a graphical expro approach instead of a command line approach so that's the decompiling part um let me think if we can create perhaps some more objects in azure i will first delete all my storage account that we created because it will cost money at some point of course and i don't need them so i will remove every i just selected all the stuff here and i press on delete and it's not deleting your resource group don't worry it's only deleting the selected items there so let's let's do that now and as you can see the cleanup process for storage account is quite fast to be fair because i pressed pause for the video but it was completed in like two or three seconds i did not expect it to be that fast to be fair but it's good yeah just for the sake of the demo let's let's create another bicep file we could create a new file from scratch and we tried to create uh some another resource in azure so no i don't want to create it there i want to create it here so let's call it uh data factory i want to create a data factory okay i call it dot bicep data again here you can do resource right my data factory i call it now like that my data factory space and there's an extremely long list here but you can type for factory variancy data factory i press top to confirm this type of object of resource actually then you specify the api version i take the latest and i press space and then you do an equal sign means you want to assign something you press space again and then required properties as you can see the for data factor is only your name required let's just call it my data factory i just want to show you the process again so very simple and with json you need to specify a lot more than this i can tell you that and let's try to deploy this data factory okay we only provided this and i again i don't know if it works i'm really just testing on the fly so let's move this out of there don't need it there anymore and we do uh we don't need that and now we had uh the data factory dot bicep file right now we have that one let's try to deploy it and see what we get and apparently it did something at least so let's inspect that a little bit it took some time not that long oh it failed by the way so let's see white found deployment failed with errors stated as the location property is required for this definition okay so location required so that's interesting because we created this data part and we did the required properties right from the bicep so there's a bug there doesn't matter so apparently the location is also required so we can do the use resource group dot location trick again right you can also of course create the far location here equals and then i'm a little bit lazy i'm going to copy that over you can provide that there and then here you can just provide the word location of course right perhaps you need a location on several other spots just want to show you a little bit how it works and you can also create a param of course right a param of type string string not sting string that that does the same but we keep it for a fire now far location power location is that and we re you reuse that there and i'll save this and i run the deployment again for the data factory and i'll clear the screen here down and let's see if we get an error again let's see when we get back okay this time we got the succeeded back incremental so that's that's good so uh let's go to our resource group here refresh it and let's see refresh it again we have a data factory here already yes i have a data factor you see data factory p2 let's open this data factory and we do sort of the same tricks perhaps i even i don't know if it's even possible by the way to set text oh you can set tags here right we don't have any tags on the on the data factory let's set some text let's see if it's possible so we go back to this data factory file text i already showed you that you open and close that and we do tag one value one and it needs to be string by the way value one i'm lazy right and tag two as well so now we add some text to our data factory i save that file go back to our script and deploy this template again this data factory dot bicep template and uh should be quite quick i think only setting some tags uh i already explained right the process here arm is checking the data factory already exists so it only knows it needs to modify the the text right all the other stuff is already okay so it won't touch that it's idempotent right and it succeeded so let's go back to the data factory let's refresh that here let's refresh and let's go to text let's see yeah you see my data factory tag one value one play two okay um let's go back to this bicep file once more and uh i already showed you on this one you know you can for instance overwrite the tag right we have a tag two here and this is hard coded now if you want to make it dynamic you can do it like that and then specify the tag there right tag two and just for now we give it the default value already showed you that we can overwrite it right so if we now run this same command again we do minus tag two and uh my value okay and you run this again now only tag two will be changed and basically on external output uh input i mean this file is now accepting an external parameter to provide a different value than value to default and use that value there on the tags so let's go back and let's refresh this and there we have tag to my value so yeah honestly it's very powerful bicep right i'm trying to show some more examples perhaps yeah i just was looking through some documentation and perhaps it's interesting to share that microsoft docs.microsoft.com you have a lot of information about all these bicep templates and how to use them and how to create objects so for instance if you are interested in kubernetes perhaps you can manage cluster and then basically you get the json file right here you see json file for the whole cluster setup and if you click on the bicep here you will see a bicep file on how to create a cluster and um perhaps i'll show that also inside of an editor right so let's create a new uh file we call it aks dot bicep i don't want to do something with the aks so we want to have a resource and a maya aks right and it has something to do with aks just let's see if we can actually find something like that or kubernetes we will need this connected cluster so and then we specify the api again we do an equal sign and we press space we do the required properties again right it's very easy and then this is all required so you need to have a name a location an identity type and at least the certificate so these are the minimum requirements to get the kubernetes connected cluster up running i just wanted to show you that you can do it from an intellisense perspective as well because this file if you copy all this over this could be perhaps a little bit intimidating to start with right it's you see it's a lot and there's lots of squiggly lines and perhaps stuff you don't understand yet you can also build such a template up right it's item potent so every time you add something you just rerun it and just extend the functionality instead of starting very big so i would advise to start very small and just build it up like i did with the storage account you know we could add properties and values over time and just advance that bicep file we don't have time for that in a demo of course but in real life you will do such a scenario this data factory for instance over time it becomes more advanced and you will add stuff and perhaps even linked services and and you know you have all kinds of relations inside of adf uh access even you know identity such so you cannot add that all from a code perspective from a bicep file um so let me go back to the sort of presentation here so we exported everything we wrapped the script even so let me go one more time back to the scrappy drawing i think i explained everything should be clear now uh so yeah and you know what comes i guess last slide a big thank you of course for watching and uh have the patience to stay with me because it's uh this time it's a long video it's 40 minutes already so i really need to quit uh thanks for watching again and i hope to see you again bye
Info
Channel: IT Generalist aka Mr Automation
Views: 52
Rating: 5 out of 5
Keywords: automation, powershell, learn automation, how to automate, windows automation, powershell automation, Mr Automation, IT Generalist, configuration as code, infrastucture as code, IaC, orchestration, bicep files, arm, azure resource manager and bicep, convert json to bicep, bicep decompile, arm and bicep, bicep vs json, azure automation, azure automation with powershell
Id: VwQGr-Xvrts
Channel Id: undefined
Length: 40min 54sec (2454 seconds)
Published: Sat Sep 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.