IaC in Azure: ARM Templates

IaC in Azure: ARM Templates

In this post, I want to share with you 6 tips that will help speed up your ARM templates learning journey and get you from 0 to 100, say 60 real quick.

If you have not read the first post in the series, please do here.

Just to reiterate, this is not a deep dive into ARM. We'll reference the tutorial from Microsoft but you can also find great learning content here.

The tips I'm sharing are borne out of my own experience learning to code with ARM templates.

Let's go!


Quick Check

Just to be sure we are on the same page, do you:

  • Know ARM templates are how you define Azure infrastructure as code?
  • Understand the basics of data structures and algorithms?
  • Familiarised yourself with the JSON basics?
  • Use Azure portal to create a simple service such as storage account or virtual machine?

If your answers to the questions above are not all YES, then kindly refer to the first post in this series.


TIP 1: Juice Up Your IDE

To author code and programmatically interact with Azure resources, you need to use an IDE, also known as a code editor and Microsoft Visual Studio Code (VS Code) is your ideal code editor to get started with.

If you want to accelerate your learning, you need to use a VS Code feature called extensions. Extensions 'extend' the functionality of your code editor and provide tools to help make your life easier.

Let's have a look at 2 of these extensions that you need to install NOW:

Azure Account: As you may be aware, every time you need to interact with the Azure resource manager via CLI or API, you need to be authenticated and authorized. This is why you provide credentials first when using the portal or run 'az login' when using Azure CLI etc. Same applies to your code editor and this extension just makes it as easy as a, b, c.

Azure Resource Manager (ARM) Tools: This extension provides you with resource snippets, autocompletion and many more. This comes in handy because if you are like me, writing JSON from scratch is not fun. This extension is a life saver, take my word for it!

Action:

Launch VS Code, go to extensions and search for these 2 listed. Ensure you read the details in the extension page to familiarise yourself with how to use.

TIP 2: Use MICROSOFT LEARN But With a Twist.

Microsoft documentation and tutorial on ARM templates is a fantastic place to start from.

However, I discovered that when I follow the tutorials but use a different example, I understand the content faster. Let me explain.

In the Microsoft tutorials, you will be shown the steps use ARM template to create a storage account. Instead of doing that, choose another service to create, let's say a virtual network(VNET) or a web app.

Whatever it is, start simple and follow the tutorial steps. Using this approach stops you from just copying and pasting code already provided, a harder path but learning can be exponentially accelerated.

Action:

  • Select a simple service to deploy other than storage account e.g. VNET, Webapp etc.
  • Visit Microsoft learn tutorial for ARM templates, follow the steps but use to create the service you selected earlier.

TIP 3: BYOA

Bring Your Own Architecture: Go beyond just creating a single resource, draw up a target architecture and set that as your goal.

Imagine a web service architecture : 3 VMs across multiple availability zones running Ubuntu OS + Apache, with diagnostics enabled to separate blob storage per VM and an application gateway to serve web traffic to the VMs.

What I described above is a TARGET architecture. I would typically go through the following iterations over time, till I hit my goal:

  • Single instance deployment of just one of any of the resources in my target architecture e.g. one instance of a storage account.
  • Single instance deployment of a combination of multiple resources e.g. one instance of a VM, VNET and storage account.
  • Multiple instance deployments e.g. three instances of a VM in addition to a VNET and storage account.

Action: Set a big goal but start small.

TIP 4: Break It Down

As I mentioned in the first post in this series, IaC will improve your understanding of cloud resources/objects and the relationships between these resources such as dependencies required to achieve your desired solution.

As a beginner, it is essential to go through a break down exercise before starting to write code.

Let me explain with this example : You are required to provision a virtual machine instance, running Ubuntu 20 and you must enable VM diagnostics and store this in blob storage.

A resource group is always required, which region do you want to create it in? For the storage account, best to use a blob? If yes, does it reside in a VNET? No, so what is needed? Tier? SKU? Region? Virtual Machine? Needs a NIC which needs a private IP address and an IP address can only exist in a subnet but I can't create a subnet without a VNET.

You get the idea?

Knowing the difference between resources and properties is also key. For example, you might be tempted to think a public IP(PIP) address is a property of network interface card resource but it's not. PIP is a resource on its own. How can you be on top of this? Tip 5 shows us the way.

TIP 5: ARM Template Documentation is Life!

When I started with ARM, I watched experts and instructors conjure up ARM template code like it was nothing.

"How did he know to add this property here?"

"oh! that's a string, not an integer?"

The answers are all in the template reference documentation, get into it, a lot!

It tells you all need to know about the resource(s) you want to code up. Tells you what the properties are, the hierarchy and the data types. Also keep an eye on what's required and what's not. Absolute life saver if you are new to this.

Let's say you've completed your resource breakdown from Tip 4 and you want to flesh out the public ip address code block. Go to the template reference webpage, search for public IP address then pick and choose, that's all.

Action:

Bookmark the template reference webpage.

TIP 6: Use the Cheat Code

ARM is not simple.

You might not get things right at first and it's OK, it's part of the learning process.

One of the best things about Azure is that when you create a resource(s) using the portal, Azure generates an ARM template for the resources that you can use for automation. If you are having trouble with your ARM template, attempt to create the resource using Azure portal and just before the final button to create the resources, there is a link to download the ARM template, click it!

Download the template and compare with yours. This should help you identify errors in your code and possible how to structure and improve your code.

I am not a fan of ARM templates. I feel it's just complex but it is a necessity at present if you work at a Microsoft shop and you want to get into infrastructure as code.

Microsoft has been working on another language called BICEP (ARM <> BICEP, get it?) and trust me, you need to experience the horror of ARM templates before trying BICEP out, it's the only to experience how exciting it is.

Bicep is a Domain Specific Language (DSL) for deploying Azure resources declaratively. It aims to drastically simplify the authoring experience with a cleaner syntax, improved type safety, and better support for modularity and code re-use.

We'll get into BICEP in the next post of this series.

Thanks for reading!
Please hit me up on Twitter if this helps you or you want to chat about this post. See ya!

Show Comments