Azure Resource Manager Templates Quick Start Guide
上QQ阅读APP看书,第一时间看更新

Writing your first template

Now it's time to focus and create our first ARM template using Visual Studio 2017. Open Visual Studio 2017 and select File | New | Project | Cloud | Azure Resource Group. Provide a Name and Location in New Project dialog box and click on OK:

Select Blank Template and click on OK:

This should create a solution and the MyFirstTemplate project in Visual Studio. It will also create an ARM template file named azureDeploy.json and a template parameters file named azureDeploy.parameters.json. There is also a PowerShell file, Deploy-AzureResourceGroup.ps1, for both creating a resource group and deploying a template in it. We are not going to use it. Readers can go ahead and delete it:

The content of the azuredeploy.json file is the same as shown before; for simplicity's sake, it is shown again:

{

"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",

"contentVersion": "1.0.0.0",
"parameters": {},
"variables": {},
"resources": [],
"outputs": {}

}

If readers try to deploy this template, it will successfully get deployed, however, it would not do anything meaningful at this stage.

Let's add a parameter, variable, resource, and an output to this template so it does something meaningful. It is possible that readers might not be able to understand many sections within this ARM template, but let me assure you that by the time you finish reading Chapter 5IaaS Solutions Using Templates, everything will become crystal clear to you.

This template is responsible for provisioning a new storage account on Azure. The name of the storage account is available as a parameter, the SKU of the storage account is defined in the variables section, and the output consists of the storage name concatenated with the storage SKU.

The entire code listing is shown here:

Let's take a look at each of these elements:

  • Line 2 declares the schema element.
  • Line 3 defines the version of the template.
  • Lines 4—8 define a single parameter named storageAccountName of the string type.
  • Lines 9—11 define a single variable named storageType with the Standard_LRS value.
  • Lines 12—23 define a single storage account resource within the resources array. The name for the resource gets its value from the parameters and syntax. [parameters('storageAccountName')] is an expression that is evaluated at runtime to get a value from the storageAccountName parameter. Similarly, the [variables('storageType')] syntax on line 19 is an expression that is evaluated at runtime to get a value from the strorageType variable. Line 17 has an expression that consists of an out-of-the-box ARM template function, resourceGroup, with the location as its property. It returns the Azure location for the resource group within which the resource is getting provisioned. ARM template expressions and functions will be covered in Chapter 3, Understanding Core Elements of ARM Templates.
  • Lines 24—29 define the outputs section and it consist of single output named storageDetails of the string type. The value is generated at runtime using the concat function, which concatenates the storageAccountName parameter and the strorageType variable together.