ARM Templates for Azure Functions with many appSettings for different environments and slots

后端 未结 3 601
迷失自我
迷失自我 2020-12-10 19:02

I\'ve got two Azure Function apps that use deployment slots, stage and production. These two Azure Function apps have about 50~ key:value pairs in Application Settings to de

相关标签:
3条回答
  • 2020-12-10 19:26

    Sorry I don't have a huge amount of time to answer, and you have a bunch of questions which relate mainly to "what's the best way to...", and the answer is always "it depends".

    One thing I find easier to manage is instead of using siteConfig to set all the app settings, you can create a top level resource of type Microsoft.Web/sites/config (which I find useful sometimes as you can create them after the site is created, so if you have dependencies elsewhere that aren't setup yet, it can be handy to separate out the config and the site).

    "parameters": {
      "appSettings": {
        "type": "object",
        "defaultValue": {
          "property1": "value1",
          "property2": "value2"
        }
      }
    }
    
    "resources": [
      {
        "type": "Microsoft.Web/sites",
        "apiVersion": "2018-11-01",
        "name": "[parameters('function-app-name')]",
        "location": "[parameters('location')]",
        "kind": "functionapp",
        "properties": {
          "enabled": true,
          "serverFarmId": "..."
        }
      },
      {
        "type": "Microsoft.Web/sites/config",
        "name": "[concat(parameters('function-app-name'), '/appsettings')]",
        "apiVersion": "2018-11-01",
        "properties": "[parameters('appSettings')]"
        "dependsOn": [ "[resourceId('Microsoft.Web/sites/sites', parameters('function-app-name'))]",
      }
    ]
    

    One of the drawbacks of the above, is that you can't use certain functions in the params section, so you can't use listKeys() to get a key to a resource, so it's only useful sometimes, or like this example, if you wanted to add a reference to app insights which is also created in the same template, this isn't possible if you're passing in the settings as a param.

      {
        "type": "Microsoft.Web/sites/config",
        "name": "[concat(parameters('function-app-name'), '/appsettings')]",
        "apiVersion": "2018-11-01",
        "properties": {
          "property1": "value1",
          "property2": "value2",
          "APPINSIGHTS_INSTRUMENTATIONKEY": "[reference(resourceId('microsoft.insights/components/', variables('appInsightsName')), '2015-05-01').InstrumentationKey]"
        }
        "dependsOn": [ 
          "[resourceId('Microsoft.Web/sites/sites', parameters('function-app-name'))]",
          "[resourceId('microsoft.insights/components', variables('appInsightsName'))]"
      }
    

    You should really be resolving everything you can at deploy time, so a storage account (for example) connection string can be added into the template securely, and resolved at deploy time only.

    Another handy tip, is to use key vault to store any secure credentials, api keys, connection strings etc that cannot be resolved in the template. You mention needing them, but then you're committing them to source control in the templates... Well, they wont stay secret very long (another tip, ensure they all use securestring instead of string types, otherwise the portal will expose them in the deployment logs for the resource group). You can access key vaults from app settings like this:

    "secretConnectionString": "[concat('@Microsoft.KeyVault(SecretUri=https://', variables('vaultName'), '.vault.azure.net/secrets/my-connection-string/)')]",
    

    But for the above to work, you will need to give your application read access to the vault "vaultName", which should be fine if you use managed service identities.

    0 讨论(0)
  • 2020-12-10 19:30

    To answer this piece:

    Is it also true that all parameters defined in the parameter file have to be defined in the deployment template file in the parameters object?

    Yes everything in the parameters file needs to be defined in the deployment file. The opposite is not true. Everything defined in your deployment file does not need to be defined in your parameters file. The definition in the deployment file can have a default value:

    "location": {
      "type": "string",
      "defaultValue": "Central US",
      "metadata": {
        "description": "Specifies the Azure location where the key vault should be created."
      }
    },
    

    Alternatively a parameter can be passed in as an override parameter in a release task.

    0 讨论(0)
  • 2020-12-10 19:41

    It is possible to combine static configuration with deployment-time references. You use the union template function to combine your static configuration (object or array) with some deployment-time value that you wrap using the json template function.

    I use this to set Application Insights at deployment-time with a base config object and a app service-specific object (broken up for easier reading):

    [union(
      variables('appServiceBaseConfig'), 
      variables('appService1'), 
      json(
        concat(
          '{\"APPINSIGHTS_INSTRUMENTATIONKEY\":\"', 
          reference(concat('microsoft.insights/components/', variables('applicationInsightsName')), '2015-05-01').InstrumentationKey,
           '\"}')
        )
      )
    ]
    
    0 讨论(0)
提交回复
热议问题