azure - 通过 ARM 使用 Azure 容器实例创建不确定数量的容器

标签 azure azure-resource-manager azure-rm-template azure-container-instances

我正在尝试通过 ARM 模板部署 Azure 存储帐户以及不确定数量的表。

由于 MS 尚未为 ARM 提供表资源类型,因此我改为使用 Azure 容器实例来启动运行 azure-cli 的容器,然后以这种方式创建表。

正如您在下面的示例中看到的,我正在使用 property iteration创建多个容器 - 每个表一个。这似乎一直有效,直到要创建的表数量发生变化,然后我开始收到错误。

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.

我明白它的意思,但对我来说,你可以创建一个容器组但不能更改其中的容器组,这似乎很奇怪。

由于 ARM 不允许您删除资源,因此我必须在部署过程中添加手动步骤以确保 ACI 不存在,这并不是真正理想的情况。

同样不可取的是使用 resource iteration创建多个 ACI - 可能会出现许多 ACI 散布在资源组中而永远不会再次使用的情况。

是否有一些我还不知道的 ARM 魔法可以帮助我创建满足以下条件的表?

  • 仅创建一个 ACI。
  • 要创建的表的数量可以更改。

注释

我尝试使用variable iteration为单个容器创建单个“命令”数组,但 ACI 似乎将所有命令视为一个行,因此这导致了错误。

进一步阅读表明,在容器启动时只能运行一个命令。

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

当前尝试

这是我的 ARM 模板中的一个片段,展示了我如何使用 property iteration努力实现我的目标。

{
    "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')]"
    }
}

最佳答案

如果它说该字段是不可变的 - 确实如此,那么您实际上对此无能为力。您始终可以为该容器实例创建一个唯一的名称,并使用完整部署模式,并且仅将 ACI 部署到此特定资源组,这样它将始终只有此 ACI 实例,而其他实例将被删除,并且它将解决不变性问题。

例如,您可以从模板(HTTP 触发器)内部调用 azure 函数并传入要创建的存储表的名称,它就会执行此操作。

但不管怎样,这都是一种黑客行为。

关于azure - 通过 ARM 使用 Azure 容器实例创建不确定数量的容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56738841/

相关文章:

azure - 适用于 Azure Active Directory 的 ARM 模板

azure - Arm 模板 - 在请求中发现错误的 JSON 内容

azure - 如何在 Azure 中使用 ARM 模板打印链接模板的输出

azure - 使用 Azure Redis DB 配置 RedisOutputCacheProvider

Azure 应用服务二头肌网络访问限制禁止公共(public)访问

Azure ARM 部署 DNS 条目

json - 使用 DependOn 配置访问策略

django - python 社交身份验证更改引荐来源地址

c# - 在异步 Azure 函数中捕获异常的位置

angular - 如何在 Angular 中使用 Azure 应用程序配置