Search code examples
azureazure-functionsazure-resource-manager

Function is not creating inside Azure function App using ARM Template


I am trying to deploy the Azure Function App inside this one function using with Azure blob trigger using the ARM template.

I am able to deploy the function app and it's up and running but the function inside is not getting created.

First I created one function app and inside that create one function. I am copying all the files for that function in blob storage and providing the init.py and function.json file in appsetting

{
  "$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'))]"
      ]
    }
  ]
}

I want to create one function app with operating system as Linux and runtime as Python3.9 and inside that one function which can take the older function configuration and files from the blob storage.

I am able to create the function app shown below. Function app in Azure

Now I want to create the function inside the function app shown below. Function inside the function app

I tried to follow the documentation but I am not able to figure out how to create function inside the function app.


Solution

  • Function is not creating inside Azure function App using ARM Template: -

    After a workaround on your requirement, I found an approach to add a function inside the function App using "Microsoft.Web/sites/functions" resource type as follows.

    Code in ARM (Json) format:

    {
      "$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')))]"
          ]
        }
      ]
    }
    

    Code in bicep file format:

    @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
    ]
    }
    

    Output:

    enter image description here

    enter image description here

    enter image description here

    Refer article by @GyanBlog for more detailed structure of the template.