Home > Back-end >  Creating of Azure function on consumption plan via PowerShell fails
Creating of Azure function on consumption plan via PowerShell fails

Time:03-09

I'm trying to create an Azure Function via PowerShell.

First I successfully did it with Basic plan

$azFunctionAppServiceObject = @{
   location = $iothubLocation
   sku = @{
        name = "B1"
        tier = "Basic"
   }
   kind = "functionapp"
   properties = @{
        reserved = "false"
        kind = "functionapp"
   }
}

$appServicePlan = Get-AzResource -ResourceGroupName $resourceGroupName -ResourceType "Microsoft.Web/serverfarms" -Name $appServicePlanName -ErrorAction SilentlyContinue

if (!$appServicePlan)
{
    $appServicePlan = New-AzResource -ResourceGroupName $resourceGroupName -ResourceType "Microsoft.Web/serverfarms" -Name $appServicePlanName -IsFullObject -PropertyObject $azFunctionAppServiceObject -Force
}
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $blobStorageAccountName -ErrorAction SilentlyContinue
if (!$storageAccount)
{
    Write-Output ">----------- Creating storage account"
    $storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $blobStorageAccountName -SkuName Standard_LRS -Location $iothubLocation -Kind StorageV2 -AccessTier Hot
}

$trigger  = Get-AzFunctionApp -ResourceGroupName $resourceGroupName `
    -Name $FunctionAppName `

if (!$trigger)
{
    Write-Output ">----------- Creating trigger"

    New-AzFunctionApp -ResourceGroupName $resourceGroupName `
        -Name $FunctionAppName `
        -PlanName $appServicePlanName `
        -StorageAccount $blobStorageAccountName `
        -Runtime Dotnet `
        -FunctionsVersion 4 `
        -RuntimeVersion 6 `
        -OSType Windows
}

But If I change the configuration object to this:

$azFunctionAppServiceObject = @{
   location = $iothubLocation
   sku = @{
        name = "Y1"
        tier = "Dynamic"
   }
   kind = "functionapp"
   properties = @{
        reserved = "false"
        kind = "functionapp"
   }
}

I get

Az.Functions.internal\New-AzFunctionApp : The server responded with a Request Error, Status: Conflict At C:\Program Files\WindowsPowerShell\Modules\Az.Functions\4.0.1\custom\New-AzFunctionApp.ps1:528 char:17
  ...             Az.Functions.internal\New-AzFunctionApp @PSBoundParameter ...
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidOperation: ({ ResourceGroup...20190801.Site }:<>f__AnonymousType0`4) [New-AzFunctionApp_Create], RestException`1
      FullyQualifiedErrorId : Conflict,Microsoft.Azure.PowerShell.Cmdlets.Functions.Cmdlets.NewAzFunctionApp_Create

CodePudding user response:

When creating an Azure Function on a consumption plan using PowerShell, you need to use a different argument to the New-AzFunctionApp call. Looking at the parameters, you can skip the -PlanName argument, and instead use the -Location argument.

There's an example on how to do this here.

CodePudding user response:

As given in this MS Doc1 and MS Doc2, PlanName is not required for the Consumption Plan and also not required to create the App Service Plan for the function App in the Consumption mode.

It means, it will create the Consumption App Service Plan automatically as described in the above references.

I have followed your code to reproduce and solved without giving the plan name as you can see in below workaround:

enter image description here

PowerShell Code:

$iothubLocation = 'Southeast Asia'
$resourceGroupName = 'hariRG'
$blobStorageAccountName = 'krishfuncblob'
$FunctionAppName = 'krishfuncapp12059'
$subscriptionId = 'xxxx-xxxx-xxxx-xxxx-xxx'


#=================Creating Azure Resource Group===============
$resourceGroup = Get-AzResourceGroup | Where-Object { $_.ResourceGroupName -eq $resourceGroupName }
if ($resourceGroup -eq $null)
{
  New-AzResourceGroup -Name $resourceGroupName -Location $location -force
}

#selecting default azure subscription by name
Select-AzSubscription -SubscriptionID $subscriptionId
Set-AzContext $subscriptionId


#========Creating Azure Storage Account========
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $blobStorageAccountName -ErrorAction SilentlyContinue


if (!$storageAccount)
{
    Write-Output ">----------- Creating storage account"
    $storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $blobStorageAccountName -SkuName Standard_LRS -Location $iothubLocation -Kind StorageV2 -AccessTier Hot
}



#========Creating Azure Function========
$trigger  = Get-AzFunctionApp -ResourceGroupName $resourceGroupName -Name $FunctionAppName

if (!$trigger)
{
    Write-Output ">----------- Creating trigger"

    New-AzFunctionApp -ResourceGroupName $resourceGroupName `
        -Name $FunctionAppName `
        -StorageAccount $blobStorageAccountName `
        -Runtime Dotnet `
        -FunctionsVersion 4 `
        -RuntimeVersion 6 `
        -OSType Windows
}
  • Related