Creating both the Azure web app and the Application Insights resources independently is no problem and should be relatively easy for anyone familiar with ARM. However, creating them fully integrated takes just a little bit more work. It’s kind of because you would want them both to be linked to each other.
If you use the Visual Studio wizard for creating an ARM template, you’ll notice that it forces the AppInsights resource to be dependent on the web app being created. So first you need to create web app and then AppInsights resource. However, when AppInsights resource is created, it would also generate instrumentation key which you would want to put inside the application settings of the web app. So we would need to do it the other way around. In this blog post, we’ll learn how to achieve our objective and create both of them in one go.
Creating application insight resource
To deploy application insight resource, we would need to use resource type microsoft.insights/components. We can use something like below code:
Do notice that we add a new tag with the name matching the resource id of the web app that we’re going to create. We can do this even if the web app has not been created yet, because we already know what the resource ID is going to look like, and also because the platform will not validate that this represents a valid resource.
The reason we add this tag, is that this creates the link between the web app and application insights resources. This allows the portal experience to work so that when you navigate to the application insights option in the web app left-side menu, it shows you the data from the right AppInsights resource.
Create the web app
The second step is creating the App Service Plan and the Web App. There are plenty of good examples of this already including previous blog posts as well. However we need to modify it for few things.
Create resources in right order
We want to make sure that the web app is created after the AppInsights resource, which we do by adding an explicit dependency next to the web app code:
This will allow us to capture the instrumentation key for the brand new AppInsights resource.
Set properties for web app
We would also need to include the key as part of web app properties:
Adding application insight site extension
If our web app is going to host a .NET Application, we also want to make sure that the Application Insights site extension is deployed in Kudu for our app. This adds the necessary profiler so that we get full dependency traces.
We can do this with yet another nested resource in our ARM template: