An Intricate Look at ARM Templates - Part 3
Building Your First Template With Visual Studio Community
Hello Skylines Academy students! My apologies for the long delay in between blog posts. Life became a little bit busier than I anticipated these last few months. I hope to release the next series of blog posts in quick succession. So, without further ado, I bring you the next installment of my ARM Templates blog series on “Building your First Template with Visual Studio Community.”
Within my first and second post, I laid the foundation to build upon as you get yourself up to speed on how ARM Templates can be plugged into Infrastructure as Code (IaC) workflows or infrastructure buildouts, I walked you through each section of an ARM Template, and detailed out the types of configurations you
may find as you start building ARM Templates. For this post, we will finally build what might be your first ARM Template in full!
So, launch Visual Studio Community and let’s get started!
When Visual Studio Community launches, you may see a screen like this:
Click on “Create a new project.”
From there, when you create the new project, you should see this show up:
Be sure to filter “All platforms” to “Azure” with the drop-down menu. From there, select “Azure
Resource Group” and then “Next.”
After that screen, you will need to give your project a name, save it locally, and leave everything else as
default. Once you fill everything in, click “Create.”
The next window will pop up:
For purposes of this first template, leave “Show templates from this location:” on “Visual Studio
Templates” and pick “Blank Template. From there, click “Ok.”
Once Visual Studio finishes preparing the solution, you should see the following screen:
Take note of the Solution Explorer on the right. You should see an “azuredeploy.json” file, an
“azuredeploy.parameters.json” file, and a “Deploy-AzureResourceGroup.ps1” file. To get a handle of ARM Templates in general, we will not focus on “References” for this installment of the blog. For purposes of understanding all these new files that were created, please refer to some of my notes about these files
below.
azuredeploy.json – This is your main template file. All resources, parameters, variables, and outputs will live here during any sort of configuration pattern (testing, new, full, or incremental changes). Think of this like your resume. Resumes do not typically change often (unless you are applying for a completely different job).
azuredeploy.parameters.json – This is your parameters file that works in conjunction with your main template file. All parameters should be called out here for anything that may change from build to build (i.e. VM size, VM name, etc.). Think of this as your cover letter. Cover letters change dependent upon the type of job being applied for.
Deploy-AzureResourceGroup.ps1 – When you select “Azure Resource Group” (like we did
a few steps back), you get this PowerShell script by default. If you are curious to understand the syntax, open it up and examine what the script is doing. When you look at the file, it’s doing a lot related to deploying the
azuredeploy.json and azuredeploy.parameters.json file in your local directory, along with adding any artifacts that may be in the same local directory (hold on to that thought – we’ll pick it up later). It is a fairly expansive script.
Double click on the azuredeploy.json and examine the file. You should see something like this:
Take note of the JSON Outline (that view will be helpful to bounce around the file as your templates grow),
the open azuredeploy.json file, and the Solution Explorer, where you can switch between files. If you examine the actual file, you should see nothing is filled in, however the blank template contains a section for parameters, variables, resources, and outputs (which matches up to what my second blog post covered).
Locate “Resources” in the JSON Outline. Right click on “Resources” and select “Add New Resource.”
Type in “Virtual Network,” select “Virtual Network,” type in a Virtual Network Name, and click “Add.”
Notice how a few things start populating in your main template file. You should have 5 variables and 1
resource listed within the JSON Outline view:
In addition, you should see some ARM Template code within the main template:
Notice the yellow line on the far left. That means you have unsaved changes within your file. If you examine
this code, it should line up to the sections I covered earlier. For purposes of making this first deployment easy, let’s save the file and deploy. When you save the file, the line will turn from yellow to green. Notice how there are not any parameters within this first deployment, as all the different components to adjust have landed in the variables section.
Once the file is saved, right click on the project name within Solution Explorer:
Then select “Deploy” and from there, select “New.”
A dialogue box will pop-up that looks like this:
Ensure your account is logged in at the top and that you are deploying to the right subscription. When you launch Visual Studio Community, it should prompt you to log in, but in the event it does not, you are able to log in at this point to Azure. Note, you can log into multiple accounts, but you will have to set that up each time you deploy in this fashion.
Let’s create a brand-new Resource Group. Click on the downward triangle on the Resource Group box and
select “Create New…”
Type in a Resource Group name and select “Create.”
After the resource group creates using Visual Studio Community, switch to the portal and locate the new
resource group you created. Isn’t it nifty that a Resource Group creates WITHOUT needing to be in the portal?
Flip back to Visual Studio Community. You should be able to see the dialogue prompt pick the new Resource
Group you created and you should see it pull in the azuredeploy.json + azuredeploy.parameters.json file. Visual Studio Community is pulling this in from the local directory on your machine, so it is 1 less step you need to worry about when deploying ARM Templates via Visual Studio Community.
Click on “Edit Parameters.” Note, you do not have any parameters within this specific deployment, but you
can edit parameters within ARM Templates from this view to give you full control over ARM Templates idempotent nature.
I will cover how to use the parameters file within a later post. Also, make sure to bookmark this blog
post for a later date (written by a teammate of mine and he demystifies when to use a variable vs. a parameter within your ARM Templates).
Once you have examined “Edit Parameters…”, click on “Deploy.”
Upon deployment, the “Deploy-AzureResourceGroup.ps1” gets called upon and you should see verbose information displayed surrounding the deployment within an Output window. The Output window will pop up and you can expand it so the window layers over the azuredeploy.json file.
Since this deployment is 1 resource and quite small, the entire output should look something like this:
Notice how the verbose flag indicates the template was deployed successfully. Once you see that at the
bottom, flip over to the Azure Portal and examine the resource you just created using Visual Studio Community:
The VNet contains 2 subnets:
Which show up in the variable section of the ARM Template you just created:
Now look at the actual resource. Examine how the virtual network gets the VNet name, the CIDR prefix, and the subnet names.
Lastly, make sure to save this template, as we will build upon it for the next blog post. Please take GREAT pride in creating your first ARM Template (with some help). The purpose of showcasing a build like this is to illustrate all of the tools at your disposal. Stay tuned for the next few blog posts where we will dig in even deeper with ARM Templates.
As always, feel free to reach out via email, LinkedIn, or Twitter if you have any additional questions about this blog post or if you have any feedback about future blog posts. Congrats on your first ARM Template!
—Shannon Kuehn