|
| 1 | +/** |
| 2 | + * @module apim-v1 (v.2025-09-01-preview) |
| 3 | + * @description This module defines the Azure API Management (APIM) resources using Bicep. |
| 4 | + * It includes configurations for creating and managing APIM instance. |
| 5 | + * This is version 1 (v1) of the APIM Bicep module. |
| 6 | + */ |
| 7 | + |
| 8 | +// ------------------ |
| 9 | +// PARAMETERS |
| 10 | +// ------------------ |
| 11 | + |
| 12 | +@description('The suffix to append to the API Management instance name. Defaults to a unique string based on subscription and resource group IDs.') |
| 13 | +param resourceSuffix string = uniqueString(subscription().id, resourceGroup().id) |
| 14 | + |
| 15 | +@description('The name of the API Management instance. Defaults to "apim-<resourceSuffix>".') |
| 16 | +param apiManagementName string = 'apim-${resourceSuffix}' |
| 17 | + |
| 18 | +@description('The location of the API Management instance. Defaults to the resource group location.') |
| 19 | +param location string = resourceGroup().location |
| 20 | + |
| 21 | +@description('The email address of the publisher. Defaults to "noreply@microsoft.com".') |
| 22 | +param publisherEmail string = 'noreply@microsoft.com' |
| 23 | + |
| 24 | +@description('The name of the publisher. Defaults to "Microsoft".') |
| 25 | +param publisherName string = 'Microsoft' |
| 26 | + |
| 27 | +@description('The pricing tier of this API Management service') |
| 28 | +@allowed([ |
| 29 | + 'Consumption' |
| 30 | + 'Developer' |
| 31 | + 'Basic' |
| 32 | + 'Basicv2' |
| 33 | + 'Standard' |
| 34 | + 'Standardv2' |
| 35 | + 'Premium' |
| 36 | +]) |
| 37 | +param apimSku string = 'Basicv2' |
| 38 | + |
| 39 | +@description('The type of managed identity to by used with API Management') |
| 40 | +@allowed([ |
| 41 | + 'SystemAssigned' |
| 42 | + 'UserAssigned' |
| 43 | + 'SystemAssigned, UserAssigned' |
| 44 | +]) |
| 45 | +param apimManagedIdentityType string = 'SystemAssigned' |
| 46 | + |
| 47 | +@description('The user-assigned managed identity ID to be used with API Management') |
| 48 | +param apimUserAssignedManagedIdentityId string = '' |
| 49 | + |
| 50 | +@description('Configuration array for APIM subscriptions') |
| 51 | +param apimSubscriptionsConfig array = [] |
| 52 | + |
| 53 | +@description('The Log Analytics Workspace ID for diagnostic settings') |
| 54 | +param lawId string = '' |
| 55 | + |
| 56 | +@description('The instrumentation key for Application Insights') |
| 57 | +param appInsightsInstrumentationKey string = '' |
| 58 | + |
| 59 | +@description('The resource ID for Application Insights') |
| 60 | +param appInsightsId string = '' |
| 61 | + |
| 62 | +@description('The release channel for the API Management service') |
| 63 | +@allowed([ |
| 64 | + 'Early' |
| 65 | + 'Default' |
| 66 | + 'Late' |
| 67 | + 'GenAI' |
| 68 | +]) |
| 69 | +param releaseChannel string = 'Default' |
| 70 | + |
| 71 | +// ------------------ |
| 72 | +// VARIABLES |
| 73 | +// ------------------ |
| 74 | + |
| 75 | + |
| 76 | +// ------------------ |
| 77 | +// RESOURCES |
| 78 | +// ------------------ |
| 79 | + |
| 80 | +// https://learn.microsoft.com/azure/templates/microsoft.apimanagement/service |
| 81 | +resource apimService 'Microsoft.ApiManagement/service@2025-09-01-preview' = { |
| 82 | + name: apiManagementName |
| 83 | + location: location |
| 84 | + sku: { |
| 85 | + name: apimSku |
| 86 | + capacity: 1 |
| 87 | + } |
| 88 | + properties: { |
| 89 | + publisherEmail: publisherEmail |
| 90 | + publisherName: publisherName |
| 91 | + releaseChannel: releaseChannel |
| 92 | + } |
| 93 | + identity: { |
| 94 | + type: apimManagedIdentityType |
| 95 | + userAssignedIdentities: apimManagedIdentityType == 'UserAssigned' && apimUserAssignedManagedIdentityId != '' ? { |
| 96 | + // BCP037: Not yet added to latest API: |
| 97 | + '${apimUserAssignedManagedIdentityId}': {} |
| 98 | + } : null |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +resource apimDiagnosticSettings 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = if(length(lawId) > 0) { |
| 103 | + scope: apimService |
| 104 | + name: 'apimDiagnosticSettings' |
| 105 | + properties: { |
| 106 | + workspaceId: lawId |
| 107 | + logAnalyticsDestinationType: 'Dedicated' |
| 108 | + logs: [ |
| 109 | + { |
| 110 | + categoryGroup: 'AllLogs' |
| 111 | + enabled: true |
| 112 | + } |
| 113 | + ] |
| 114 | + metrics: [ |
| 115 | + { |
| 116 | + category: 'AllMetrics' |
| 117 | + enabled: true |
| 118 | + } |
| 119 | + ] |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +resource apimLogger 'Microsoft.ApiManagement/service/loggers@2025-09-01-preview' = if(length(lawId) > 0) { |
| 124 | + parent: apimService |
| 125 | + name: 'azuremonitor' |
| 126 | + properties: { |
| 127 | + loggerType: 'azureMonitor' |
| 128 | + isBuffered: false // Set to false to ensure logs are sent immediately |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// Create a logger only if we have an App Insights ID and instrumentation key. |
| 133 | +resource apimAppInsightsLogger 'Microsoft.ApiManagement/service/loggers@2025-09-01-preview' = if (!empty(appInsightsId) && !empty(appInsightsInstrumentationKey)) { |
| 134 | + name: 'appinsights-logger' |
| 135 | + parent: apimService |
| 136 | + properties: { |
| 137 | + credentials: { |
| 138 | + instrumentationKey: appInsightsInstrumentationKey |
| 139 | + } |
| 140 | + description: 'APIM Logger for Application Insights' |
| 141 | + isBuffered: false |
| 142 | + loggerType: 'applicationInsights' |
| 143 | + resourceId: appInsightsId |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +@batchSize(1) |
| 148 | +resource apimSubscription 'Microsoft.ApiManagement/service/subscriptions@2025-09-01-preview' = [for subscription in apimSubscriptionsConfig: if(length(apimSubscriptionsConfig) > 0) { |
| 149 | + name: subscription.name |
| 150 | + parent: apimService |
| 151 | + properties: { |
| 152 | + allowTracing: true |
| 153 | + displayName: '${subscription.displayName}' |
| 154 | + scope: '/apis' |
| 155 | + state: 'active' |
| 156 | + } |
| 157 | +}] |
| 158 | + |
| 159 | + |
| 160 | +// ------------------ |
| 161 | +// OUTPUTS |
| 162 | +// ------------------ |
| 163 | + |
| 164 | +output id string = apimService.id |
| 165 | +output name string = apimService.name |
| 166 | +output principalId string = (apimManagedIdentityType == 'SystemAssigned') ? apimService.identity.principalId : '' |
| 167 | +output gatewayUrl string = apimService.properties.gatewayUrl |
| 168 | +output loggerId string = (length(lawId) > 0) ? apimLogger.id : '' |
| 169 | + |
| 170 | +#disable-next-line outputs-should-not-contain-secrets |
| 171 | +output apimSubscriptions array = [for (subscription, i) in apimSubscriptionsConfig: { |
| 172 | + name: subscription.name |
| 173 | + displayName: subscription.displayName |
| 174 | + key: apimSubscription[i].listSecrets().primaryKey |
| 175 | +}] |
0 commit comments