Aryan M
Aryan M

Reputation: 657

ARM - Map an existing application insights resource to the app-service

I need to map an existing application insights resource from a different resource group to an app-service

I'm creating an app-service as part of the ARM template. Could you please let me know, how will i be able to map it.

PFB my template using which i'm trying to create a new app-service,

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
"serverFarmName": {
  "type": "string",
  "defaultValue": "asp-prod-brand-digital-onlinesetup-shared"
},
"serverFarmResourceGroup": {
  "type": "string",
  "defaultValue": "rg-prod-brand-digital-onlinesetup-shared"
},
"ss-prod-brand-digitial-onlinesetup-govAdminLogin": {
  "type": "string",
  "minLength": 1
},
"ss-prod-brand-digitial-onlinesetup-govAdminLoginPassword": {
  "type": "securestring"
},
"db-prod-brand-digital-onlinesetup-govName": {
  "type": "string",
  "minLength": 1
},
"db-prod-brand-digital-onlinesetup-govCollation": {
  "type": "string",
  "minLength": 1,
  "defaultValue": "SQL_Latin1_General_CP1_CI_AS"
},
"db-prod-brand-digital-onlinesetup-govEdition": {
  "type": "string",
  "defaultValue": "Standard",
  "allowedValues": [
    "Basic",
    "Standard",
    "Premium"
  ]
},
"db-prod-brand-digital-onlinesetup-govRequestedServiceObjectiveName": {
  "type": "string",
  "defaultValue": "S2",
  "allowedValues": [
    "Basic",
    "S0",
    "S1",
    "S2",
    "P1",
    "P2",
    "P3"
  ],
  "metadata": {
    "description": "Describes the performance level for Edition"
  }
}},
  "variables": {
"ap-prod-brand-digital-onlinesetup-govName": "[concat('ap-prod-brand-digital-onlinesetup-gov', uniqueString(resourceGroup().id))]",
"ss-prod-brand-digitial-onlinesetup-govName": "[concat('ss-prod-brand-digitial-onlinesetup-gov', uniqueString(resourceGroup().id))]"},
  "resources": [
{
  "name": "[variables('ap-prod-brand-digital-onlinesetup-govName')]",
  "type": "Microsoft.Web/sites",
  "location": "australiasoutheast",
  "apiVersion": "2015-08-01",
  "dependsOn": [ ],
  "tags": {
    "[concat('hidden-related:', resourceId(parameters('serverFarmResourceGroup'), 'Microsoft.Web/serverFarms', parameters('serverFarmName')))]": "Resource",
    "displayName": "ap-prod-brand-digital-onlinesetup-gov"
  },
  "properties": {
    "name": "[variables('ap-prod-brand-digital-onlinesetup-govName')]",
    "serverFarmId": "[resourceId(parameters('serverFarmResourceGroup'), 'Microsoft.Web/serverFarms', parameters('serverFarmName'))]"
  }
},
{
  "name": "[variables('ss-prod-brand-digitial-onlinesetup-govName')]",
  "type": "Microsoft.Sql/servers",
  "location": "[resourceGroup().location]",
  "apiVersion": "2014-04-01-preview",
  "dependsOn": [ ],
  "tags": {
    "displayName": "ss-prod-brand-digitial-onlinesetup-gov"
  },
  "properties": {
    "administratorLogin": "[parameters('ss-prod-brand-digitial-onlinesetup-govAdminLogin')]",
    "administratorLoginPassword": "[parameters('ss-prod-brand-digitial-onlinesetup-govAdminLoginPassword')]"
  },
  "resources": [
    {
      "name": "AllowAllWindowsAzureIps",
      "type": "firewallrules",
      "location": "[resourceGroup().location]",
      "apiVersion": "2014-04-01-preview",
      "dependsOn": [
        "[resourceId('Microsoft.Sql/servers', variables('ss-prod-brand-digitial-onlinesetup-govName'))]"
      ],
      "properties": {
        "startIpAddress": "0.0.0.0",
        "endIpAddress": "0.0.0.0"
      }
    },
    {
      "name": "[parameters('db-prod-brand-digital-onlinesetup-govName')]",
      "type": "databases",
      "location": "[resourceGroup().location]",
      "apiVersion": "2014-04-01-preview",
      "dependsOn": [
        "[resourceId('Microsoft.Sql/servers', variables('ss-prod-brand-digitial-onlinesetup-govName'))]"
      ],
      "tags": {
        "displayName": "db-prod-brand-digital-onlinesetup-gov"
      },
      "properties": {
        "collation": "[parameters('db-prod-brand-digital-onlinesetup-govCollation')]",
        "edition": "[parameters('db-prod-brand-digital-onlinesetup-govEdition')]",
        "maxSizeBytes": "1073741824",
        "requestedServiceObjectiveName": "[parameters('db-prod-brand-digital-onlinesetup-govRequestedServiceObjectiveName')]"
      }
    }
  ]
}],
  "outputs": {}
}

I need to map the existing application insights to the app-service "ap-prod-brand-digital-onlinesetup-gov", which is part of the existing ARM-template.

Please advice on the same.

Upvotes: 0

Views: 982

Answers (1)

Joy Wang
Joy Wang

Reputation: 42063

To set the application insight to app service, it actually add an application setting called APPINSIGHTS_INSTRUMENTATIONKEY to the app service.

You could try to add the template snippet to your template.

parameters:

 "applicationinsightkey": {
            "type": "String"
        }

resources:

"siteConfig": {
                    "appSettings": [
                        {
                            "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                            "value": "[parameters('applicationinsightkey')]"
                        }
                    ]
                }

Complete sample template:

{
    "parameters": {
        "name": {
            "type": "string"
        },
        "hostingPlanName": {
            "type": "string"
        },
        "location": {
            "type": "string"
        },
        "hostingEnvironment": {
            "type": "string"
        },
        "serverFarmResourceGroup": {
            "type": "string"
        },
        "subscriptionId": {
            "type": "string"
        },
           "applicationinsightkey": {
            "type": "String"
        }

    },
    "resources": [
        {
            "apiVersion": "2016-03-01",
            "name": "[parameters('name')]",
            "type": "Microsoft.Web/sites",
            "properties": {
                "name": "[parameters('name')]",
                "siteConfig": {
                    "appSettings": [
                        {
                            "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                            "value": "[parameters('applicationinsightkey')]"
                        }
                    ]
                },
                "serverFarmId": "[concat('/subscriptions/', parameters('subscriptionId'),'/resourcegroups/', parameters('serverFarmResourceGroup'), '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]",
                "hostingEnvironment": "[parameters('hostingEnvironment')]"
            },
            "location": "[parameters('location')]"
        }
    ],
    "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0"
}

It works fine on my side.

enter image description here

enter image description here

You could navigate to your application insight in the portal to get the applicationinsightkey, refer to the screenshot. Note the template will overwrite the all the application settings in your app service.

enter image description here

Besides, instead of using ARM template, I recommend you to use REST API, essentially, the template is also calling API.

Upvotes: 1

Related Questions