0

I'm attempting to create a Storage Account with a file share via an ARM template. To do this, I'm creating the Storage Account and then running a az CLI command within a container instance, as described here.

The template deploys just fine, but the trouble is that the container is only started on the first run. Subsequent deployments do not result in the container instance being started, and thus if the file share has been removed (humans make mistakes), it's not recreated.

I can't use a complete deployment because there are other resources in the Resource Group.

I have consulted to documentation, but there doesn't seem to be anything about this in there.

Is there anyway to tell the container instance to always start?

Here is the example template that I am using -

{
  "$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",
        "Standard_ZRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    },
    "storageAccountName": {
      "type": "string",
      "defaultValue": "[uniquestring(resourceGroup().id)]",
      "metadata": {
        "description": "Storage Account Name"
      }
    },
    "fileShareName": {
      "type": "string",
      "metadata": {
        "description": "File Share Name"
      }
    },
    "containerInstanceLocation": {
      "type": "string",
      "defaultValue": "[parameters('location')]",
      "allowedValues": [
        "westus",
        "eastus",
        "westeurope",
        "southeastaisa",
        "westus2"
      ],
      "metadata": {
        "description": "Container Instance Location"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "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": "2017-10-01",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "kind": "Storage",
      "properties": {}
    },
    {
      "name": "[variables('containerGroupName')]",
      "type": "Microsoft.ContainerInstance/containerGroups",
      "apiVersion": "2018-02-01-preview",
      "location": "[parameters('containerInstanceLocation')]",
      "dependsOn": [
        "[concat('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'),'2017-10-01').keys[0].value]"
                },
                {
                  "name": "AZURE_STORAGE_ACCOUNT",
                  "value": "[parameters('storageAccountName')]"
                }
              ],
              "resources": {
                "requests": {
                  "cpu": "[variables('cpuCores')]",
                  "memoryInGb": "[variables('memoryInGb')]"
                }
              }
            }
          }
        ],
        "restartPolicy": "OnFailure",
        "osType": "Linux"
      }
    }
  ]
}
David Gard
  • 11,225
  • 36
  • 115
  • 227

1 Answers1

0

What if you manually delete the ACI from Azure portal or trough az cli in between using az container delete. As I understand Azure Container Instances are meant to be disposable. I usually just delete the old container when I want to deploy new one. Of course this is done usually by CI&CD pipeline like Jenkins.

EDIT: ARM-template only starts the container if it doesn't find the resource but because it does find the ACI it does nothing. IMHO you should not use ARM-Templates for container management.

Toni Nurmi
  • 355
  • 4
  • 20
  • I find that if I change the command that is being run, the container instance starts. ARM is (incorrectly IMO) not starting it on occasions where that doesn't happen. For typical infrastructure that would suffice, but in cases like this it's not appropriate. My hope is that MS will add support for provisioning File Shares via ARM, as they have for Blob Containers. In this case though, I'm going to abandon my efforts, as the whole idea was to remove the need to use both ARM and CLI, so having to use CLI to delete a Container Instance defeats to point. Thanks for the answer though. – David Gard Feb 19 '19 at 14:43
  • You could try to approach the problem from other provisioning technologies like Terraform or using ARM in conjunction with CI&CD job that creates the container and the fileshare – Toni Nurmi Feb 20 '19 at 08:26