快速入门:使用 Bicep 创建和部署 Azure Functions 资源
在本文中,你将组合使用 Azure Functions 和 Bicep 在 Azure 中创建函数应用和相关资源。 函数应用为函数代码执行提供了执行上下文。
完成本快速入门会从你的 Azure 帐户中扣取最多几美分的费用。
Bicep 是一种特定于域的语言 (DSL),使用声明性语法来部署 Azure 资源。 它提供简明的语法、可靠的类型安全性以及对代码重用的支持。 Bicep 会针对你的 Azure 基础结构即代码解决方案提供最佳创作体验。
创建函数应用后,你可以将 Azure Functions 项目代码部署到该应用。
在开始之前,必须拥有一个包含有效订阅的 Azure 帐户。 创建帐户。
本快速入门中使用的 Bicep 文件来自 Azure 快速入门模板。
@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'
}
}
下面四个 Azure 资源由此 Bicep 文件创建:
- Microsoft.Storage/storageAccounts:创建 Functions 需要的 Azure 存储帐户。
- Microsoft.Web/serverfarms:为函数应用创建无服务器的消耗托管计划。
- Microsoft.Web/sites:创建函数应用。
- microsoft.insights/components:创建用于监视的 Application Insights 实例。
重要
存储帐户用于存储重要的应用数据,有时包括应用程序代码本身。 应限制其他应用和用户对存储帐户的访问。
将该 Bicep 文件另存为本地计算机上的 main.bicep。
使用 Azure CLI 或 Azure PowerShell 来部署该 Bicep 文件。
az group create --name exampleRG --location chinanorth2 az deployment group create --resource-group exampleRG --template-file main.bicep --parameters appInsightsLocation=<app-location>
备注
将 <app-location> 替换为 Application Insights 所在区域,这通常与资源组位置相同。
部署完成后,应会看到一条指出部署成功的消息。
使用 Azure CLI 或 Azure PowerShell 验证部署。
az resource list --resource-group exampleRG
使用上一验证步骤的输出来检索为函数应用创建的唯一名称。
打开浏览器并输入以下 URL:<https://<appName.chinacloudsites.cn>。 请确保将 <\appName> 替换为为函数应用创建的唯一名称。
访问 URL 时,应看到如下所示的页面:
若要继续执行下一步并添加 Azure 存储队列输出绑定,请保留到目前为止构建的所有资源。
否则,如果不再需要资源,请使用 Azure CLI、PowerShell 或 Azure 门户删除资源组及其资源。
az group delete --name exampleRG
现在,你已在 Azure 中创建了函数应用资源,你可以使用下列工具之一将代码部署到现有应用: