David Gard
David Gard

Reputation: 12047

Use Azure Container Instances via ARM to create an indeterminate number of containers

I am attempting to deploy an Azure Storage account along with an indeterminate number of tables via an ARM template.

Since MS are yet to provide a tables resource type for ARM, I'm instead using Azure Container Instances to spin up a container running azure-cli and then create the table that way.

As you can see in my example below, I'm using property iteration to create multiple containers - one for each table. This seemed to be working until the number of tables to create changed, and then I started getting errors.

The updates on container group 'your-aci-instance' are invalid. If you are going to update the os type, restart policy, network profile, CPU, memory or GPU resources for a container group, you must delete it first and then create a new one.

I understand what it's saying, but it does seem strange to me that you can create a container group yet not alter the group of containers within.

As ARM doesn't allow you do delete resources, I'd have to add a manual step to my deployment process to ensure that the ACI doesn't exist, which isn't really desirable.

Equally undesirable would be to use resource iteration to create multiple ACI's - there would be the possibility of many ACI's being strewn about the Resource Group that will never be used again.

Is there some ARM magic that I don't yet know about which can help me achieve the creation of tables that meets the following criteria?

Notes

I have tried to use variable iteration to create a single 'command' array for a single container, but it seems that ACI considers all commands as a one liner, so this caused an error.

Further reading suggests that it is only possible to run one command on container startup.

How do I run multiple commands when deploying a container group?

Current attempt

Here is a snippet from my ARM template showing how I used property iteration to try and achieve my goal.

{
    "condition": "[not(empty(variables('tables')))]",
    "type": "Microsoft.ContainerInstance/containerGroups",
    "name": "[parameters('containerInstanceName')]",
    "apiVersion": "2018-10-01",
    "location": "[resourceGroup().location]",
    "properties": {
        "copy": [
            {
                "name": "containers",
                "count": "[max(length(variables('tables')), 1)]",
                "input": {
                    "name": "[toLower(variables('tables')[copyIndex('containers')])]",
                    "properties": {
                        "image": "microsoft/azure-cli",
                        "command": [
                            "az",
                            "storage",
                            "table",
                            "create",
                            "--name",
                            "[variables('tables')[copyIndex('containers')]]"
                        ],
                        "environmentVariables": [
                            {
                                "name": "AZURE_STORAGE_KEY",
                                "value": "[listkeys(parameters('storageAccount_Application_Name'), '2019-04-01').keys[0].value]"
                            },
                            {
                                "name": "AZURE_STORAGE_ACCOUNT",
                                "value": "[parameters('storageAccount_Application_Name')]"
                            },
                            {
                                "name": "DATETIME",
                                "value": "[parameters('dateTime')]"
                            }
                        ],
                        "resources": {
                            "requests": {
                                "cpu": "1",
                                "memoryInGb": "1.5"
                            }
                        }
                    }
                }
            }
        ],
        "restartPolicy": "OnFailure",
        "osType": "Linux"
    },
    "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccount_Application_Name'))]"
    ],
    "tags": {
        "displayName": "Application Storage Account - Tables",
        "Application": "[parameters('tagApplication')]",
        "environment": "[parameters('tagEnvironment')]",
        "version": "[parameters('tagVersion')]"
    }
}

Upvotes: 1

Views: 1104

Answers (1)

4c74356b41
4c74356b41

Reputation: 72181

If it says the field is immutable - it is, there's nothing you can do about it really. You can always create a unique name for that container instance and use complete deployment mode and only deploy ACI to this particular resource group, that way it will always have only this ACI instance and others will get deleted and it will work around immutability.

you can call an azure function from inside the template (HTTP trigger) and pass in names of storage tables to create and it will do that, for example.

But either way its a hack.

Upvotes: 1

Related Questions