-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrg_funcapp.bicep
194 lines (175 loc) · 6.16 KB
/
rg_funcapp.bicep
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// This file creates a function app
targetScope = 'resourceGroup'
@description('Unique suffix')
param suffix string = uniqueString(resourceGroup().id)
@description('The location of the resources')
param location string = resourceGroup().location
@description('The name of the function app to use')
param appName string = 'rpmfnapp${suffix}'
@description('The name of the upload directory to use')
param upload_directory string = 'upload'
@description('The repository type to create')
param repo_type string = 'distribution'
@description('Using shared keys or managed identity')
param use_shared_keys bool = true
// Storage account names must be between 3 and 24 characters, and unique, so
// generate a unique name.
@description('The name of the storage account to use')
param storage_account_name string = 'rpmrepo${suffix}'
// Choose the package container name. This will be passed to the function app.
var package_container_name = 'packages'
// Create a container for the Python code
var python_container_name = 'python'
// The version of Python to run with
var python_version = '3.11'
// The name of the hosting plan, application insights, and function app
var functionAppName = appName
var hostingPlanName = appName
var applicationInsightsName = appName
// Existing resources
resource uami 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' existing = {
name: 'uami${suffix}'
}
resource storageAccount 'Microsoft.Storage/storageAccounts@2023-01-01' existing = {
name: storage_account_name
}
resource defBlobServices 'Microsoft.Storage/storageAccounts/blobServices@2023-01-01' existing = {
parent: storageAccount
name: 'default'
}
resource packageContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2023-01-01' existing = {
parent: defBlobServices
name: package_container_name
}
resource pythonContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2023-01-01' existing = if (!use_shared_keys) {
parent: defBlobServices
name: python_container_name
}
@description('This is the built-in Storage Blob Data Contributor role. See https://learn.microsoft.com/en-gb/azure/role-based-access-control/built-in-roles#storage-blob-data-contributor')
resource storageBlobDataContributor 'Microsoft.Authorization/roleDefinitions@2022-04-01' existing = {
scope: subscription()
name: 'ba92f5b4-2d11-453d-a403-e96b0029c9fe'
}
resource storageBlobDataContributorRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' existing = {
name: guid(storageAccount.id, uami.id, storageBlobDataContributor.id)
scope: storageAccount
}
// Create a hosting plan for the function app
resource hostingPlan 'Microsoft.Web/serverfarms@2023-01-01' = {
name: hostingPlanName
location: location
sku: {
name: 'Y1'
tier: 'Dynamic'
}
properties: {
reserved: true
}
}
// Create application insights
resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
name: applicationInsightsName
location: location
kind: 'web'
properties: {
Application_Type: 'web'
Request_Source: 'rest'
}
}
// Construct the app settings
var common_settings = [
{
name: 'FUNCTIONS_EXTENSION_VERSION'
value: '~4'
}
{
name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
value: applicationInsights.properties.InstrumentationKey
}
{
name: 'FUNCTIONS_WORKER_RUNTIME'
value: 'python'
}
// Pass the blob container name to the function app - this is the
// container which is monitored for new packages.
{
name: 'BLOB_CONTAINER'
value: packageContainer.name
}
// Pass the value of the upload directory to the function app.
{
name: 'UPLOAD_DIRECTORY'
value: upload_directory
}
// Pass the value of the repository type to the function app.
{
name: 'REPO_TYPE'
value: repo_type
}
]
// Construct the application settings
// If using shared keys, include the shared key settings. Otherwise, include the managed identity settings.
var app_settings = use_shared_keys ? concat(common_settings, [
{
name: 'AzureWebJobsStorage'
value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccount.name};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
}
{
name: 'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING'
value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccount.name};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
}
{
name: 'WEBSITE_CONTENTSHARE'
value: toLower(functionAppName)
}
]) : concat(common_settings, [
{
name: 'AzureWebJobsStorage__accountName'
value: storageAccount.name
}
{
name: 'WEBSITE_RUN_FROM_PACKAGE'
value: 'https://${storageAccount.name}.blob.${environment().suffixes.storage}/${pythonContainer.name}/function_app.zip'
}
// Pass the container URL to the function app for the `from_container_url` call.
{
name: 'BLOB_CONTAINER_URL'
value: 'https://${storageAccount.name}.blob.${environment().suffixes.storage}/${packageContainer.name}/'
}
])
// Create the function app.
resource functionApp 'Microsoft.Web/sites@2023-01-01' = {
name: functionAppName
dependsOn: [storageBlobDataContributorRoleAssignment]
location: location
identity: {
type: 'SystemAssigned'
}
kind: 'functionapp,linux'
properties: {
serverFarmId: hostingPlan.id
siteConfig: {
linuxFxVersion: 'Python|${python_version}'
pythonVersion: python_version
appSettings: app_settings
ftpsState: 'FtpsOnly'
minTlsVersion: '1.2'
}
httpsOnly: true
}
}
// Grant the Function App Storage Blob Data Contributor on the storage account
// so it can access the package. Only necessary when using managed identity.
resource funcAppRole 'Microsoft.Authorization/roleAssignments@2022-04-01' = if (!use_shared_keys) {
name: guid(storageAccount.id, functionApp.id, storageBlobDataContributor.id)
scope: storageAccount
properties: {
principalId: functionApp.identity.principalId
roleDefinitionId: storageBlobDataContributor.id
principalType: 'ServicePrincipal'
}
}
// Output useful values
output function_app_name string = functionApp.name