Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of mappen te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen om mappen te wijzigen.
In dit artikel gebruikt u Bicep om een functie-app te maken in een Flex Consumption-abonnement in Azure, samen met de vereiste Azure-resources. De functie-app biedt een serverloze uitvoeringscontext voor de uitvoering van uw functiecode. De app maakt gebruik van Microsoft Entra ID met beheerde identiteiten om verbinding te maken met andere Azure-resources.
Het voltooien van deze snelle start brengt kosten met zich mee van een paar dollarcent of minder, die in rekening worden gebracht op uw Azure-account.
Bicep is een domeinspecifieke taal (DSL) die declaratieve syntaxis gebruikt om Azure-resources te implementeren. Het biedt beknopte syntaxis, betrouwbare typeveiligheid en ondersteuning voor het hergebruik van code. Bicep biedt de beste ontwerpervaring voor uw infrastructuur als code-oplossingen in Azure.
Nadat u de functie-app hebt gemaakt, kunt u uw Azure Functions-projectcode implementeren in die app. Een laatste stap voor het implementeren van code valt buiten het bereik van dit quickstart-artikel.
Prerequisites
Azure-account
Voordat u begint, moet u een Azure-account met een actief abonnement hebben. Gratis een account maken
Het Bicep-bestand bekijken
Het Bicep-bestand dat in deze quickstart wordt gebruikt, is afkomstig van een Azure-quickstartsjabloon.
/* This Bicep file creates a function app running in a Flex Consumption plan
that connects to Azure Storage by using managed identities with Microsoft Entra ID. */
//********************************************
// Parameters
//********************************************
@description('Primary region for all Azure resources.')
@minLength(1)
param location string = resourceGroup().location
@description('Language runtime used by the function app.')
@allowed(['dotnet-isolated','python','java', 'node', 'powerShell'])
param functionAppRuntime string = 'dotnet-isolated' //Defaults to .NET isolated worker
@description('Target language version used by the function app.')
@allowed(['3.10','3.11', '7.4', '8.0', '9.0', '10', '11', '17', '20'])
param functionAppRuntimeVersion string = '8.0' //Defaults to .NET 8.
@description('The maximum scale-out instance count limit for the app.')
@minValue(40)
@maxValue(1000)
param maximumInstanceCount int = 100
@description('The memory size of instances used by the app.')
@allowed([2048,4096])
param instanceMemoryMB int = 2048
@description('A unique token used for resource name generation.')
@minLength(3)
param resourceToken string = toLower(uniqueString(subscription().id, location))
@description('A globally unique name for your deployed function app.')
param appName string = 'func-${resourceToken}'
//********************************************
// Variables
//********************************************
// Generates a unique container name for deployments.
var deploymentStorageContainerName = 'app-package-${take(appName, 32)}-${take(resourceToken, 7)}'
// Key access to the storage account is disabled by default
var storageAccountAllowSharedKeyAccess = false
// Define the IDs of the roles we need to assign to our managed identities.
var storageBlobDataOwnerRoleId = 'b7e6dc6d-f1e8-4753-8033-0f276bb0955b'
var storageBlobDataContributorRoleId = 'ba92f5b4-2d11-453d-a403-e96b0029c9fe'
var storageQueueDataContributorId = '974c5e8b-45b9-4653-ba55-5f855dd0fb88'
var storageTableDataContributorId = '0a9a7e1f-b9d0-4cc4-a60d-0319b160aaa3'
var monitoringMetricsPublisherId = '3913510d-42f4-4e42-8a64-420c390055eb'
//********************************************
// Azure resources required by your function app.
//********************************************
resource logAnalytics 'Microsoft.OperationalInsights/workspaces@2023-09-01' = {
name: 'log-${resourceToken}'
location: location
properties: any({
retentionInDays: 30
features: {
searchVersion: 1
}
sku: {
name: 'PerGB2018'
}
})
}
resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
name: 'appi-${resourceToken}'
location: location
kind: 'web'
properties: {
Application_Type: 'web'
WorkspaceResourceId: logAnalytics.id
DisableLocalAuth: true
}
}
resource storage 'Microsoft.Storage/storageAccounts@2023-05-01' = {
name: 'st${resourceToken}'
location: location
kind: 'StorageV2'
sku: { name: 'Standard_LRS' }
properties: {
accessTier: 'Hot'
allowBlobPublicAccess: false
allowSharedKeyAccess: storageAccountAllowSharedKeyAccess
dnsEndpointType: 'Standard'
minimumTlsVersion: 'TLS1_2'
networkAcls: {
bypass: 'AzureServices'
defaultAction: 'Allow'
}
publicNetworkAccess: 'Enabled'
}
resource blobServices 'blobServices' = {
name: 'default'
properties: {
deleteRetentionPolicy: {}
}
resource deploymentContainer 'containers' = {
name: deploymentStorageContainerName
properties: {
publicAccess: 'None'
}
}
}
}
resource userAssignedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
name: 'uai-data-owner-${resourceToken}'
location: location
}
resource roleAssignmentBlobDataOwner 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(subscription().id, storage.id, userAssignedIdentity.id, 'Storage Blob Data Owner')
scope: storage
properties: {
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', storageBlobDataOwnerRoleId)
principalId: userAssignedIdentity.properties.principalId
principalType: 'ServicePrincipal'
}
}
resource roleAssignmentBlob 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(subscription().id, storage.id, userAssignedIdentity.id, 'Storage Blob Data Contributor')
scope: storage
properties: {
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', storageBlobDataContributorRoleId)
principalId: userAssignedIdentity.properties.principalId
principalType: 'ServicePrincipal'
}
}
resource roleAssignmentQueueStorage 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(subscription().id, storage.id, userAssignedIdentity.id, 'Storage Queue Data Contributor')
scope: storage
properties: {
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', storageQueueDataContributorId)
principalId: userAssignedIdentity.properties.principalId
principalType: 'ServicePrincipal'
}
}
resource roleAssignmentTableStorage 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(subscription().id, storage.id, userAssignedIdentity.id, 'Storage Table Data Contributor')
scope: storage
properties: {
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', storageTableDataContributorId)
principalId: userAssignedIdentity.properties.principalId
principalType: 'ServicePrincipal'
}
}
resource roleAssignmentAppInsights 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(subscription().id, applicationInsights.id, userAssignedIdentity.id, 'Monitoring Metrics Publisher')
scope: applicationInsights
properties: {
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', monitoringMetricsPublisherId)
principalId: userAssignedIdentity.properties.principalId
principalType: 'ServicePrincipal'
}
}
//********************************************
// Function app and Flex Consumption plan definitions
//********************************************
resource appServicePlan 'Microsoft.Web/serverfarms@2024-04-01' = {
name: 'plan-${resourceToken}'
location: location
kind: 'functionapp'
sku: {
tier: 'FlexConsumption'
name: 'FC1'
}
properties: {
reserved: true
}
}
resource functionApp 'Microsoft.Web/sites@2024-04-01' = {
name: appName
location: location
kind: 'functionapp,linux'
identity: {
type: 'UserAssigned'
userAssignedIdentities: {
'${userAssignedIdentity.id}':{}
}
}
properties: {
serverFarmId: appServicePlan.id
httpsOnly: true
siteConfig: {
minTlsVersion: '1.2'
}
functionAppConfig: {
deployment: {
storage: {
type: 'blobContainer'
value: '${storage.properties.primaryEndpoints.blob}${deploymentStorageContainerName}'
authentication: {
type: 'UserAssignedIdentity'
userAssignedIdentityResourceId: userAssignedIdentity.id
}
}
}
scaleAndConcurrency: {
maximumInstanceCount: maximumInstanceCount
instanceMemoryMB: instanceMemoryMB
}
runtime: {
name: functionAppRuntime
version: functionAppRuntimeVersion
}
}
}
resource configAppSettings 'config' = {
name: 'appsettings'
properties: {
AzureWebJobsStorage__accountName: storage.name
AzureWebJobsStorage__credential : 'managedidentity'
AzureWebJobsStorage__clientId: userAssignedIdentity.properties.clientId
APPINSIGHTS_INSTRUMENTATIONKEY: applicationInsights.properties.InstrumentationKey
APPLICATIONINSIGHTS_AUTHENTICATION_STRING: 'ClientId=${userAssignedIdentity.properties.clientId};Authorization=AAD'
}
}
}
Met dit implementatiebestand worden deze Azure-resources gemaakt die nodig zijn voor een functie-app die veilig verbinding maakt met Azure-services:
- Microsoft.Web/sites: maakt uw functie-app.
- Microsoft.Web/serverfarms: maakt een serverloze Flex Consumption-hostingabonnement voor uw app.
- Microsoft.Storage/storageAccounts: maakt een Azure Storage-account dat vereist is voor Functions.
- Microsoft.Insights/components: hiermee maakt u een Application Insights-exemplaar voor het bewaken van uw app.
- Microsoft.OperationalInsights/workspaces: hiermee maakt u een werkruimte die is vereist voor Application Insights.
- Microsoft.ManagedIdentity/userAssignedIdentities: hiermee maakt u een door de gebruiker toegewezen beheerde identiteit die door de app wordt gebruikt voor verificatie met andere Azure-services met behulp van Microsoft Entra.
- Microsoft.Authorization/roleAssignments: maakt roltoewijzingen aan de door de gebruiker toegewezen beheerde identiteit, die de app toegang bieden tot minimale bevoegdheden wanneer u verbinding maakt met andere Azure-services.
Overwegingen bij de implementatie:
- Het opslagaccount wordt gebruikt om belangrijke app-gegevens op te slaan, inclusief het implementatiepakket voor toepassingscode. Met deze implementatie maakt u een opslagaccount dat wordt geopend met behulp van Microsoft Entra ID-verificatie en beheerde identiteiten. Identiteitstoegang wordt verleend op basis van minimale machtigingen.
- Het Bicep-bestand maakt standaard een C#-app die gebruikmaakt van .NET 8 in een geïsoleerd proces. Voor andere talen gebruikt u de
functionAppRuntimeenfunctionAppRuntimeVersionparameters om de specifieke taal en versie op te geven waarop uw app moet worden uitgevoerd. Zorg ervoor dat u de programmeertaal bovenaan het artikel selecteert.
Het Bicep-bestand implementeren
Sla het Bicep-bestand op als main.bicep op uw lokale computer.
Implementeer het Bicep-bestand met behulp van Azure CLI of Azure PowerShell.
az group create --name exampleRG --location <SUPPORTED_REGION> az deployment group create --resource-group exampleRG --template-file main.bicep --parameters functionAppRuntime=dotnet-isolated functionAppRuntimeVersion=8.0az group create --name exampleRG --location <SUPPORTED_REGION> az deployment group create --resource-group exampleRG --template-file main.bicep --parameters functionAppRuntime=java functionAppRuntimeVersion=17az group create --name exampleRG --location <SUPPORTED_REGION> az deployment group create --resource-group exampleRG --template-file main.bicep --parameters functionAppRuntime=node functionAppRuntimeVersion=20az group create --name exampleRG --location <SUPPORTED_REGION> az deployment group create --resource-group exampleRG --template-file main.bicep --parameters functionAppRuntime=python functionAppRuntimeVersion=3.11az group create --name exampleRG --location <SUPPORTED_REGION> az deployment group create --resource-group exampleRG --template-file main.bicep --parameters functionAppRuntime=powerShell functionAppRuntimeVersion=7.4Vervang in dit voorbeeld door
<SUPPORTED_REGION>een regio die ondersteuning biedt voor het Flex Consumption-abonnement.Wanneer de implementatie is voltooid, ziet u een bericht waarin wordt aangegeven dat de implementatie is voltooid.
De implementatie valideren
Gebruik Azure CLI of Azure PowerShell om de implementatie te valideren.
az resource list --resource-group exampleRG
Welkomstpagina van functie-app bezoeken
Gebruik de uitvoer van de vorige validatiestap om de unieke naam op te halen die voor uw functie-app is gemaakt.
Open een browser en voer de volgende URL in: <https://< appName.azurewebsites.net>. Zorg ervoor dat u \appName< vervangt door> de unieke naam die voor uw functie-app is gemaakt.
Wanneer u de URL bezoekt, ziet u een pagina zoals deze:
De hulpbronnen opschonen
Nu u een functie-app en gerelateerde resources hebt geïmplementeerd in Azure, kunt u doorgaan met de volgende stap van het publiceren van projectcode naar uw app. Gebruik anders deze opdrachten om de resources te verwijderen wanneer u ze niet meer nodig hebt.
az group delete --name exampleRG
U kunt ook resources verwijderen met behulp van Azure Portal.
Volgende stappen
U kunt nu een codeproject implementeren in de resources van de functie-app die u in Azure hebt gemaakt.
U kunt vanuit deze lokale omgevingen een codeproject maken, verifiëren en implementeren in uw nieuwe functie-app: