函数未使用 ARM 模板在 Azure 函数应用程序内创建

问题描述 投票:0回答:2

我正在尝试使用 ARM 模板与 Azure blob 触发器一起使用此函数来部署 Azure Function App。

我能够部署函数应用程序并且它已启动并运行,但内部函数尚未创建。

首先,我创建了一个函数应用程序,并在其中创建了一个函数。我正在复制 blob 存储中该函数的所有文件,并在 appsetting 中提供 init.pyfunction.json

文件
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.10.61.36676",
      "templateHash": "2440974564149075183"
    }
  },
  "parameters": {
    "functionAppName": {
      "type": "string",
      "defaultValue": "test-function-name",
      "metadata": {
        "description": "The name of the Azure Function app."
      }
    },
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    },
    "appInsightsLocation": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for Application Insights"
      }
    },
    "functionWorkerRuntime": {
      "type": "string",
      "defaultValue": "python",
      "allowedValues": [
        "dotnet",
        "node",
        "python",
        "java"
      ],
      "metadata": {
        "description": "The language worker runtime to load in the function app."
      }
    },
    "linuxFxVersion": {
      "type": "string",
      "defaultValue": "python|3.9",
      "metadata": {
        "description": "Required for Linux app to represent runtime stack in the format of 'runtime|runtimeVersion'. For example: 'python|3.9'"
      }
    }
  },
  "variables": {
    "hostingPlanName": "[parameters('functionAppName')]",
    "applicationInsightsName": "[parameters('functionAppName')]",
    "storageAccountName": "[format('{0}azfunctions', uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2022-05-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "kind": "Storage"
    },
    {
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2021-02-01",
      "name": "[variables('hostingPlanName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Y1",
        "tier": "Dynamic",
        "size": "Y1",
        "family": "Y"
      },
      "properties": {
        "computeMode": "Dynamic",
        "reserved": true
      }
    },
    {
      "type": "Microsoft.Insights/components",
      "apiVersion": "2020-02-02",
      "name": "[variables('applicationInsightsName')]",
      "location": "[parameters('appInsightsLocation')]",
      "tags": {
        "[format('hidden-link:{0}', resourceId('Microsoft.Web/sites', variables('applicationInsightsName')))]": "Resource"
      },
      "properties": {
        "Application_Type": "web"
      },
      "kind": "web"
    },
    {
      "type": "Microsoft.Web/sites",
      "apiVersion": "2022-03-01",
      "name": "[parameters('functionAppName')]",
      "location": "[parameters('location')]",
      "kind": "functionapp,linux",
      "properties": {
        "reserved": true,
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
        "siteConfig": {
          "linuxFxVersion": "[parameters('linuxFxVersion')]",
          "appSettings": [
            {
              "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
              "value": "[reference(resourceId('Microsoft.Insights/components', parameters('functionAppName')), '2015-05-01').InstrumentationKey]"
            },
            {
              "name": "AzureWebJobsStorage",
              "value": "[format('DefaultEndpointsProtocol=https;AccountName={0};EndpointSuffix={1};AccountKey={2}', variables('storageAccountName'), environment().suffixes.storage, listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2019-06-01').keys[0].value)]"
            },
            {
              "name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
              "value": "[format('DefaultEndpointsProtocol=https;AccountName={0};EndpointSuffix={1};AccountKey={2}', variables('storageAccountName'), environment().suffixes.storage, listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2019-06-01').keys[0].value)]"
            },
            {
              "name": "WEBSITE_CONTENTSHARE",
              "value": "[toLower(parameters('functionAppName'))]"
            },
            {
              "name": "FUNCTIONS_EXTENSION_VERSION",
              "value": "~4"
            },
            {
              "name": "FUNCTIONS_WORKER_RUNTIME",
              "value": "[parameters('functionWorkerRuntime')]"
            },
            {
              "name": "SCM_DO_BUILD_DURING_DEPLOYMENT",
              "value": "true"
            }
          ]
        },
        "apiDefinition": 
        {
            "value": [
            {
              "name": "test-trigger-function",
              "type": "function",
              "properties": {
                  "script_href": "blob_url/folder/__init__.py",
                  "script_root_path_href": "blob_url/folder/",
                  "config_href": "blob_url/folder/function.json",
                  "config": {},
                  "language": "python",
                  "isDisabled": false
              }
            }
          ]
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
        "[resourceId('Microsoft.Insights/components', variables('applicationInsightsName'))]",
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
      ]
    }
  ]
}

我想创建一个函数应用程序,其操作系统为 Linux,运行时为 Python3.9,并且在该函数内部可以从 blob 存储中获取较旧的函数配置和文件。

我能够创建如下所示的函数应用程序。

现在我想在如下所示的函数应用程序中创建函数。

我尝试遵循文档,但我无法弄清楚如何在函数应用程序中创建函数。

azure azure-functions azure-resource-manager
2个回答
1
投票

函数未使用 ARM 模板在 Azure 函数应用程序内创建:-

在解决您的要求后,我找到了一种使用

"Microsoft.Web/sites/functions"
资源类型在函数 App 内添加函数的方法,如下所示。

ARM(Json)格式的代码:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.19.5.34762",
      "templateHash": "1037450281519309886"
    }
  },
  "parameters": {
    "functionAppName": {
      "type": "string",
      "defaultValue": "newxxx",
      "metadata": {
        "description": "The name of the Azure Function app."
      }
    },
    "storageAccountName": {
      "type": "string",
      "metadata": {
        "description": "The name of the storage account that you wish to use."
      }
    },
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    },
    "blobContainerName": {
      "type": "string",
      "metadata": {
        "description": "The name of the blob container that you wish to use."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    },
    "appInsightsLocation": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for Application Insights"
      }
    },
    "functionWorkerRuntime": {
      "type": "string",
      "defaultValue": "python",
      "allowedValues": [
        "dotnet",
        "node",
        "python",
        "java"
      ],
      "metadata": {
        "description": "The language worker runtime to load in the function app."
      }
    },
    "functionName": {
      "type": "string",
      "metadata": {
        "description": "The name of the function that you wish to create."
      }
    },
    "linuxFxVersion": {
      "type": "string",
      "defaultValue": "python|3.9",
      "metadata": {
        "description": "Required for Linux app to represent runtime stack in the format of 'runtime|runtimeVersion'. For example: 'python|3.9'"
      }
    }
  },
  "variables": {
    "hostingPlanName": "[parameters('functionAppName')]",
    "applicationInsightsName": "[parameters('functionAppName')]",
    "storageAccountName_var": "[format('{0}azfunctions', uniqueString(resourceGroup().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2022-05-01",
      "name": "'storage${uniqueString(resourceGroup().id)}'",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "kind": "Storage"
    },
    {
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2021-02-01",
      "name": "[variables('hostingPlanName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Y1",
        "tier": "Dynamic",
        "size": "Y1",
        "family": "Y"
      },
      "properties": {
        "computeMode": "Dynamic",
        "reserved": true
      }
    },
    {
      "type": "Microsoft.Insights/components",
      "apiVersion": "2020-02-02",
      "name": "[variables('applicationInsightsName')]",
      "location": "[parameters('appInsightsLocation')]",
      "tags": {
        "[format('hidden-link:{0}', resourceId('Microsoft.Web/sites', variables('applicationInsightsName')))]": "Resource"
      },
      "properties": {
        "Application_Type": "web"
      },
      "kind": "web"
    },
    {
      "type": "Microsoft.Web/sites",
      "apiVersion": "2022-03-01",
      "name": "[parameters('functionAppName')]",
      "location": "[parameters('location')]",
      "kind": "functionapp,linux",
      "properties": {
        "reserved": true,
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
        "siteConfig": {
          "linuxFxVersion": "[parameters('linuxFxVersion')]",
          "appSettings": [
            {
              "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
              "value": "[reference(resourceId('Microsoft.Insights/components', parameters('functionAppName')), '2015-05-01').InstrumentationKey]"
            },
            {
              "name": "AzureWebJobsStorage",
              "value": "[format('DefaultEndpointsProtocol=https;AccountName={0};EndpointSuffix={1};AccountKey={2}', variables('storageAccountName_var'), environment().suffixes.storage, listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName_var')), '2019-06-01').keys[0].value)]"
            },
            {
              "name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
              "value": "[format('DefaultEndpointsProtocol=https;AccountName={0};EndpointSuffix={1};AccountKey={2}', variables('storageAccountName_var'), environment().suffixes.storage, listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName_var')), '2019-06-01').keys[0].value)]"
            },
            {
              "name": "WEBSITE_CONTENTSHARE",
              "value": "[toLower(parameters('functionAppName'))]"
            },
            {
              "name": "FUNCTIONS_EXTENSION_VERSION",
              "value": "~4"
            },
            {
              "name": "FUNCTIONS_WORKER_RUNTIME",
              "value": "[parameters('functionWorkerRuntime')]"
            },
            {
              "name": "SCM_DO_BUILD_DURING_DEPLOYMENT",
              "value": "true"
            }
          ]
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]"
      ]
    },
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-08-01",
      "name": "[parameters('storageAccountName')]",
      "location": "[parameters('location')]",
      "tags": "sds",
      "sku": {
        "name": "Premium_LRS"
      },
      "kind": "[parameters('storageAccountType')]"
    },
    {
      "type": "Microsoft.Storage/storageAccounts/blobServices",
      "apiVersion": "2021-06-01",
      "name": "[format('{0}/{1}', parameters('storageAccountName'), 'default')]",
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName'))]"
      ]
    },
    {
      "type": "Microsoft.Storage/storageAccounts/blobServices/containers",
      "apiVersion": "2019-06-01",
      "name": "[format('{0}/{1}/{2}', parameters('storageAccountName'), 'default', format('{0}default{1}', parameters('storageAccountName'), parameters('blobContainerName')))]",
      "properties": {
        "publicAccess": "None"
      },
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts/blobServices', parameters('storageAccountName'), 'default')]"
      ]
    },
    {
      "type": "Microsoft.Web/sites/functions",
      "apiVersion": "2018-02-01",
      "name": "[format('{0}/{1}', parameters('functionAppName'), format('{0}', parameters('functionName')))]",
      "properties": {
        "config": {
          "bindings": [
            {
              "name": "myBlob",
              "type": "blobTrigger",
              "direction": "in",
              "path": "[format('default/{0}/{{name}}', parameters('blobContainerName'))]",
              "connection": "AzureWebJobsStorage"
            }
          ],
          "disabled": false
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.Web/sites', parameters('functionAppName'))]",
        "[resourceId('Microsoft.Storage/storageAccounts/blobServices/containers', parameters('storageAccountName'), 'default', format('{0}default{1}', parameters('storageAccountName'), parameters('blobContainerName')))]"
      ]
    }
  ]
}

二头肌文件格式的代码:

@description('The name of the Azure Function app.')
param  functionAppName  string = 'newjappsun'
@description('The name of the storage account that you wish to use.')
param  storageAccountName  string = 'latestjstore'
@description('Storage Account type')
@allowed([
'Standard_LRS'
'Standard_GRS'
'Standard_RAGRS'
])
param  storageAccountType  string = 'Standard_LRS'
@description('The name of the blob container that you wish to use.')
param  blobContainerName  string = 'new'
@description('Location for all resources.')
param  location  string = resourceGroup().location
@description('Location for Application Insights')
param  appInsightsLocation  string = resourceGroup().location
@description('The language worker runtime to load in the function app.')
@allowed([
'dotnet'
'node'
'python'
'java'
])
param  functionWorkerRuntime  string = 'python'
@description('The name of the function that you wish to create.')
param  functionName  string = 'newfuncj'
@description('Required for Linux app to represent runtime stack in the format of \'runtime|runtimeVersion\'. For example: \'python|3.9\'')
param  linuxFxVersion  string = 'python|3.9'
var  hostingPlanName = functionAppName
var  applicationInsightsName = functionAppName
resource  hostingPlan  'Microsoft.Web/serverfarms@2021-02-01' = {
name: hostingPlanName
location: location
sku: {
name: 'Y1'
tier: 'Dynamic'
size: 'Y1'
family: 'Y'
}
properties: {
computeMode: 'Dynamic'
reserved: true
}
}
resource  applicationInsights  'Microsoft.Insights/components@2020-02-02' = {
name: applicationInsightsName
location: appInsightsLocation
tags: {
'hidden-link:${resourceId('Microsoft.Web/sites',  applicationInsightsName)}': 'Resource'
}
properties: {
Application_Type: 'web'
}
kind: 'web'
}
resource  functionApp 'Microsoft.Web/sites@2022-03-01' = {
name: functionAppName
location: location
kind: 'functionapp,linux'
properties: {
reserved: true
serverFarmId: hostingPlan.id
siteConfig: {
linuxFxVersion: linuxFxVersion
appSettings: [
{
name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
value: reference(resourceId('Microsoft.Insights/components',  functionAppName),  '2015-05-01').InstrumentationKey
}
{
name: 'AzureWebJobsStorage'
value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${listKeys(resourceId('Microsoft.Storage/storageAccounts',  storageAccountName),  '2019-06-01').keys[0].value}'
}
{
name: 'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING'
value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${listKeys(resourceId('Microsoft.Storage/storageAccounts',  storageAccountName),  '2019-06-01').keys[0].value}'
}
{
name: 'WEBSITE_CONTENTSHARE'
value: toLower(functionAppName)
}
{
name: 'FUNCTIONS_EXTENSION_VERSION'
value: '~4'
}
{
name: 'FUNCTIONS_WORKER_RUNTIME'
value: functionWorkerRuntime
}
{
name: 'SCM_DO_BUILD_DURING_DEPLOYMENT'
value: 'true'
}
]
}
}
}
resource  storageAccount 'Microsoft.Storage/storageAccounts@2021-08-01' = {
name: storageAccountName
location: location
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
}
resource  blobService 'Microsoft.Storage/storageAccounts/blobServices@2021-06-01' = {
name: 'default'
parent: storageAccount
}
resource  storageAccountName_default_blobContainer  'Microsoft.Storage/storageAccounts/blobServices/containers@2019-06-01' = {
name: '${storageAccountName}default${blobContainerName}'
parent: blobService
properties: {
publicAccess: 'None'
}
}
resource  functionAppName_function 'Microsoft.Web/sites/functions@2018-02-01' = {
parent: functionApp
name: '${functionName}'
properties: {
config: {
bindings: [
{
name: 'myBlob'
type: 'blobTrigger'
direction: 'in'
path: 'default/${blobContainerName}/{name}'
connection: 'AzureWebJobsStorage'
}
]
disabled: false
}
}
dependsOn: [
storageAccountName_default_blobContainer
]
}

输出:

enter image description here

enter image description here

enter image description here

请参阅@GyanBlog 的文章,了解模板的更详细结构。


0
投票

在问答环节之后,似乎无法安装依赖项。我也正处于类似的探索阶段。您找到解决这个问题的方法了吗?如果您能分享@Ravi Kant Gautam 和@Jahnavi

的任何见解,我将不胜感激
© www.soinside.com 2019 - 2024. All rights reserved.