Azure ARM Template DependentOn FileShare

Viewed 676

I need to create via ARM template storage account --> File share --> Container with mounted fileshare.
The dependency is:

  1. file share depends on storage account
  2. container depends on file share

How to get ReferenceId of Fileshare?

I have the following code:

    {
        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "storageAccountType": {
                "type": "string",
                "defaultValue": "Standard_LRS",
                "allowedValues": [
                    "Standard_LRS",
                    "Standard_GRS"
                ],
                "metadata": {
                    "description": "Storage account type (SKU)"
                }
            },
            "fileShareName": {
                "type": "string",
                "minLength": 3,
                "maxLength": 63,
                "defaultValue": "sftp",
                "metadata": {
                    "description": "Name of the File Share to be created. "
                }
            }
        },
        "variables": {
            "deploymentLocation": "[resourceGroup().location]",
            "storageAccountName": "[concat('sftpstr', uniqueString(resourceGroup().id))]"
        },
        "resources": [
            {
                "type": "Microsoft.Storage/storageAccounts",
                "name": "[variables('storageAccountName')]",
                "apiVersion": "2019-06-01",
                "location": "[variables('deploymentLocation')]",
                "sku": {
                    "name": "[parameters('storageAccountType')]"
                },
                "kind": "StorageV2",
                "properties": {
                    "accessTier": "Hot"
                }
            },
            {
                "type": "Microsoft.Storage/storageAccounts/fileServices/shares",
                "apiVersion": "2019-06-01",
                "properties": {
                    "accessTier": "Hot"
                },
                "name": "[concat(variables('storageAccountName'), '/default/', parameters('fileShareName'))]",
                "dependsOn": [
                    "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
                ]
            }, 
            {
                "type": "Microsoft.ContainerInstance/containerGroups",
                "name": "sftp-container-group",
                "apiVersion": "2018-04-01",
                "dependsOn": [
                    "[resourceId('Microsoft.Storage/storageAccounts/fileServices/shares', ????? )]" //how to get reference to specific Fileshare??? I've tried >   [concat(variables('storageAccountName'), '/default/', parameters('fileShareName'))] but it didn't work
                ],
                "properties": {
                    .......
                }
            }
        ] 
    }

and I don't know how to set dependency on Fileshare:

"dependsOn": [
    "[resourceId('Microsoft.Storage/storageAccounts/fileServices/shares', ????? )]" //how to get reference to specific Fileshare??? 
],

I've tried [concat(variables('storageAccountName'), '/default/', parameters('fileShareName'))] but it didn't work.

Any idea?

Thank you

3 Answers

Use following format:

"dependsOn": [
    "[resourceId('Microsoft.Storage/storageAccounts/fileServices/shares', variables('storageAccountName') , 'default', parameters('fileShareName') )]"
],

You could create two separate resources Microsoft.Storage/storageAccounts/fileServices and Microsoft.Storage/storageAccounts/fileServices/shares and try to set dependency on Fileshare like this:

        "dependsOn": [
            "[resourceId('Microsoft.Storage/storageAccounts/fileServices/shares', variables('storageAccountName'),  'default',  parameters('fileShareName') )]" 
        ],

Alternatively, It's recommended to creates a storage account and a file share via azure-CLI in a Container Instance and refer to this quickstart template.

 "variables": {
    "image": "microsoft/azure-cli",
    "cpuCores": "1.0",
    "memoryInGb": "1.5",
    "containerGroupName": "createshare-containerinstance",
    "containerName": "createshare"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "name": "[parameters('storageAccountName')]",
      "apiVersion": "2019-06-01",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "kind": "StorageV2"
    },
    {
      "name": "[variables('containerGroupName')]",
      "type": "Microsoft.ContainerInstance/containerGroups",
      "apiVersion": "2019-12-01",
      "location": "[parameters('containerInstanceLocation')]",
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts/', parameters('storageAccountName'))]"
      ],
      "properties": {
        "containers": [
          {
            "name": "[variables('containerName')]",
            "properties": {
              "image": "[variables('image')]",
              "command": [
                "az",
                "storage",
                "share",
                "create",
                "--name",
                "[parameters('fileShareName')]"
              ],
              "environmentVariables": [
                {
                  "name": "AZURE_STORAGE_KEY",
                  "value": "[listKeys(parameters('storageAccountName'),'2019-06-01').keys[0].value]"
                },
                {
                  "name": "AZURE_STORAGE_ACCOUNT",
                  "value": "[parameters('storageAccountName')]"
                }
              ],
              "resources": {
                "requests": {
                  "cpu": "[variables('cpuCores')]",
                  "memoryInGb": "[variables('memoryInGb')]"
                }
              }
            }
          }
        ],
        "restartPolicy": "OnFailure",
        "osType": "Linux"
      }
    }
  ]

I found a couple of things that made it work for me:

ACI has to depend on the share otherwise it gets created ahead of volume sometimes

"dependsOn": ["resourceId('Microsoft.Storage/storageAccounts/fileServices/shares',variables('storage-account-name'), 'default', variables('file-share-name'))]"]

For some reason, the share wasn't working as a child resource so I had to make it a full resource

{
    "type": "Microsoft.Storage/storageAccounts/fileServices/shares",
    "apiVersion": "2021-04-01",
    "name": "[concat(variables('storage-account-name'),'/default/',variables('nginx-share-name'))]",
    "properties":{
        "enabledProtocols": "SMB"
    },
    "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storage-account-name'))]"
    ]
}

Also, I had to pay attention to default part of the resource ID

"name": "[concat(variables('storage-account-name'),'/default/',variables('nginx-share-name'))]"

And finally, ACI wasn't connecting properly unless I explicitly enabled SMB as a protocol

"properties":{
    "enabledProtocols": "SMB"
}

This is a template I'm using to spin up NGINX in Azure Container instances with an Azure File Share as a mounted volume.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "containerGroups_name": {
            "defaultValue": "container-app",
            "type": "String"
        },
        "containerGroups_reverse_proxy_name": {
            "defaultValue": "app-proxy",
            "type": "String"
        },
        "acrPassword": {
            "type": "securestring"
        }
    },
    "variables": {
        "nginx-proxy-image": "nginx:stable",
        "storage-account-name": "[concat('storage', uniqueString(resourceGroup().name))]",
        "nginx-share-name": "nginx-share",
        "nginx-volume-name": "nginx-volume"
    },
    "resources": [
        {
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2017-10-01",
            "name": "[variables('storage-account-name')]" ,
            "location": "[resourceGroup().location]",
            "kind": "StorageV2",
            "sku": {
                "name": "Standard_LRS",
                "tier": "Standard"
            }
        },
        {
            "type": "Microsoft.Storage/storageAccounts/fileServices/shares",
            "apiVersion": "2021-04-01",
            "name": "[concat(variables('storage-account-name'),'/default/',variables('nginx-share-name'))]",
            "properties":{
                "enabledProtocols": "SMB"
            },
            "dependsOn": [
                "[resourceId('Microsoft.Storage/storageAccounts', variables('storage-account-name'))]"
            ]
        },
        {
            "type": "Microsoft.ContainerInstance/containerGroups",
            "apiVersion": "2021-03-01",
            "name": "[parameters('containerGroups_name')]",
            "location": "[resourceGroup().location]",
            "properties": {
                "sku": "Standard",
                "containers": [
                    {
                        "name": "[parameters('containerGroups_reverse_proxy_name')]",
                        "properties" : {
                            "image": "[variables('nginx-proxy-image')]",
                            "ports": [
                                {
                                    "protocol": "TCP",
                                    "port": 80
                                },
                                {
                                    "protocol": "TCP",
                                    "port": 433
                                }
                            ],
                            "volumeMounts" : [
                                {
                                    "name": "[variables('nginx-volume-name')]",
                                    "mountPath": "/etc/nginx"
                                }
                            ],
                            "resources": {
                                "requests":{
                                    "cpu": 1,
                                    "memoryInGB": 1.5
                                }
                            }
                        }
                    }                    
                ],
                "initContainers": [],
                "restartPolicy": "OnFailure",
                "osType": "Linux",
                "volumes": [
                    {
                        "name": "[variables('nginx-volume-name')]",
                        "azureFile": {
                            "shareName": "[variables('nginx-share-name')]",
                            "storageAccountName": "[variables('storage-account-name')]",
                            "storageAccountKey": "[listKeys(resourceId('Microsoft.Storage/storageAccounts',variables('storage-account-name')),'2017-10-01').keys[0].value]"
                        }
                    }
                ]
            },
            "dependsOn": [
                "[resourceId('Microsoft.Storage/storageAccounts/fileServices/shares',variables('storage-account-name'), 'default', variables('nginx-share-name'))]"
            ]
        }
    ]
}
Related