ARM - 仅在不存在时部署存储帐户

ARM - Deploy storage account only if not exists

是否可以使用 Azure 资源管理器 (ARM) 模板仅在存储帐户不存在时部署该存储帐户?

以下模板将创建:

  1. App Insights 资源(共享)
  2. 存储帐户(共享)
  3. 应用计划
  4. 配置包含 App Insights Instrumentation Key 和存储帐户连接字符串的应用程序实例。

我希望前两个步骤是可选的,意思是如果它们已经存在,就使用它们。

到目前为止我唯一发现的是 newOrExisting 模式,但这没有任何意义。该脚本应该能够判断这些资源是否已经存在并简单地跳过创建。

其他部署脚本将使用相同的 App Insights 和存储帐户,因此如果模板能够解决这个问题就好了。

感谢您的帮助!

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "environmentName": {
        "type": "string",
        "defaultValue": "Dev",
        "allowedValues": [
            "Dev",
            "Test",
            "Prod"
        ]
    }
},
"variables": {
    "rgLocation": "[resourceGroup().location]",
    "insightsName": "[concat('Insights-', parameters('environmentName'))]",
    "appName": "[concat('MyAppName-', parameters('environmentName'))]",
    "genStorageName": "[concat('blgenstorage', parameters('environmentName'))]"
},
{
        "comments": "Creates a general storage account that is used to save various data, including configurations.",
        "name": "[variables('genStorageName')]",
        "type": "Microsoft.Storage/storageAccounts",
        "apiVersion": "2017-06-01",
        "sku": {
            "name": "Standard_LRS"
        },
        "kind": "Storage",
        "location": "[variables('rgLocation')]",
        "tags": {},
        "properties": {}            
    },
    {
        "comments": "Creates the service plan under which the web app will live.",
        "name": "[concat('ServicePlan-MyApp-', parameters('environment'))]",
        "type": "Microsoft.Web/serverfarms",
        "apiVersion": "2016-09-01",
        "kind": "app",
        "location": "[variables('rgLocation')]",
        "tags": {},
        "properties": {
            "name": "[concat('ServicePlan-MyApp-', parameters('environmentName'))]",
            "perSiteScaling": "false",
            "reserved": "false"
        },
        "sku": {
            "name": "S1",
            "tier": "Standard",
            "size": "S1",
            "family": "S",
            "capacity": 1
        }
    },
    {
        "comments": "Primary web app deployment.",
        "name": "[variables('appName')]",
        "type": "Microsoft.Web/sites",
        "apiVersion": "2016-08-01",
        "kind": "app",
        "location": "[variables('rgLocation')]",
        "tags": {},
        "dependsOn": [
            "[resourceId('Microsoft.Web/serverfarms', concat('ServicePlan-MyApp-', variables('envShortName')))]",
            "[resourceId('Microsoft.Storage/storageAccounts', variables('genStorageName'))]",
            "[resourceId('microsoft.insights/components', variables('insightsName'))]"
        ],
        "properties": {
            "enabled": true,
            "hostNameSslStates": [
                {
                    "name": "[concat(variables('appName'), '.azurewebsites.net')]",
                    "sslState": "Disabled"
                },
                {
                    "name": "[concat(variables('appName'), '.scm.azurewebsites.net')]",
                    "sslState": "Disabled"
                }
            ],
            "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', concat('ServicePlan-MyApp-', parameters('environmentName')))]",
            "siteConfig": {
                "numberOfWorkers": 1,
                "defaultDocuments": [
                    "Default.htm",
                    "Default.html",
                    "Default.asp",
                    "index.htm",
                    "index.html",
                    "iisstart.htm",
                    "default.aspx",
                    "index.php",
                    "hostingstart.html"
                ],
                "netFrameworkVersion": "v4.6",
                "appSettings": [
                    {
                        "name": "AppInsightsInstrumentationKey",
                        "value": "[reference(resourceId('Microsoft.Insights/components', variables('insightsName')), '2015-05-01').InstrumentationKey]"
                    }
                ],
                "connectionStrings": [
                    {
                        "name": "AzureStorage",
                        "connectionString": "[listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('genStorageName')), '2017-06-01').keys[0].value]",
                        "type": "Custom"
                    }
                ],
                "alwaysOn": true,
                "managedPipelineMode": "Integrated",
                "virtualApplications": [
                    {
                        "virtualPath": "/",
                        "physicalPath": "site\wwwroot",
                        "preloadEnabled": false
                    }
                ],
                "autoHealEnabled": false,
                "vnetName": ""
            },
            "microService": "WebSites",
            "clientAffinityEnabled": false,
            "clientCertEnabled": false,
            "hostNamesDisabled": false
        }
}

如果存在存储帐户,您将如何创建?

基本上,如果 ARM 模板遇到其尝试部署的资源,它会在属性不匹配时更新它。在你的情况下它不会做任何事情(它会跳过它)。