Create Service Bus resources using Azure Resource Manager templates

This article describes how to create and deploy Service Bus resources using Azure Resource Manager templates, PowerShell, and the Service Bus resource provider.

Azure Resource Manager templates help you define the resources to deploy for a solution, and to specify parameters and variables that enable you to input values for different environments. The template is written in JSON and consists of expressions that you can use to construct values for your deployment. For detailed information about writing Azure Resource Manager templates, and a discussion of the template format, see structure and syntax of Azure Resource Manager templates.

Note

The examples in this article show how to use Azure Resource Manager to create a Service Bus namespace and messaging entity (queue). For other template examples, visit the Azure Quickstart Templates gallery and search for Service Bus.

Note

This article has been updated to use the new Azure PowerShell Az module. You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. For Az module installation instructions, see Install Azure PowerShell.

Service Bus Resource Manager templates

These Service Bus Azure Resource Manager templates are available for download and deployment. Click the following links for details about each one, with links to the templates on GitHub:

Deploy with PowerShell

The following procedure describes how to use PowerShell to deploy an Azure Resource Manager template that creates a Standard tier Service Bus namespace, and a queue within that namespace. This example is based on the Create a Service Bus namespace with queue template. The approximate workflow is as follows:

  1. Install PowerShell.
  2. Create the template and (optionally) a parameter file.
  3. In PowerShell, log in to your Azure account.
  4. Create a new resource group if one does not exist.
  5. Test the deployment.
  6. If desired, set the deployment mode.
  7. Deploy the template.

For complete information about deploying Azure Resource Manager templates, see Deploy resources with Azure Resource Manager templates.

Install PowerShell

Install Azure PowerShell by following the instructions in Getting started with Azure PowerShell.

Create a template

Clone the repository or copy the 201-servicebus-create-queue template from GitHub:

{
  "$schema": "https://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "serviceBusNamespaceName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Service Bus namespace"
      }
    },
    "serviceBusQueueName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Queue"
      }
    }
  },
  "variables": {
    "defaultSASKeyName": "RootManageSharedAccessKey",
    "authRuleResourceId": "[resourceId('Microsoft.ServiceBus/namespaces/authorizationRules', parameters('serviceBusNamespaceName'), variables('defaultSASKeyName'))]",
	"sbVersion": "2017-04-01"
  },
  "resources": [
    {
      "apiVersion": "2017-04-01",
      "name": "[parameters('serviceBusNamespaceName')]",
      "type": "Microsoft.ServiceBus/Namespaces",
      "location": "[resourceGroup().location]",
      "sku": {
        "name": "Standard"
      },
      "properties": {},
      "resources": [
        {
          "apiVersion": "2017-04-01",
          "name": "[parameters('serviceBusQueueName')]",
          "type": "Queues",
          "dependsOn": [
            "[concat('Microsoft.ServiceBus/namespaces/', parameters('serviceBusNamespaceName'))]"
          ],
          "properties": {
            "lockDuration": "PT5M",
            "maxSizeInMegabytes": "1024",
            "requiresDuplicateDetection": "false",
            "requiresSession": "false",
            "defaultMessageTimeToLive": "P10675199DT2H48M5.4775807S",
            "deadLetteringOnMessageExpiration": "false",
            "duplicateDetectionHistoryTimeWindow": "PT10M",
            "maxDeliveryCount": "10",
            "autoDeleteOnIdle": "P10675199DT2H48M5.4775807S",
            "enablePartitioning": "false",
            "enableExpress": "false"
          }
        }
      ]
    }
  ],
  "outputs": {
    "NamespaceConnectionString": {
      "type": "string",
      "value": "[listkeys(variables('authRuleResourceId'), variables('sbVersion')).primaryConnectionString]"
    },
    "SharedAccessPolicyPrimaryKey": {
      "type": "string",
      "value": "[listkeys(variables('authRuleResourceId'), variables('sbVersion')).primaryKey]"
    }
  }
}

Create a parameters file (optional)

To use an optional parameters file, copy the 201-servicebus-create-queue file. Replace the value of serviceBusNamespaceName with the name of the Service Bus namespace you want to create in this deployment, and replace the value of serviceBusQueueName with the name of the queue you want to create.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "serviceBusNamespaceName": {
            "value": "<myNamespaceName>"
        },
        "serviceBusQueueName": {
            "value": "<myQueueName>"
        },
        "serviceBusApiVersion": {
            "value": "2017-04-01"
        }
    }
}

For more information, see the Parameters article.

Log in to Azure and set the Azure subscription

From a PowerShell prompt, run the following command:

Connect-AzAccount

You are prompted to log on to your Azure account. After logging on, run the following command to view your available subscriptions:

Get-AzSubscription

This command returns a list of available Azure subscriptions. Choose a subscription for the current session by running the following command. Replace <YourSubscriptionId> with the GUID for the Azure subscription you want to use:

Set-AzContext -SubscriptionID <YourSubscriptionId>

Set the resource group

If you do not have an existing resource group, create a new resource group with the New-AzResourceGroup command. Provide the name of the resource group and location you want to use. For example:

New-AzResourceGroup -Name MyDemoRG -Location "West US"

If successful, a summary of the new resource group is displayed.

ResourceGroupName : MyDemoRG
Location          : westus
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/<GUID>/resourceGroups/MyDemoRG

Test the deployment

Validate your deployment by running the Test-AzResourceGroupDeployment cmdlet. When testing the deployment, provide parameters exactly as you would when executing the deployment.

Test-AzResourceGroupDeployment -ResourceGroupName MyDemoRG -TemplateFile <path to template file>\azuredeploy.json

Create the deployment

To create the new deployment, run the New-AzResourceGroupDeployment cmdlet, and provide the necessary parameters when prompted. The parameters include a name for your deployment, the name of your resource group, and the path or URL to the template file. If the Mode parameter is not specified, the default value of Incremental is used. For more information, see Incremental and complete deployments.

The following command prompts you for the three parameters in the PowerShell window:

New-AzResourceGroupDeployment -Name MyDemoDeployment -ResourceGroupName MyDemoRG -TemplateFile <path to template file>\azuredeploy.json

To specify a parameters file instead, use the following command:

New-AzResourceGroupDeployment -Name MyDemoDeployment -ResourceGroupName MyDemoRG -TemplateFile <path to template file>\azuredeploy.json -TemplateParameterFile <path to parameters file>\azuredeploy.parameters.json

You can also use inline parameters when you run the deployment cmdlet. The command is as follows:

New-AzResourceGroupDeployment -Name MyDemoDeployment -ResourceGroupName MyDemoRG -TemplateFile <path to template file>\azuredeploy.json -parameterName "parameterValue"

To run a complete deployment, set the Mode parameter to Complete:

New-AzResourceGroupDeployment -Name MyDemoDeployment -Mode Complete -ResourceGroupName MyDemoRG -TemplateFile <path to template file>\azuredeploy.json

Verify the deployment

If the resources are deployed successfully, a summary of the deployment is displayed in the PowerShell window:

DeploymentName    : MyDemoDeployment
ResourceGroupName : MyDemoRG
ProvisioningState : Succeeded
Timestamp         : 4/19/2017 10:38:30 PM
Mode              : Incremental
TemplateLink      :
Parameters        :
                    Name             Type                       Value
                    ===============  =========================  ==========
                    serviceBusNamespaceName  String             <namespaceName>
                    serviceBusQueueName  String                 <queueName>
                    serviceBusApiVersion  String                2017-04-01

Next steps

You've now seen the basic workflow and commands for deploying an Azure Resource Manager template. For more detailed information, visit the following links: