Learn Azure Administration
上QQ阅读APP看书,第一时间看更新

Writing a template from scratch

Conceptually, the easiest way to generate an ARM template is to write our very own. While, initially, it is pretty easy, you will probably reconsider your approach after a while. When your infrastructure contains tens of components, maintaining a JSON file with over 1,000 lines may be really painful. Nonetheless, this is one of the options and we should cover it so that you have the full picture of all the available options.

In the following link, you can view all the resources that are managed by ARM and their references: https://docs.microsoft.com/en-us/azure/templates/microsoft.aad/allversions. Each resource belongs to a particular namespace (so if you search for Storage Account, you will go to the Storage section). ARM also maintains more than one version of its API, which is why you can select from multiple available APIs.

Since we are about to create a brand-new piece of storage, it seems like a good idea to use the most recent version. The minimal (containing only required fields) version of the storage account resource for version 2019-06-01 looks like this:

{
  "name": "<string>",
  "type": "Microsoft.Storage/storageAccounts",
  "apiVersion": "2019-04-01",
  "sku": {
    "name": "Standard_LRS"
  },
  "kind": "Storage",
  "location": "<string>",
"properties" {} }

Now, let's check what the full template containing the aforementioned storage accounts would look like. Go and check it out at https://gist.github.com/kamil-mrzyglod/7b868e6a892cba008b7a909a1baabf43.

In the presented template, the second storage account is linked to the first one with the dependsOn property. This property is used to define identifiers of resources that have to be deployed before this particular one. This is especially helpful when deploying resources that rely on connection strings or other parameters that have to be injected into a configuration.

There is no guarantee regarding the order of deployments performed by Azure Resource Manager. This is why you should always use the dependsOn  property to avoid conflicts during deployments.

In the preceding example, we used a special function named resourceId. It takes two parameters – the namespace and the resource name. Once it has been evaluated, it returns the full resource identifier, which uniquely defines the relationship.

This section should have helped you understand some of the common concepts of writing ARM templates such as structure, syntax, and use cases. However, writing a template from scratch is not always necessary. The next section will show you how you can export a template with a single click.