Bicep - Deploying Azure Application Insights
Keeping our cloud applications healthy and reliable requires constant monitoring. Azure Application Insights allows us to track performance, identify errors, and gain valuable insights into the behavior of our applications.
In this blog post, we will leverage Bicep, for deploying Azure resources, to configure Application Insights seamlessly. Remember our previous exploration of Log Analytics workspaces? We’ll take advantage of that knowledge by integrating it with Application Insights using Bicep. This powerful combination delivers a robust cloud monitoring solution. Let’s get started!
Prerequisites #
Before you start, you’ll need the following to deploy and manage resources with Bicep:
- You need Azure CLI version 2.20.0 or later to deploy Bicep files on your local machine.
- A text editor or IDE of your choice (Visual Studio Code with Bicep extension is my recommendation)
Create the Bicep files #
The first step in deploying a Bicep template is to create the Bicep file that defines your resources. Create a new file named appi.bicep. This file will contain the code needed to define and configure the deployment of your resources.
@description('Common tags for all resources.')
param commonTags object = {
project: 'jorgebernhardt.com'
bicep: 'true'
}
@description('Configuration parameters for the Log Analytics Workspace with default values.')
param logAnalyticsWorkspaceConfig object = {
name: 'defaultLogAnalyticsWorkspace'
location: resourceGroup().location
retentionInDays: 30
skuName: 'PerGB2018'
enableDataExport: 'false'
publicNetworkAccessForIngestion: 'Enabled'
publicNetworkAccessForQuery: 'Enabled'
dailyQuotaGb: -1
}
@description('Configuration parameters for the Application Insights component with default values.')
param applicationInsightsConfig object = {
name: 'defaultApplicationInsights'
kind: 'web'
applicationType: 'web'
samplingPercentage: 100
disableLocalAuth: false
}
// Resource: Log Analytics Workspace
resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2023-09-01' = {
name: logAnalyticsWorkspaceConfig.name
location: logAnalyticsWorkspaceConfig.location
tags: commonTags
properties: {
sku: {
name: logAnalyticsWorkspaceConfig.skuName
}
features:{
enableDataExport: logAnalyticsWorkspaceConfig.enableDataExport
}
retentionInDays: logAnalyticsWorkspaceConfig.retentionInDays
publicNetworkAccessForIngestion: logAnalyticsWorkspaceConfig.publicNetworkAccessForIngestion
publicNetworkAccessForQuery: logAnalyticsWorkspaceConfig.publicNetworkAccessForQuery
workspaceCapping: {
dailyQuotaGb: logAnalyticsWorkspaceConfig.dailyQuotaGb
}
}
}
// Resource: Application Insights
resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
name: applicationInsightsConfig.name
location: logAnalyticsWorkspaceConfig.location
kind: applicationInsightsConfig.kind
tags: commonTags
properties: {
Application_Type: applicationInsightsConfig.applicationType
WorkspaceResourceId: logAnalyticsWorkspace.id
SamplingPercentage: applicationInsightsConfig.samplingPercentage
DisableLocalAuth: applicationInsightsConfig.disableLocalAuth
}
}
// Output: Log Analytics Workspace Resource ID
output logAnalyticsWorkspaceId string = logAnalyticsWorkspace.id
// Output: Application Insights Resource ID
output applicationInsightsId string = applicationInsights.id
Deployment scope #
You can target your deployment to a resource group, subscription, management group, or tenant. In this case, when creating an Application Insights instance with an associated Log Analytics Workspace, an Azure resource group is needed to group all the necessary resources together. By default, when deploying a Bicep template, the scope where the resource should be deployed is a resource group.
You can use an existing Resource Group, or you can create a new Resource Group. If you want to know how to create a Resource Group using Azure CLI, check out this link.
Deploy the Bicep template using the Azure CLI #
Once your Bicep template is prepared, and you’ve selected your desired scope, you can proceed to deploy the template through the Azure CLI. To do so, execute the following commands.
Parameters #
Personalization is key to making your template reusable. With the parameters, you can easily tailor the template to your specific needs. You can use either inline parameters or a parameter file to pass parameter values. In my case, I will use a file to pass the parameters; here is an example.
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"commonTags": {
"value": {
"environment": "DEV",
"bicep": "true"
}
},
"logAnalyticsWorkspaceConfig": {
"value": {
"name": "LAW-DEMO-WE-001",
"location": "westeurope",
"retentionInDays": 30,
"skuName": "PerGB2018",
"enableDataExport": false,
"publicNetworkAccessForIngestion": "Disabled",
"publicNetworkAccessForQuery": "Disabled",
"dailyQuotaGb": 1
}
},
"applicationInsightsConfig": {
"value": {
"name": "APPI-DEMO-WE-001",
"kind": "web",
"applicationType": "web",
"samplingPercentage": 75,
"disableLocalAuth": false
}
}
}
}
Important: Please note that the parameter file stores parameter values in plain text format. If you need to include a parameter with sensitive data, it’s recommended to store the value in a secure key vault.
Preview changes #
Before deploying a Bicep file, you can preview the changes that will occur to your resources. Using what-if operations does not change existing resources; it simply shows you an output that includes color-coded results that allow you to see different changes.
az deployment group what-if \
--resource-group <resource-group-name> \
--template-file <filename>.bicep \
--parameters @<filename>.parameters.json
Deploy the Azure resource #
Finally, to deploy the template, run the following command.
az deployment group create \
--resource-group <resource-group-name> \
--template-file <filename>.bicep \
--parameters @<filename>.parameters.json
Validate the deployment #
To verify that the resource was created correctly, you can either use the Azure Portal or the Azure CLI to check the created resources and their configurations. For Azure CLI, use the following command.
az resource list \
--resource-group <resource-group-name> \
--resource-type "Microsoft.Insights/components" \
--query "[].{Name:name, Location:location, ProvisioningState:provisioningState}" \
-o table
References and useful links #
Thank you for taking the time to read my post. I sincerely hope that you find it helpful.