Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions infra/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ module apiManagement 'modules/services/api-management.bicep' = {
tags: tags
apiManagementSettings: apiManagementSettings
appInsightsName: appInsightsSettings.appInsightsName
keyVaultName: keyVaultName
}
dependsOn: [
appInsights
Expand All @@ -163,9 +164,11 @@ module assignRolesToDeployer 'modules/shared/assign-roles-to-principal.bicep' =
params: {
principalId: deployer().objectId
isAdmin: true
appInsightsName: appInsightsSettings.appInsightsName
keyVaultName: keyVaultName
}
dependsOn: [
appInsights
keyVault
]
}
Expand Down
14 changes: 14 additions & 0 deletions infra/modules/services/api-management.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ param apiManagementSettings apiManagementSettingsType
@description('The name of the App Insights instance that will be used by API Management')
param appInsightsName string

@description('The name of the Key Vault that will contain the secrets')
param keyVaultName string

//=============================================================================
// Variables
//=============================================================================
Expand Down Expand Up @@ -84,6 +87,16 @@ resource apiManagementService 'Microsoft.ApiManagement/service@2024-10-01-previe
}
}

// Assign roles to system-assigned identity of API Management

module assignRolesToApimSystemAssignedIdentity '../shared/assign-roles-to-principal.bicep' = {
params: {
principalId: apiManagementService.identity.principalId
principalType: 'ServicePrincipal'
appInsightsName: appInsightsName
keyVaultName: keyVaultName
}
}

// Store the app insights connection string in a named value

Expand All @@ -110,6 +123,7 @@ resource apimAppInsightsLogger 'Microsoft.ApiManagement/service/loggers@2024-10-
// If we would reference the connection string directly using appInsights.properties.ConnectionString,
// a new named value is created every time we execute a deployment
connectionString: '{{${appInsightsConnectionStringNamedValue.properties.displayName}}}'
identityClientId: 'SystemAssigned'
}
resourceId: appInsights.id
}
Expand Down
4 changes: 4 additions & 0 deletions infra/modules/services/app-insights.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ resource logAnalyticsWorkspace 'Microsoft.OperationalInsights/workspaces@2025-07
sku: {
name: 'PerGB2018'
}
features: {
disableLocalAuth: true // Disable Non-EntraID based Auth
}
}
}

Expand All @@ -53,5 +56,6 @@ resource appInsights 'Microsoft.Insights/components@2020-02-02' = {
publicNetworkAccessForQuery: 'Enabled'
WorkspaceResourceId: logAnalyticsWorkspace.id
RetentionInDays: appInsightsSettings.retentionInDays
DisableLocalAuth: true // Disable Non-EntraID based Auth
}
}
22 changes: 21 additions & 1 deletion infra/modules/shared/assign-roles-to-principal.bicep
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//=============================================================================
// Assign roles to principal on resources like Key Vault
// Assign roles to principal on resources like App Insights and Key Vault
//=============================================================================

//=============================================================================
Expand All @@ -15,6 +15,9 @@ param principalType string?
@description('The flag to determine if the principal is an admin or not')
param isAdmin bool = false

@description('The name of the App Insights instance on which to assign roles')
param appInsightsName string

@description('The name of the Key Vault on which to assign roles')
param keyVaultName string

Expand All @@ -26,11 +29,16 @@ var keyVaultRole string = isAdmin
? '00482a5a-887f-4fb3-b363-3b7fe8e74483' // Key Vault Administrator
: '4633458b-17de-408a-b874-0445c86b69e6' // Key Vault Secrets User

var monitoringMetricsPublisher string = '3913510d-42f4-4e42-8a64-420c390055eb' // Monitoring Metrics Publisher

//=============================================================================
// Existing Resources
//=============================================================================

resource appInsights 'Microsoft.Insights/components@2020-02-02' existing = {
name: appInsightsName
}

resource keyVault 'Microsoft.KeyVault/vaults@2025-05-01' existing = {
name: keyVaultName
}
Expand All @@ -39,6 +47,18 @@ resource keyVault 'Microsoft.KeyVault/vaults@2025-05-01' existing = {
// Resources
//=============================================================================

// Assign role Application Insights to the principal

resource assignAppInsightRolesToPrincipal 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(principalId, appInsights.id, subscriptionResourceId('Microsoft.Authorization/roleDefinitions', monitoringMetricsPublisher))
scope: appInsights
properties: {
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', monitoringMetricsPublisher)
principalId: principalId
principalType: principalType
}
}

// Assign role on Key Vault to the principal

resource assignRolesOnKeyVaultToPrincipal 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
Expand Down