Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get connection strings in ARM

I'm creating an Azure Resource Manager template that instantiates multiple resources.

I'd like to be able to capture the primary connection strings of Redis , AzureWebJobsDashboard, AzureWebJobsStorage and AzureWebJobsServiceBus.

like image 984
zapoo Avatar asked May 15 '17 09:05

zapoo


1 Answers

According to your description, I suggest you could use the ARM Template Function ListKeys to get the Keys. And we could use the following template code to set the connection string.

Here is a demo I capture the Redis, Storage, Service Bus connectionstring and add it to the web application settings.

Since AzureWebJobsDashboard, AzureWebJobsStorage is storage connection string, AzureWebJobsServiceBus is service Bus root manager connection string.

So in my template, I directly get the connection string according to the storage and service bus name.

1.Create Basic Azure Resource Group project with template WebApp

2.From demo remove the unnecessary resource.

3.Add the connection string setting

"resources": [         {           "name": "connectionstrings",           "type": "config",           "apiVersion": "2015-08-01",           "dependsOn": [             "[concat('Microsoft.Web/sites/', variables('webSiteName'))]"           ],           "tags": {             "displayName": "tomConnectionString"           },           "properties": {              "storage": {               "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('storageAccountName'),';AccountKey=',concat(listKeys(variables('storageAccountId'),'2015-05-01-preview').key1))]",               "type": "Custom"             },              "Redis": {               "value": "[listKeys(resourceId('Microsoft.Cache/Redis', variables('RedisName')), '2016-04-01').primaryKey]",               "type": "Custom"             },              "ServiceBus": {               "value": "[listKeys(resourceId('Microsoft.ServiceBus/namespaces/AuthorizationRules',parameters('serviceBusNamespace'),'RootManageSharedAccessKey'),'2015-08-01').primaryConnectionString]",               "type": "Custom"             }            }         }       ] 

4.Add the corresponding parameters or variables such as storage info or service bus name.

5.Deploy the template

The result is as below:

enter image description here

Full template code:

{   "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",   "contentVersion": "1.0.0.0",   "parameters": {     "hostingPlanName": {       "type": "string",       "minLength": 1     },     "skuName": {       "type": "string",       "defaultValue": "S1",       "allowedValues": [         "F1",         "D1",         "B1",         "B2",         "B3",         "S1",         "S2",         "S3",         "P1",         "P2",         "P3",         "P4"       ],       "metadata": {         "description": "Describes plan's pricing tier and instance size. Check details at https://azure.microsoft.com/en-us/pricing/details/app-service/"       }     },     "storageAccountName": {       "type": "string",       "metadata": {         "description": "Storage Account to access blob storage."       }     },      "serviceBusNamespace": {       "type": "string",       "metadata": {         "description": "access service bus."       }     },     "skuCapacity": {       "type": "int",       "defaultValue": 1,       "minValue": 1,       "metadata": {         "description": "Describes plan's instance count"       }     }   },   "variables": {     "webSiteName": "[concat('webSite', uniqueString(resourceGroup().id))]",     "RedisName": "brando",          "storageAccountId": "[concat(resourceGroup().id,'/providers/Microsoft.Storage/storageAccounts/', parameters('storageAccountName'))]"   },   "resources": [     {       "apiVersion": "2015-08-01",       "name": "[parameters('hostingPlanName')]",       "type": "Microsoft.Web/serverfarms",       "location": "[resourceGroup().location]",       "tags": {         "displayName": "HostingPlan"       },       "sku": {         "name": "[parameters('skuName')]",         "capacity": "[parameters('skuCapacity')]"       },       "properties": {         "name": "[parameters('hostingPlanName')]"       }     },     {       "apiVersion": "2015-08-01",       "name": "[variables('webSiteName')]",       "type": "Microsoft.Web/sites",       "location": "[resourceGroup().location]",       "tags": {         "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]": "Resource",         "displayName": "Website"       },       "dependsOn": [         "[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]"       ],       "properties": {         "name": "[variables('webSiteName')]",         "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('hostingPlanName'))]"       },        "resources": [         {           "name": "connectionstrings",           "type": "config",           "apiVersion": "2015-08-01",           "dependsOn": [             "[concat('Microsoft.Web/sites/', variables('webSiteName'))]"           ],           "tags": {             "displayName": "tomConnectionString"           },           "properties": {              "storage": {               "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('storageAccountName'),';AccountKey=',concat(listKeys(variables('storageAccountId'),'2015-05-01-preview').key1))]",               "type": "Custom"             },              "Redis": {               "value": "[listKeys(resourceId('Microsoft.Cache/Redis', variables('RedisName')), '2016-04-01').primaryKey]",               "type": "Custom"             },              "ServiceBus": {               "value": "[listKeys(resourceId('Microsoft.ServiceBus/namespaces/AuthorizationRules',parameters('serviceBusNamespace'),'RootManageSharedAccessKey'),'2015-08-01').primaryConnectionString]",               "type": "Custom"             }            }         }       ]     }   ] } 
like image 174
Brando Zhang Avatar answered Oct 31 '22 13:10

Brando Zhang