Search code examples
azureazure-web-app-serviceazure-storageazure-resource-manager

ARM Template Can't Setup ConnectionString from Storage Account (for web-appsettings)


Hello StackOverflow community,

I am currently working on an Azure Resource Manager (ARM) nested template, where I'm trying to set up a connection string from my storage account. However, I keep running into a roadblock.

My ARM template code seems to be unable to establish the connection string from my storage account as expected. Instead, whenever I try to implement it, I get the following error:

Could not find resource Microsoft.Storage/storageAccounts/mystorageaccount under resource group .

I've checked the name of the storage account and it is indeed mystorageaccount. My storage account and connection string are also set in the same deployment nesting level.

I understand that in the error message suggests that the resource group is not properly defined. However, I have ensured that I have correctly mentioned the resource group in my template. So, I am at a loss as to why I am seeing this error.

I was wondering if anyone else has encountered this issue before, and if so, how did you resolve it? Any guidance or suggestions would be greatly appreciated.

Here's the relevant section of my ARM template for reference:


 {
      "condition": "[variables('ResourceGroupNameValidation')]",
      "type": "Microsoft.Resources/resourceGroups",
      "apiVersion": "2022-09-01",
      "name": "[parameters('resourceGroupName')]",
      "location": "[variables('regionLocation')]"
    }, 
{
              "type": "Microsoft.Storage/storageAccounts",
              "apiVersion": "2022-09-01",
              "name": "[variables('storageAccountName')]",
              "location": "[variables('regionLocation')]",
              "sku": {
                "name": "[variables('storageAccountType')]"
              },
              "kind": "[variables('storageKind')]",
              "properties": {
                "accessTier": "[variables('storageAccessTier')]",
                "secondaryLocation": "[variables('secondaryRegionLocation')]",
                "minimumTlsVersion":  "[variables('storageMinimumTlsVersion')]",                  
                "services": {
                  "blob": 
                  {
                  "enabled": "true",
                  "keyType": "string"
                  },
                  "file": 
                  {
                  "enabled": "false"
                  }
                },
                "virtualNetworkRules": [
                  {
                    "action": "Allow"
                  }
                ]
              }            
            },
            {
              "type": "Microsoft.Storage/storageAccounts/blobServices",
              "apiVersion": "2022-09-01",              
              "dependsOn" : [
                "[variables('storageAccountName')]"
              ],
              "name": "[concat(variables('storageAccountName'), '/default')]"
            },
            {
              "type": "Microsoft.Sql/servers/databases",
              "apiVersion": "2021-11-01",
              "name": "[concat(variables('sqlServerName'), '/', variables('sqlDatabaseName'))]",
              "location": "[variables('regionLocation')]",
              "sku": {
                "name": "Basic",
                "tier": "Basic"
              },
              "dependsOn": [
                "[variables('sqlServerName')]"
              ]
            },
            { 
              "type": "Microsoft.Web/sites", 
              "apiVersion": "2022-03-01", 
              "name": "[variables('webAppName')]",     
              "location": "[variables('regionLocation')]", 
              "kind": "app", 
              "properties": { 
                "serverFarmId": "[variables('appServicePlanName')]", 
                "siteConfig": { 
                  "linuxFxVersion": "[variables('webAppLunxfxVersion')]",
                  "alwaysOn": true,
                  "ftpsState": "FtpsOnly"                
                }, 
                "httpsOnly": true 
              },                         
              "dependsOn": [ 
                "[variables('appServicePlanName')]",
                "[variables('sqlServerName')]",
                "[variables('sqlDatabaseName')]",
                "[variables('storageAccountName')]"
              ]
            },
            {
              "type": "Microsoft.Web/sites/config",              
              "apiVersion": "2022-03-01",
              "name": "[concat(variables('webAppName'), '/appsettings')]",
              "kind": "string",
              "properties": {     
                "StorageAccountConnectionString": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2022-09-01').keys[0].value)]"
              },
              "dependsOn": [
                "[variables('webAppName')]",
                "[variables('appServicePlanName')]",
                "[variables('sqlServerName')]",
                "[variables('sqlDatabaseName')]",
                "[variables('storageAccountName')]"
              ]
            }, 

I have tried different versions and harcoding of the following line of code: "StorageAccountConnectionString": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2022-09-01').keys[0].value)]",

I have looked at various forums in search of a solution for my issue, but unfortunately, I've yet to find a helpful resolution to my error.

These forums: older stack overlfow

Listkeys function not working in variable

ARM quickstart templates from Azure


Solution

  • Your code looks good for me. But still the error shows that "resource/storage account not found".

    Verify below to resolve the issue:-

    • It is possible that the resource group is not properly defined.
    • You can also verify if the resource group is located in the same location as the storage account resource.
    • The issue might be related to the order of dependencies on how you are referring to the storage account name.
    • Here the Microsoft.Web/sites/config resource is dependent on the Microsoft.Web/sites, which is dependent on the Microsoft.Storage/storageAccounts resource. However, you're referencing variables('storageAccountName') without explicitly providing the resource group in the StorageAccountConnectionString field.

    Try modifying the storageaccountconnectionString as below:

    "StorageAccountConnectionString": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(resourceId(parameters('resourceGroupName'), 'Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2022-09-01').keys[0].value)]"
    

    After modifying it, I tried your code and was able to deploy it successfully.

    enter image description here

    Refer SO by @Jim Xu & MSDoc for the relevant storage account connection string issues.