0

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
Matthijs van der Veer
  • 3,865
  • 2
  • 12
  • 22
amplifier
  • 1,793
  • 1
  • 21
  • 55

2 Answers2

1

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
}

Result:

enter image description here

Updated Answer:

Azure Portal itself does not allow the creation of another function app in the existing Consumption Plan.

It is only allowing the creation of another function apps in existing Hosting Plans of type (App Service Plan and Premium type) as you can see in below Gif Image:

enter image description here

  • But in this case Name of plan is assigned automatically. I'd like to follow my convention in naming all the resources including App Service Plan. Not just GermanyWestCentralPlan, but MyCompanyAzFunAppServicePlan. – amplifier Mar 08 '22 at 10:51
  • To add the app Service plan in consumption mode manually using PowerShell Cmdlets, check this [workaround](https://stackoverflow.com/questions/47637581/create-a-consumption-based-app-service-plan-with-powershell) –  Mar 08 '22 at 10:55
  • I can create consumption plan from PS. The problem is that I cannot then apply it to Azure Function – amplifier Mar 09 '22 at 15:02
  • Hello @amplifier, I have updated the answer with a result screenshot - As you can see the Hosting Plan (Consumption Type) is automatically created with the given code, in the same location, and also linked with that newly created function app. –  Mar 09 '22 at 16:21
  • So, why do you want to create the Consumption Hosting Plan externally and link it with the newly created Function app in consumption Mode instead? Microsoft allows/given the code to create the plan automatically if it is the consumption plan you're choosing for the Azure Function app as specified in the docs given in the answer! –  Mar 09 '22 at 16:25
  • I do want, but the PowerShell gives me error when I try to link the existing Consumption plan with Azure function via -PlanName parameter – amplifier Mar 10 '22 at 08:37
0

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.

Matthijs van der Veer
  • 3,865
  • 2
  • 12
  • 22