Azure Application Insights uses ApplicationInsights.config where we supposed to provide Your Key and may be some other parameters. The problem is the key is not constant and not known until the deployment time. So we need to have a script to go into given Subscription and Resource Group, find Application Insights instance and load its InstrumentationKey and then use it for variable substitution when web site is deployed.
I see multiple problems here: 1. How to authenticate and run Azure script when Subscription and Resource Group are strings only known at deployment time? 2. Where to load found InstrumentationKey so it is usable for config variable substitution?
Thanks for getting in touch.
On your first issue for authenticating, you can authenticate with Azure directly in the PowerShell script using values hardcoded in the script or passed in via project variables. You can see an example of how to do this in the Calamari Azure Context code here.
If you are running 2018.5.x, you can also set up the account to connect to Azure via a variable, which allows you scope the account for the step to be different per environment/tenant. Additionally, when you use an Azure Account variable, each of the properties of the account will be available as variables in the script. Turn on
OctopusPrintVariables to see all the variable values available.
Another option could be to use a different account for the App Insights query that has access to all subscriptions. Within the script you can select the appropriate subscription based on a variable, which could be prompted, by using the command:
Select-AzureRmSubscription -SubscriptionName $subscriptionName
Select-AzureRmSubscription -SubscriptionId $subscriptionId
You can query the Application Insights Key using the following PowerShell and then set the Instrumentation Key as an Output Variable:
$appInsights = Get-AzureRmApplicationInsights -Name $AppInsightsName -ResourceGroupName $ResourceGroupName
Set-OctopusVariable -name "InstrumentationKey" -value $appInsights.InstrumentationKey
The variable created by the
Set-OctopusVariable line above will be available in later steps in both a PowerShell script as
$OctopusParameters["Octopus.Action[StepA].Output.InstrumentationKey"] or in variable substitution syntax as
You can then replace the value in your config file for the Instrumentation Key with this variable substitution and the value will be updated when you turn on the
Configuration Variables feature on your application deployment step.
Hopefully this has covered off all your questions. Feel free to get back to me if you have further issues.