{
  "$type": "site.standard.document",
  "canonicalUrl": "https://johnnyreilly.com/posts/application-insights-bicep-azure-static-web-apps",
  "description": "Application Insights are a great way to monitor Azure Static Web Apps and Azure Functions. But how do you deploy that using Bicep? Let's find out!",
  "path": "/posts/application-insights-bicep-azure-static-web-apps",
  "publishedAt": "2023-01-01T00:00:00.000Z",
  "site": "at://did:plc:yy3apqjlms24kso7ahn7lbmb/site.standard.publication/3mova7c4nho2b",
  "tags": [
    "azure static web apps",
    "azure functions",
    "bicep",
    "azure"
  ],
  "textContent": "Application Insights are a great way to monitor Azure Static Web Apps and Azure Functions. But how do you deploy that using Bicep? Let's find out!\n\n\n\nUpdated 9 March 2023\n\nI've updated this post to use the correct configuration approach for Static Web Apps with Azure Functions. Historically they used to be configured separately but that's no longer the case. You can see some discussion of this on this GitHub issue.\n\nTo be super clear; the Microsoft.Web/staticSites/config@2022-03-01 functionappsettings is deprecated. Don't use it. Use the appsettings resource alone instead.\n\nMonitoring Azure Static Web Apps\n\nThis post should possibly win some kind of \"least pithy blog title\" award. But it's definitely descriptive. Let's get into it.\n\nI recently wrote about using dynamic redirects in Azure Static Web Apps using the Azure Function they support. I wanted to monitor the redirects that were being performed. I knew I could do this with Application Insights. But how do I deploy Application Insights using Bicep?\n\nMy blog runs on Azure Static Web Apps which is deployed using Bicep. I've written about deploying Azure Static Web Apps with Bicep previously. I wanted to add Application Insights to that deployment.\n\nDeploying Application Insights with Bicep\n\nThe first thing we need to do is deploy the Application Insights workspace. This is a resource that is required for Application Insights to work. And then deploy an Application Insights resource that uses it. We can achieve that with the following appInsights.bicep Bicep module:\n\nUsing the Application Insights module\n\nWe can now use the module in our main.bicep file:\n\nThere's a few things to note here:\n\n- We have two modules. One for the Application Insights workspace and one for the Azure Static Web App.\n- The Static Web App module _depends_ on the outputs from the Application Insights module. This is because we need the id, InstrumentationKey and ConnectionString properties of the Application Insights resource.\n\nConfiguring the Azure Static Web App to use Application Insights\n\nAt this point we have something that deploys the Application Insights. The interesting part now is how we configure the Azure Static Web App to use Application Insights. We need to do that in the staticWebApp.bicep file:\n\nThere's some code here you can ignore; the part related to custom domains for instance.\n\nBut there's two relevant things to note:\n\n1. Configuring the Azure Static Web App and Azure Function to use Application Insights\n2. Connecting the Azure Static Web App to the Application Insights resource in the Azure Portal\n\n1. Configuring the Azure Static Web App and Azure Function to use Application Insights\n\nFirst of all, let's look at how we get data flowing from the Azure Static Web App and Azure Function to Application Insights:\n\nWe're setting the APPINSIGHTS_INSTRUMENTATIONKEY and APPLICATIONINSIGHTS_CONNECTION_STRING application settings on the Azure Static Web App and its associated Azure Function. These settings are what tells the Azure Static Web App and Azure Function to use Application Insights. Please note that the configuration above is _shared_ by the Azure Static Web App and Azure Function.\n\n2. Connecting the Azure Static Web App to the Application Insights resource in the Azure Portal\n\nThe other thing we need to do is to connect the Azure Static Web App to the Application Insights resource in the Azure Portal. What that means is that when you click on the Application Insights resource in the Azure Portal, you'll have a button which takes you from the Azure Static Web App in the portal to Application Insights resource:\n\nThis is done by setting the hidden-link tags on the Azure Static Web App resource. Here's how we do that:\n\nConclusion\n\nWith this in place, we can now deploy our Azure Static Web App with an Application Insights resource using Bicep and have the Azure Static Web App connected to, and providing data to, the Application Insights resource. Monitoring awaits!",
  "title": "Using Application Insights with Bicep to monitor Azure Static Web Apps and Azure Functions"
}