Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy app settings to azure function app

Need help in finding a way to deploy my application's custom app setting via either - 1. Using appsettings.json created by my .funproj (vs tools for 2015) 2. appveyor's environment variables 3. Any other trick

All I want is to avoid having to set these things manually in the portal and have them source controlled (better - using deployment, ex. - appveyor's secure environment variables)

Thanks in advance! below are an example of portal settings I am targetting -

enter image description here

example of appveyor's environment variable setting -

environment:
    SolutionDir: $(APPVEYOR_BUILD_FOLDER)\
    my_var1: value1
    my_var2: value2

sample usage in function app (run.csx) -

using System;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;

public static void Run(string input, TraceWriter log)
{
    log.Info($"C# manually triggered function called with input: {input}");
    log.Info($"This is a custom setting: {GetEnvironmentVariable("my_var1")}");
}

public static string GetEnvironmentVariable(string name)
{
    return name + ": " +
        System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
}
like image 538
Abhishek Avatar asked Mar 10 '17 20:03

Abhishek


People also ask

How do you specify app settings for an Azure Functions project during local development?

To find the application settings, see Get started in the Azure portal. The Application settings tab maintains settings that are used by your function app. You must select Show values to see the values in the portal. To add a setting in the portal, select New application setting and add the new key-value pair.

How do I enable application insights in Azure function app?

In your function app, select Configuration under Settings, and then select Application settings. If you see a setting named APPINSIGHTS_INSTRUMENTATIONKEY , Application Insights integration is enabled for your function app running in Azure.


1 Answers

You can do this by arm templates. A sample arm template to do this:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "functionappname": {
      "type": "string"
    }
  },
  "variables": {
    "serviceplanname": "[concat('functionserviceplan-',parameters('functionappname'),'-', uniqueString(resourceGroup().id))]",
    "functionstoragename": "[substring(toLower(concat('st',parameters('functionappname'), uniqueString(resourceGroup().id))),0,24)]"
  },
  "resources": [
    {
        "name": "[variables('serviceplanname')]",
        "type": "Microsoft.Web/serverfarms",
        "kind": "functionapp",
        "sku": {
            "name": "Y1",
            "tier": "Dynamic",
            "size": "Y1",
            "family": "Y",
            "capacity": 0
        },
        "apiVersion": "2015-08-01",
        "location": "[resourceGroup().location]",
        "properties": { "name": "[variables('serviceplanname')]" }
    },
    {
      "type": "Microsoft.Storage/storageAccounts",
      "name": "[variables('functionstoragename')]",
      "apiVersion": "2016-01-01",
      "sku": { "name": "Standard_LRS" },
      "location": "[resourceGroup().location]",
      "kind": "Storage"
    },
    {
        "type": "Microsoft.Web/sites",
        "kind": "functionapp",
        "name": "[parameters('functionappname')]",
        "apiVersion": "2015-08-01",
        "location": "[resourceGroup().location]",
        "properties": {
            "name": "[parameters('functionappname')]",
            "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('serviceplanname'))]",
            "hostNames": [ "[concat(parameters('functionappname'),'.azurewebsites.net')]" ],
            "enabledHostNames": [
                "[concat(parameters('functionappname'),'.azurewebsites.net')]",
                "[concat(parameters('functionappname'),'.scm.azurewebsites.net')]"
            ],
            "siteConfig": {
                "appSettings": [
                    { "name": "FUNCTIONS_EXTENSION_VERSION", "value": "~1" },
                    { "name": "AzureWebJobsDashboard", "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',variables('functionstoragename'),';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('functionstoragename')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value)]" },
                    { "name": "AzureWebJobsStorage", "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',variables('functionstoragename'),';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('functionstoragename')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value)]" },
                    { "name": "WEBSITE_NODE_DEFAULT_VERSION", "value": "6.5.0" }
                ]
            }
        },
        "dependsOn": [
            "[resourceId('Microsoft.Web/serverfarms', variables('serviceplanname'))]",
            "[resourceId('Microsoft.Storage/storageAccounts', variables('functionstoragename'))]"
        ]
    }
  ]
}

You can read how to automate deployments from VSTS: use-vsts-to-deploy-functions-as-infrastructure-as-code

like image 184
Peter Avatar answered Sep 20 '22 23:09

Peter