Yo arm-template! Generating ARM templates with Yeoman

A common piece of feedback I get from people who author ARM templates is that the design experience isn’t as great as it could be. There is some tooling in the full version of Visual Studio which helps get you started. But a lot of people don’t install Visual Studio as it seems overkill. After all, it’s just managing the text files that make up your typical ARM file based project. That got me thinking that surely there should be an easier way, and then the idea hit me! I set off to make a set of Yeoman generators for ARM resources. I think what I’ve come up with here will be super useful to a lot of people.

If you aren’t a typical web developer, you may not have ever heard of Yeoman. It is a tool that is useful to generate the scaffolding of a new project. For example, I could ask it to generate me a template for an ASP.NET website, and it would set up the files for me. That would include a set of personalised files based on responses to questions it asked me during the creation process. Looking at this, I thought there is no reason we can’t use this same tooling to help speed up ARM template-based projects. Let me take you through the process of setting up and using the “arm-template” generator.

Installing Yeoman

To get started you will need to install Yeoman, which needs an install of node.js as well. If you don’t already have node.js installed check out the node.js download page. When that installation completes, open a new command line window (PowerShell if you’re on windows, or bash if you’re on Linux/Mac). Run this command to install Yeoman:

npm install -g yo

That will install Yeoman globally for you to be able to use in different contexts.

Installing the arm-template generator

Once you have Yeoman installed, installing the arm-template generator is another one-line command.

npm install -g generator-arm-template

That will again install the generator globally so you can use it anywhere.

Install the Yo VS Code Extension (optional)

You can run all of the Yeoman commands from the console in VS Code or from an external PowerShell session, but if you want some of the integration shown below you’ll want to install the “Yo” extension.

Creating a new ARM template project

Now we are ready to create a new project. Start by creating a new empty folder for your files. At this point, you can either call the generator from a command line, or from within VS code. To call it from a command line simply enter the commands below into your chosen command line tool. If you want to use VS Code, the process is a little different. Begin by pressing ‘F1’ and then entering “yo” into the command line at the top of the window. From here you can select a generator and then the process is more or less the same.

Get started by creating a new ARM template project. Run the following command:

yo arm-template

In VS code the steps would be:

  1. Press F1
  2. Enter “yo”
  3. Choose “arm-template”

If you are in VS Code, you’ll miss the great ASCII art view, but Yeoman will ask the same questions.

You will be prompted to answer some questions to help generate the script. When that process completes, you will see a file structure ready to go in your folder!

At the time of me writing this post, the scripts/content here is almost identical to what you get when you choose the “Azure Resource Group” project type from Visual Studio. I do have plans to streamline this a little more compared to that project type though.

Adding resources

Now we have an empty project; we should add some resources to it. For that, we can use “sub-generators” to be able to add them for us. To get started, let’s add a storage account. Run this command to add one in:

yo arm-template:storageaccount

You will again be prompted to select a name and settings for the storage account. At this will overwrite azuredeploy.json with new content you will also need to accept this in a separate prompt. When the prompt completes, the new resource will be visible in the azuredeploy.json file.

There is a full list of the sub-generators you can use listed on the generator-arm-template npm page.

Intelligent templates

One of the things I set out to do with this project was to make the templates as smart as possible. For example, if I add a network adapter without an IP address I want it to set the property to either static or dynamic automatically. I didn’t want to only prompt the user line by line for each setting in the JSON schema. You will find as you work through the templates that elements like this make it easier to get your ARM templates right with less work. Given the way Yeoman lets me leverage custom code to build out the templates, this is something that can continue to improve as I add more features to the project.

Deploying to Azure

Now that we have a resource ready to deploy, it’s time to execute the script. For those doing this outside of VS Code, you can just call the main PowerShell script. But for those who use VS Code, I’ve included an automatic tie into the debugger. If you select the debug side menu, you can then choose how to launch the script.

Here you can choose to launch the template, or just validate it. Either way VS Code will start the appropriate PowerShell right in the window for you. A little note here though, if you need to log in to Azure (which you will the first time) the authentication window may appear behind VS code. That is due to a known issue with VS Code and child windows. If you run your script and it looks like it is hanging there doing nothing, minimise VS Code and look for this window. Once you start the script, you will see the output the same as you would in the full version of Visual Studio.

Can I customise the scripts and template myself?

The answer to this is yes! Yeoman is about generating the files and content for you initially. What you do with it after Yeoman runs is up to you. Feel free to change values, substitute variables and add dependencies as you need to.

I have feedback or want more resource types

If you have any bugs, feedback or requests to get a new resource type added, head to the GitHub repo for the project. Here you can add items to the “issues” section, and I’ll do my best to respond to them. Also if you aren’t on GitHub feel free to add comments here and I’ll help the best I can as well.

What’s next?

This project is only four days old at this point, so I’m keen to get feedback on how people use it. I’ll continue to extend it going forward as well. That should help you get started in writing your ARM templates though, so if it looks helpful please also make sure you let people know and leave me some feedback! Happy ARM authoring!

5 Replies to “Yo arm-template! Generating ARM templates with Yeoman”

  1. I got the extension installed, relaunched VS Code and am still not seeing VS code recognize anything when I type yo into the prompt after hitting F1. It does work from the terminal window in VS Code though. I’m not sure if there is another step required to get support for using F1.

Leave a Reply