hi welcome in another video of cloud in five minutes today I want to talk about
the azure resource manager arm template you heard about it but you
don't know how to get started today is your lucky day because I will share with
you my 5 tips to get a clean arm template
every time aussi si vous preferez, j'ai une version en francais de ce video. Cliquer juste ici dans le coin en haut a droite
hello again let me introduce myself I'm Frank Boucher a Microsoft Azure MVP
based in Montreal in Canada today I will be talking about armed template
those are G's and 5 you could be building them from scratch with your
favorite text editor but if you're just getting started building your script
from scratch could be a little bit hard so what you could do is go in the azure
portal create your deployment by using the UI interface but then before
creating the create button just look on the side the download template and
parameters click on that link it will open a new blade with everything you
need the parameter files and the arm template just click the download button
and you're good to go if your solution is already running in Azure you can go
in the azure portal open the resource group blade and then on the Left panel
look for the automation script click on that and you will be able to download
your script just like that in today's demo what I want to do is use the arm
template that was generated when we were creating a VM and clean it and apply the
best practices let's get started ok so I download the code generated from
the azure portal and unzip it in a folder and I just I will be working in
Visual Studio code I like it you could use whatever tool you like you like I
really like usual pseudocode to play with arm template in the zip file we
download of course we had the template and the parameter file but we have also
bunch of deployment script so in PowerShell bash Ruby and c-sharp that
could help us deploying this template but today we won't play with those we
only focus on the parameter files and the template files step number one use
git even if it's only locally use skip that will give you an infinite control Z
option you will always be able to return to a point where
your scrip was working and we'll keep everything clean use get so since I'm in
Visual Studio code it's super hazy for me and just go in the source control tab
and I click this button here and initialize my repo and I can do my first
comment step number two validate validate validate when your validation
pass then you commit and you validate often to validate today I will use Azure
CLI you could be using Azure PowerShell also if you like the command to validate
is as a group deployment validate and you pass a resource group if you don't
have one it's time to create one you can use AZ group create and then you pass
the name and the location of your user group then for the validation we pass
the template file and the parameter file we hit enter take just few second and
when we get a long script like that is the arm template populated with every
real resource name that will be used as resource ID means it's good if it was an
error she would have been super short so perfect step number three nobody likes
an iron template with 51 parameters so step 3 reduce the number of parent heirs
as much as possible we cannot just delete them of course we need to move
them to the variables and then play from there the way I do that is I go back in
the parameter files and I bubble up every parameter that I want to keep so
far in this sample I will keep only four so location virtual machine name and
middle name and I'm and password everything else I want to remove it so I
will just select everything that I want to remove remove it remove the last
comma so that will be my new parameter file and I paste that in
document just like this so what's nice in Visual Studio code is I have a
multi-select cursor so for example if I click here and then I hit the ctrl F 2 I
now have a multi selection and I can just continue and select everything
until that if I delete voila and now I could do the same thing in here ctrl F 2
and what I have now everything that is ready to be used in my template file use
that and paste it in my template in my template I will go into section
variables so here or keep just an empty line make it clear that those are new
perfect and now I can remove all the parameter I'm not using anymore just
like that perfect however now in my file it's using parameter that doesn't exist
so I need to change every time I was using a parameter in a name that I
changed it to our variables I need to change it for variables here so you just
need to pass and do that every time
once you think you're done cleaning the parameters now it's time to validate so
don't forget to save and go save your parameter files also and now it's time
to validate perfect it worked so now we commit a little tips here if you're
using a template that was generating from a solution that is already running
in Azure you will have a little bit of noise a lot of information in fact
regarding what's already running so in the arm template you will have like the
full IDs and the states of all the resources you will need to clean that
all so for example in here I have comments so don't hesitate you can
remove that you won't need it also for multiple resources you will have
provisioning state and unique identifiers you don't need those all so
don't hesitate and remove them it will help you a lot to have a clean arm
template so step 4 use a unique string suffix or a unique identifier to add to
your deployment why because if you're trying to deploy the same VM over and
over you will get some problem because some names may already be used so a good
way to do that is use a unique identifier and had it to your names are
your resources for example you could add a suffix just like like that and that
suffix is use the function unique string that will build the hash based on the
parameter you pass and right now I'm what I'm using as a parameter are the
resource group ID and the location by doing so if you're deploying in another
location the ash will be also different now for example if I want to use that in
the virtual machine name what I want to do is we'll create a new variable called
VM name and I will concatenate the parameter virtual machine and the
variable suffix now what I want to do is I want to change
we're everywhere was parameter a virtual name was used and instead use the
variable VM name just like that and of course in this first one I don't want to
change it so we'll skip it and I'll do the others so now you guys sit let's
validate and it pass so before going to the next step we commit so
step 5 use the variables of course it's in this step that we will spend a lot of
time because we want to remove all the static value and put something more
dynamic for example if I look here to my virtual network name I use cloud five
minutes in the name because it was the name of my result group that's why the
azure portal generated like that but if I'm deploying in another resource group
I don't want that name to be there so I want something a little bit more dynamic
so let's replace that so we'll create another variable just under so you could
see it and I will be concatenating the variable resource group name with
meaning why I use the resource group name while I have the object and I could
just point to it well I will explain so first let's create that variable so
over to a group name here and I put a two lower because sometime when we
create so root name we are allowed to have upper case and lower case so it's
great it's easy to read but for some value having upper case won't work so
that's why I do that to minimize the issue another example here will be my
network interface name where the name of the VM is used I don't want to use that
name maybe the name of the VM I'm creating right now is different someone
something dynamic so instead I could be using something like that so again to
lower I now will concatenate the virtual machine name that I have output Nick as
kind of a roll so will help me to identify and the
suffix another example here will be that diagnostic storage account name if I'm
trying to use the same pattern it will be too long because the name must be
shorter than 24 character so what I can do is use the same pattern but use the
function substring but only keep the 24 first characters and you just need to
continue like that parameter parameter and try to remove everything that is
static or too precise for this specific instance and step number six because I
have something more for you so use t-shirt size what it means is you're
using something simple for complex stuff for example as a developer I don't
really know what kind of size of a machine I need is it standard is it epic
is it like I don't know like it's too much information for me what I know is
what I I will be doing on it so the idea is to create a parameter where you will
pass a word that means something for the developer or the person that will be
consuming and deploying your um template and inside the arm template you
translate that in something more specific for example here I create a new
parameter called environment size and it's a string and the default value is
medium and I allow only a certain type of value so right now it's only two it
could be large and it could be medium no small no they have one small machine and
of course to keep the best practices I pass metadata so now what I want to do
is create a variable that leverage that information so instead of virtual
machine size I won't need that one anymore so how long you need it and I
will paste that one so t-shirt size now I have to object in it have the object
medium and have the object large those objected come
a much detailed information and one specific parameter could have a lot of
values in it so I could reuse that and adds up a lot of impact so right now I'm
just specifying two different size of VM so now I just need to use it so I'll go
in my virtual machine when it's created and I will change this variable value by
a new one and it will be my variable t-shirt size with the parameter
environment size so then I will be navigating so I will be t-shirt size dot
parameter environment dot VM size and that way I will be using the good value
to be sure the best way is to validate the template probably be on the top so I
search here I see the VM size it's perfect it's exactly what I was
expecting I didn't pass any new parameter so it used the default value
if I pass a parameter large then it will use the good one
if I pass small or extra-large this value is not supported so the person
will have an error and the arm template won't get executed I hope it will help
you allowed me a lot when I'm doing arm template development if you have
questions or any suggestion about the best practices that we should be using
write it down in the comment I hope that you like this video and I hope it was
useful see you next time
you