DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Deploying Resource Groups with ARM Templates

Take a look here at how to take advantage of the new feature introduced by Microsoft that will allow you to deploy your entire resource group in one go.

Sam Cogan user avatar by
Sam Cogan
CORE ·
Aug. 30, 18 · Tutorial
Like (1)
Save
Tweet
Share
9.42K Views

Join the DZone community and get the full member experience.

Join For Free

Ever since they were released, ARM templates required you to supply the name of the Resource Group you want to deploy to as part the deployment command. This restriction meant that the Resource Group always needed to exist before running your deployment. I mentioned in my article on Terraform that one of the advantages of this is that you can create the resource group as part of your deployment template, no need to create it separately.

After a recent update, it is now finally possible to create resource groups inside ARM templates and to use them for deploying other resources. However, the process to do this is quite as seamless as you might think, so in this article, we'll explore how that works.

All the ARM templates in this article can be found on Github here.

Pre-Requisites

Deploying Resource Groups is a new feature and requires new commands to deploy, so make sure you have the latest version of either the Azure PowerShell commands or Azure CLI.

Deploying Resource Groups

Define Resource Groups in ARM

This update adds a new resource of type "Microsoft.Resources/resourceGroups" to the ARM template spec. Creating a Resource Group is as simple as using this and providing a name and a location to create the group.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "rgName": {
            "type": "string"
        },
        "rgLocation": {
            "type": "string"
        }
    },
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2018-05-01",
            "location": "[parameters('rgLocation')]",
            "name": "[parameters('rgName')]",
            "properties": {}
        }
    ],
    "outputs": {}
}

Deployment

The commands to deploy an ARM template ( new-azureRMResourceGroupDeployment  or  az group deploy ) both require you to provide a Resource Group name to deploy to, which does not make sense here. Instead, we have a new command for undertaking subscription level deployments —  new-AzureRMDeploymentor  az deployment. These commands are not just for deploying Resource Groups; they are used for any subscription-level resource deployment. These subscription level resources also include Azure Policies, Role Based Access at the subscription level, and Azure Security Center. See here for more details on subscription level deployments.

To deploy our the template above we would run:

PowerShell

New-AzureRmDeployment -Name "rg1" -Location "West Europe" -TemplateParameterFile .\template.parameters.json -TemplateFile .\template.json 

CLI

az deployment create --name "rg1" --location "West Europe" --template-file template.json --parameters template.parameters.json 

Using the Resource Group

So far deployment has been pretty simple, and if all you want to do is deploy a resource group, then you're done. However, I suspect most people are going to want to deploy a Resource Group and then deploy some resources into it, and this is where it gets a bit more complicated.

Unlike subscription level resources, most Azure resources need to be deployed into a Resource Group. Up until now the Resource Group to deploy to has been provided as part of the deployment command, and everything in the template uses that Resource Group (with a few exceptions). There is not a way to pass a Resource Group to resources inside the template, and Microsoft has not retrofitted one for this updated. To be able to do what we want we need to use the concept of nested templates.

We've looked at nested templates before, it provides a way to call one template from inside another, either as an inline template inside the same file, or call separate files. When you use a nested template, you do define the resource group to us in that template, and so this provides a way for resources to use the Resource Group we just created. In the example below, we are going to deploy a storage account into the Resource Group we create. We use an inline nested template and pass the Resource Group in, as well as having a dependency on the Resource Group to ensure it is created first.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "rgName": {
            "type": "string"
        },
        "rgLocation": {
            "type": "string"
        },
        "storageAccountName": {
            "type": "string"
        }
    },
    "variables": {},
    "resources": [

        {
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2018-05-01",
            "location": "[parameters('rgLocation')]",
            "name": "[parameters('rgName')]",
            "properties": {}
        },
        {
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2017-05-10",
            "name": "storageDeployment",
            "resourceGroup": "[parameters('rgName')]",
            "dependsOn": [
                "[resourceId('Microsoft.Resources/resourceGroups/', parameters('rgName'))]"
            ],
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "parameters": {},
                    "variables": {},
                    "resources": [
                        {
                            "type": "Microsoft.Storage/storageAccounts",
                            "apiVersion": "2017-10-01",
                            "name": "[parameters('storageAccountName')]",
                            "location": "[parameters('rgLocation')]",
                            "kind": "StorageV2",
                            "sku": {
                                "name": "Standard_LRS"
                            }
                        }
                    ],
                    "outputs": {}
                }
            }
        }
    ],
    "outputs": {}
}

When we run this deployment from scratch, we get a newly-created Resource Group, with a Storage account inside.

In reality, if you had complex templates, you would likely have the nested template be a call to another file, rather than doing this inline. If you want more details on how to use nested templates have a look at my article on modularisation of ARM templates.

Summary

We now finally have a way to deploy all our Azure resources in one go, including the Resource Group, which is great. The way it works is a little disappointing, I would have preferred an update to allow specifying a Resource Group on a resource, rather than having to use nested templates, but it works.

All the ARM templates in this article can be found on Github here.

Image Attribution

Data Center flickr photo by Bob Mical Ⓥ shared under a Creative Commons (BY-NC) license.

Template Arm (geography)

Published at DZone with permission of Sam Cogan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Best Practices for Writing Clean and Maintainable Code
  • Integrating AWS Secrets Manager With Spring Boot
  • Use Golang for Data Processing With Amazon Kinesis and AWS Lambda
  • What To Know Before Implementing IIoT

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: