Getting Started with PSArm

In the first part of this series, you learned about PSArm — a PowerShell embedded DSL — that you can use to declaratively define your Azure infrastructure and generate an ARM template. PSArm, being a PowerShell DSL, supports PowerShell language constructs and builds the object model for ARM templates on top of PowerShell. So, if you are already familiar with PowerShell, writing ARM templates now will be as simple as writing another PowerShell script. So, how do you get started?

Installing PSArm

You can get PSArm module from the PowerShell gallery.

You can also build module from the source code available on GitHub.

This module, when loaded, exports a bunch of functions and cmdlets.

You can see that the exported functions are similar to what ARM template language offers.

PSArm Syntax Basics

A typical PSArm script for ARM template starts with the Arm keyword. Within the Arm body, you define each Azure resource using the Resource keyword.

The Arm, Resource, and output keywords are aliases defined in the module.

With Arm keyword, you can specify an optional Name parameter which will be used as a name of the deployment within the template. For the resource definition, you use the Resource keyword. You must specify a Name to be used for the resource you want to provision, Namespace of the resource, ApiVersion, and Type. As you enter arguments for these four parameters, you will see that PowerShell dynamically adds some more parameters based on the type of resource you intend to provision.

For example, as you see in the above screenshot, as soon as I added the Type parameter and its argument, I get Kind and Tags as the dynamic parameters. You can, then, use the ArmSku keyword to specify the SKU of the resource that you intend to provision. In case of a storage account, this can be set to Standard_LRS or any other supported value. Each Azure resource may need some more additional properties for resource provisioning and configuration. You can use the properties keyword for this purpose. PSArm gives you the auto-completion of property names within the properties block.

Finally, the output keyword can be used to retrieve properties required from the deployed resource objects. This keyword takes Name, Type, and Value as parameters.

First PSArm Script

Here is a complete PSArm script for provisioning a simple storage account.

Make a note of the ResourceId and Concat functions used along with the output keyword. PSArm has public function parity with what is offered in ARM template language. You can save this script with a .psarm.ps1 extension and generate the ARM template JSON using the Publish-PSArmTemplate cmdlet that PSArm module provides.

Here is the generated ARM template JSON for the PSArm script that you just built.

This ARM template can be deployed using your favorite command line tool or using template deployment in Azure Portal.

When using Azure CLI,

When using Azure PowerShell,

This is it. Congratulations. You just used PowerShell based DSL to generate and deploy an ARM template. In the next part of this series, you will learn more parameterizing PSArm scripts.

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts