|
| 1 | +targetScope = 'resourceGroup' |
| 2 | + |
| 3 | +@minLength(1) |
| 4 | +@maxLength(64) |
| 5 | +@description('Name of the environment that will be used to name resources') |
| 6 | +param environmentName string |
| 7 | + |
| 8 | +@minLength(1) |
| 9 | +@description('Primary location for all resources') |
| 10 | +param location string |
| 11 | + |
| 12 | +@description('Name of the resource group') |
| 13 | +param resourceGroupName string = '' |
| 14 | + |
| 15 | +@description('Log Analytics workspace name') |
| 16 | +param logAnalyticsName string = '' |
| 17 | + |
| 18 | +@description('Application Insights dashboard name') |
| 19 | +param applicationInsightsName string = '' |
| 20 | + |
| 21 | +@description('Container Apps Environment name') |
| 22 | +param containerAppsEnvironmentName string = '' |
| 23 | + |
| 24 | +@description('Container Registry name') |
| 25 | +param containerRegistryName string = '' |
| 26 | + |
| 27 | +@description('Service name') |
| 28 | +param serviceName string = 'cnpj-api' |
| 29 | + |
| 30 | +@description('The image name for the api service') |
| 31 | +param apiImageName string = '' |
| 32 | + |
| 33 | +// Generate a unique token for resource names |
| 34 | +var resourceToken = toLower(uniqueString(subscription().id, resourceGroup().id, environmentName)) |
| 35 | + |
| 36 | +// Resource names |
| 37 | +var abbrs = loadJsonContent('abbreviations.json') |
| 38 | +var actualLogAnalyticsName = !empty(logAnalyticsName) ? logAnalyticsName : '${abbrs.operationalInsightsWorkspaces}${resourceToken}' |
| 39 | +var actualApplicationInsightsName = !empty(applicationInsightsName) ? applicationInsightsName : '${abbrs.insightsComponents}${resourceToken}' |
| 40 | +var actualContainerAppsEnvironmentName = !empty(containerAppsEnvironmentName) ? containerAppsEnvironmentName : '${abbrs.appManagedEnvironments}${resourceToken}' |
| 41 | +var actualContainerRegistryName = !empty(containerRegistryName) ? containerRegistryName : '${abbrs.containerRegistryRegistries}${resourceToken}' |
| 42 | + |
| 43 | +// Tags that should be applied to all resources. |
| 44 | +var tags = { |
| 45 | + 'azd-env-name': environmentName |
| 46 | +} |
| 47 | + |
| 48 | +// Log analytics workspace for monitoring |
| 49 | +resource logAnalytics 'Microsoft.OperationalInsights/workspaces@2022-10-01' = { |
| 50 | + name: actualLogAnalyticsName |
| 51 | + location: location |
| 52 | + tags: tags |
| 53 | + properties: { |
| 54 | + retentionInDays: 30 |
| 55 | + features: { |
| 56 | + searchVersion: 1 |
| 57 | + } |
| 58 | + sku: { |
| 59 | + name: 'PerGB2018' |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// Application insights for telemetry |
| 65 | +resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = { |
| 66 | + name: actualApplicationInsightsName |
| 67 | + location: location |
| 68 | + tags: tags |
| 69 | + kind: 'web' |
| 70 | + properties: { |
| 71 | + Application_Type: 'web' |
| 72 | + WorkspaceResourceId: logAnalytics.id |
| 73 | + Flow_Type: 'Redfield' |
| 74 | + Request_Source: 'AzureTfsExtensionAzureProject' |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// Container registry for storing container images |
| 79 | +resource containerRegistry 'Microsoft.ContainerRegistry/registries@2023-07-01' = { |
| 80 | + name: actualContainerRegistryName |
| 81 | + location: location |
| 82 | + tags: tags |
| 83 | + sku: { |
| 84 | + name: 'Basic' |
| 85 | + } |
| 86 | + properties: { |
| 87 | + adminUserEnabled: true |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// User assigned managed identity for container apps |
| 92 | +resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = { |
| 93 | + name: '${abbrs.managedIdentityUserAssignedIdentities}${resourceToken}' |
| 94 | + location: location |
| 95 | + tags: tags |
| 96 | +} |
| 97 | + |
| 98 | +// Role assignment for managed identity to pull images from container registry |
| 99 | +resource acrPullRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = { |
| 100 | + name: guid(containerRegistry.id, managedIdentity.id, 'acrPull') |
| 101 | + scope: containerRegistry |
| 102 | + properties: { |
| 103 | + roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '7f951dda-4ed3-4680-a7ca-43fe172d538d') // AcrPull |
| 104 | + principalId: managedIdentity.properties.principalId |
| 105 | + principalType: 'ServicePrincipal' |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// Container Apps environment |
| 110 | +resource containerAppsEnvironment 'Microsoft.App/managedEnvironments@2024-03-01' = { |
| 111 | + name: actualContainerAppsEnvironmentName |
| 112 | + location: location |
| 113 | + tags: tags |
| 114 | + properties: { |
| 115 | + appLogsConfiguration: { |
| 116 | + destination: 'log-analytics' |
| 117 | + logAnalyticsConfiguration: { |
| 118 | + customerId: logAnalytics.properties.customerId |
| 119 | + sharedKey: logAnalytics.listKeys().primarySharedKey |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// Container App for the API |
| 126 | +resource api 'Microsoft.App/containerApps@2024-03-01' = { |
| 127 | + name: '${abbrs.appContainerApps}api-${resourceToken}' |
| 128 | + location: location |
| 129 | + tags: union(tags, { 'azd-service-name': serviceName }) |
| 130 | + identity: { |
| 131 | + type: 'UserAssigned' |
| 132 | + userAssignedIdentities: { |
| 133 | + '${managedIdentity.id}': {} |
| 134 | + } |
| 135 | + } |
| 136 | + properties: { |
| 137 | + managedEnvironmentId: containerAppsEnvironment.id |
| 138 | + configuration: { |
| 139 | + ingress: { |
| 140 | + external: true |
| 141 | + targetPort: 8080 |
| 142 | + corsPolicy: { |
| 143 | + allowedOrigins: ['*'] |
| 144 | + allowedMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'] |
| 145 | + allowedHeaders: ['*'] |
| 146 | + allowCredentials: false |
| 147 | + } |
| 148 | + } |
| 149 | + registries: [ |
| 150 | + { |
| 151 | + server: containerRegistry.properties.loginServer |
| 152 | + identity: managedIdentity.id |
| 153 | + } |
| 154 | + ] |
| 155 | + } |
| 156 | + template: { |
| 157 | + containers: [ |
| 158 | + { |
| 159 | + image: !empty(apiImageName) ? apiImageName : 'mcr.microsoft.com/azuredocs/containerapps-helloworld:latest' |
| 160 | + name: 'api' |
| 161 | + resources: { |
| 162 | + cpu: json('0.25') |
| 163 | + memory: '0.5Gi' |
| 164 | + } |
| 165 | + } |
| 166 | + ] |
| 167 | + scale: { |
| 168 | + minReplicas: 1 |
| 169 | + maxReplicas: 10 |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +// Outputs |
| 176 | +output AZURE_LOCATION string = location |
| 177 | +output AZURE_CONTAINER_REGISTRY_ENDPOINT string = containerRegistry.properties.loginServer |
| 178 | +output AZURE_CONTAINER_REGISTRY_NAME string = containerRegistry.name |
| 179 | +output AZURE_CONTAINER_APPS_ENVIRONMENT_NAME string = containerAppsEnvironment.name |
| 180 | +output AZURE_CONTAINER_APPS_ENVIRONMENT_ID string = containerAppsEnvironment.id |
| 181 | +output AZURE_CONTAINER_APPS_ENVIRONMENT_DEFAULT_DOMAIN string = containerAppsEnvironment.properties.defaultDomain |
| 182 | +output SERVICE_API_ENDPOINT_URL string = 'https://${api.properties.configuration.ingress.fqdn}' |
| 183 | +output SERVICE_API_NAME string = api.name |
| 184 | +output RESOURCE_GROUP_ID string = resourceGroup().id |
0 commit comments