Share via


Quickstart: Azure Functions-resources maken en implementeren met Bicep

In dit artikel gebruikt u Azure Functions met Bicep om een functie-app en gerelateerde resources in Azure te maken. De functie-app biedt een uitvoeringscontext voor het uitvoeren van uw functiecode.

Voor het voltooien van deze quickstart worden kosten van een paar dollarcent of minder in rekening gebracht bij 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 Azure Functions projectcode implementeren in die app.

Vereisten

Azure-account

Voordat u begint, moet u een Azure-account hebben met een actief abonnement. Gratis een account maken

Het Bicep-bestand controleren

Het Bicep-bestand dat in deze quickstart wordt gebruikt, is afkomstig van Azure-snelstartsjablonen.

@description('The name of the function app that you wish to create.')
param appName string = 'fnapp${uniqueString(resourceGroup().id)}'

@description('Storage Account type')
@allowed([
  'Standard_LRS'
  'Standard_GRS'
  'Standard_RAGRS'
])
param storageAccountType string = 'Standard_LRS'

@description('Location for all resources.')
param location string = resourceGroup().location

@description('Location for Application Insights')
param appInsightsLocation string

@description('The language worker runtime to load in the function app.')
@allowed([
  'node'
  'dotnet'
  'java'
])
param runtime string = 'node'

var functionAppName = appName
var hostingPlanName = appName
var applicationInsightsName = appName
var storageAccountName = '${uniqueString(resourceGroup().id)}azfunctions'
var functionWorkerRuntime = runtime

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-05-01' = {
  name: storageAccountName
  location: location
  sku: {
    name: storageAccountType
  }
  kind: 'Storage'
  properties: {
    supportsHttpsTrafficOnly: true
    defaultToOAuthAuthentication: true
  }
}

resource hostingPlan 'Microsoft.Web/serverfarms@2021-03-01' = {
  name: hostingPlanName
  location: location
  sku: {
    name: 'Y1'
    tier: 'Dynamic'
  }
  properties: {}
}

resource functionApp 'Microsoft.Web/sites@2021-03-01' = {
  name: functionAppName
  location: location
  kind: 'functionapp'
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    serverFarmId: hostingPlan.id
    siteConfig: {
      appSettings: [
        {
          name: 'AzureWebJobsStorage'
          value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
        }
        {
          name: 'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING'
          value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
        }
        {
          name: 'WEBSITE_CONTENTSHARE'
          value: toLower(functionAppName)
        }
        {
          name: 'FUNCTIONS_EXTENSION_VERSION'
          value: '~4'
        }
        {
          name: 'WEBSITE_NODE_DEFAULT_VERSION'
          value: '~14'
        }
        {
          name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
          value: applicationInsights.properties.InstrumentationKey
        }
        {
          name: 'FUNCTIONS_WORKER_RUNTIME'
          value: functionWorkerRuntime
        }
      ]
      ftpsState: 'FtpsOnly'
      minTlsVersion: '1.2'
    }
    httpsOnly: true
  }
}

resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
  name: applicationInsightsName
  location: appInsightsLocation
  kind: 'web'
  properties: {
    Application_Type: 'web'
    Request_Source: 'rest'
  }
}

De volgende vier Azure-resources worden gemaakt door dit Bicep-bestand:

Belangrijk

Het opslagaccount wordt gebruikt voor het opslaan van belangrijke app-gegevens, soms inclusief de toepassingscode zelf. U moet de toegang van andere apps en gebruikers tot het opslagaccount beperken.

Het Bicep-bestand implementeren

  1. Sla het Bicep-bestand op als main.bicep op uw lokale computer.

  2. Implementeer het Bicep-bestand met behulp van Azure CLI of Azure PowerShell.

    az group create --name exampleRG --location eastus
    az deployment group create --resource-group exampleRG --template-file main.bicep --parameters appInsightsLocation=<app-location>
    

    Notitie

    Vervang app-locatie> door< de regio voor Application Insights, die meestal hetzelfde is als de resourcegroep.

    Wanneer de implementatie is voltooid, ziet u een bericht dat aangeeft 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

Ga naar de welkomstpagina van de functie-app

  1. Gebruik de uitvoer van de vorige validatiestap om de unieke naam op te halen die voor uw functie-app is gemaakt.

  2. 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:

    Welkomstpagina van functie-app

Resources opschonen

Als u verdergaat met de volgende stap en een uitvoerbinding voor een Azure Storage-wachtrij toevoegt, kunt u alle resources het beste op dezelfde plaats laten staan. U gaat ze namelijk gebruiken in deze stap.

Als u de resources niet meer nodig hebt, gebruikt u Azure CLI, PowerShell of Azure Portal om de resourcegroep en de bijbehorende resources te verwijderen.

az group delete --name exampleRG

Volgende stappen

Nu u de resources voor de functie-app in Azure hebt gemaakt, kunt u uw code implementeren in de bestaande app met behulp van een van de volgende hulpprogramma's: