使用Powershell创建基于消费的应用服务计划

9

我找到了问题的答案。

有人知道如何在Azure中创建消费应用服务计划吗?

当我查看由Gui创建的一个(使用https://resources.azure.com/),我看到以下属性;

  },
  "sku": {
    "name": "Y1",
    "tier": "Dynamic",
    "size": "Y1",
    "family": "Y",
    "capacity": 0
  }


{
  "id": "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/MyResourceGroup/providers/Microsoft.Web/serverfarms/MyHandMadeConsumptionAppServicePlan",
  "name": "MyHandMadeConsumptionAppServicePlan",
  "type": "Microsoft.Web/serverfarms",
  "kind": "functionapp",
  "location": "East US",

但如果我尝试(重点是“-Tier Dynamic”)

$plan = New-AzureRmAppServicePlan -Name 'MyPowershellCreatedAppServicePlan' -ResourceGroupName 'MyResourceGroup' -Location 'PickALocation' -Tier Dynamic

我遇到了异常:

异常 - +无法验证参数“Tier”的参数。参数“Dynamic”不属于由ValidateSet属性指定的集合“Free,Shared,Basic,Standard,Premium,PremiumV2”。提供一个集合中的参数,然后再尝试执行命令。

2个回答

8

天啊。

这让我苦恼了三天。最终我找到了一些帮助(我提到的链接)。

看起来目前无法使用New-AzureRmAppServicePlan实现此操作。

但是您可以退而求其次,使用更通用的New-AzureRmResource。

我最终成功的代码:

function SafeCreateAppServicePlan(
    [Parameter(Mandatory = $true)]
    [System.String]$location, 
    [Parameter(Mandatory = $true)]
    [System.String]$resourceGroupName,
    [Parameter(Mandatory = $true)]
    [String]$appServicePlanName 
)
{

    Write-Host "SafeCreateAppServicePlan.Parameter:location: $location"
    Write-Host "SafeCreateAppServicePlan.Parameter:resourceGroupName: $resourceGroupName"
    Write-Host "SafeCreateAppServicePlan.Parameter:appServicePlanName: $appServicePlanName"

    $SkuName = "Y1"
    $SkuTier = "Dynamic"
    $WebAppApiVersion = "2015-08-01"

    $fullObject = @{
        location = $location
        sku = @{
            name = $SkuName
            tier = $SkuTier
        }
    }

    Write-Host "Ensuring the $appServicePlanName app service plan exists"
    $plan = Get-AzureRmAppServicePlan -Name $appServicePlanName -ResourceGroupName $resourceGroupName -ErrorAction SilentlyContinue
    if(-not $plan) {
        Write-Host "Creating $appServicePlanName app service plan"
        New-AzureRmResource -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/serverfarms -Name $appServicePlanName -IsFullObject -PropertyObject $fullObject -ApiVersion $WebAppApiVersion -Force
    }
    else {
        Write-Host "$appServicePlanName app service plan already exists"   
    }

}

我得到的帮助:

https://github.com/davidebbo/AzureWebsitesSamples/blob/master/PowerShell/HelperFunctions.ps1

在上面的url中搜索"SkuName"以找到神奇的代码行。

请注意,这只是部署azure函数基础设施的整体方程式的一部分(如果您不使用arm模板)。当我说基础设施时,下面的代码并没有部署azure函数本身,但下面的代码将设置执行此操作所需的基础设施。

这个人做了一个很好的解释:

https://clouddeveloper.space/2017/10/26/deploy-azure-function-using-powershell/

"clouddeveloper" 的意思是,在使用 Azure Functions 的消耗计划时,您需要拥有一个存储帐户。这与通过 Azure 门户手动添加 Function-App 时获得的“信息”按钮相一致。
引用块中写道:“需要支持 Blob、Queue 和 Table 存储的存储帐户。在使用消耗计划函数定义时,会将其存储在文件存储中。”

enter image description here

所以我的完整代码将创建一个应用服务计划,创建一个存储账户,创建一个适用于Azure函数/函数应用的应用服务,并将存储账户与应用服务匹配起来。代码如下:
function SafeCreateAppServicePlan(
    [Parameter(Mandatory = $true)]
    [System.String]$location, 
    [Parameter(Mandatory = $true)]
    [System.String]$resourceGroupName,
    [Parameter(Mandatory = $true)]
    [String]$appServicePlanName 
)
{

    Write-Host "SafeCreateAppServicePlan.Parameter:location: $location"
    Write-Host "SafeCreateAppServicePlan.Parameter:resourceGroupName: $resourceGroupName"
    Write-Host "SafeCreateAppServicePlan.Parameter:appServicePlanName: $appServicePlanName"

    $SkuName = "Y1"
    $SkuTier = "Dynamic"
    $WebAppApiVersion = "2015-08-01"

    $fullObject = @{
        location = $location
        sku = @{
            name = $SkuName
            tier = $SkuTier
        }
    }

    Write-Host "Ensuring the $appServicePlanName app service plan exists"
    $plan = Get-AzureRmAppServicePlan -Name $appServicePlanName -ResourceGroupName $resourceGroupName -ErrorAction SilentlyContinue
    if(-not $plan) {
        Write-Host "Creating $appServicePlanName app service plan"
        New-AzureRmResource -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/serverfarms -Name $appServicePlanName -IsFullObject -PropertyObject $fullObject -ApiVersion $WebAppApiVersion -Force
    }
    else {
        Write-Host "$appServicePlanName app service plan already exists"   
    }

}

function SafeCreateAzureFunctionAppService(
    [Parameter(Mandatory = $true)]
    [System.String]$location, 
    [Parameter(Mandatory = $true)]
    [System.String]$resourceGroupName,
    [Parameter(Mandatory = $true)]
    [String]$appServicePlanName,
    [Parameter(Mandatory = $true)]
    [String]$functionAppName        
)
{

    Write-Host "SafeCreateAzureFunctionAppService.Parameter:location: $location"
    Write-Host "SafeCreateAzureFunctionAppService.Parameter:resourceGroupName: $resourceGroupName"
    Write-Host "SafeCreateAzureFunctionAppService.Parameter:appServicePlanName: $appServicePlanName"
    Write-Host "SafeCreateAzureFunctionAppService.Parameter:functionAppName: $functionAppName"

    [String]$planId = ''

    $plan = Get-AzureRmAppServicePlan -Name $appServicePlanName -ResourceGroupName $resourceGroupName -ErrorAction SilentlyContinue
    if(-not $plan) {
        throw [System.ArgumentOutOfRangeException] "Missing App Service Plan.  (ResourceGroupName='$resourceGroupName', AppServicePlan.Name = '$appServicePlanName')"
    }
    else {
        Write-Host "START AzureRmAppServicePlan Properties"   
        $plan.PSObject.Properties   
        Write-Host "END AzureRmAppServicePlan Properties"   

        #get the planId, so that can be used as the backing-app-service-plan for this AppService
        [String]$planId = $plan.Id
    }

    #wire up the necessary properties for this AppService
    $props = @{
        ServerFarmId = $planId
        }


    $functionAppResource = Get-AzureRmResource | Where-Object { $_.ResourceName -eq $functionAppName -And $_.ResourceType -eq 'Microsoft.Web/Sites' }

    if ($functionAppResource -eq $null)
    {
        New-AzureRmResource -ResourceType 'Microsoft.Web/Sites' -ResourceName $functionAppName -kind 'functionapp' -Location $location -ResourceGroupName $resourceGroupName -Properties $props -force
    }    

}


function SafeCreateStorageAccountToBackConsumptionAzureFunctions(
    [Parameter(Mandatory = $true)]
    [System.String]$location, 
    [Parameter(Mandatory = $true)]
    [System.String]$resourceGroupName,
    [Parameter(Mandatory = $true)]
    [String]$storageAccountName   
)
{

    Write-Host "SafeCreateStorageAccount.Parameter:location: $location"
    Write-Host "SafeCreateStorageAccount.Parameter:resourceGroupName: $resourceGroupName"
    Write-Host "SafeCreateStorageAccount.Parameter:storageAccountName: $storageAccountName"

    $azureRmStorageAccountGetCheck = Get-AzureRmStorageAccount -ResourceGroupName $resourceGroupName -AccountName $storageAccountName -ErrorAction SilentlyContinue

    if(-not $azureRmStorageAccountGetCheck) 
    {
        New-AzureRmStorageAccount -ResourceGroupName $resourceGroupName -AccountName $storageAccountName -Location $location -SkuName 'Standard_LRS'
    }
    else 
    {
        Write-Host "$storageAccountName storage account already exists"
    }    
}



function MatchStorageSettingsToAppService(
    [Parameter(Mandatory = $true)]
    [System.String]$location, 
    [Parameter(Mandatory = $true)]
    [System.String]$resourceGroupName,
    [Parameter(Mandatory = $true)]
    [String]$functionAppName,
    [Parameter(Mandatory = $true)]
    [String]$storageAccountName       
)
{

    Write-Host "MatchStorageSettingsToAppService.Parameter:location: $location"
    Write-Host "MatchStorageSettingsToAppService.Parameter:resourceGroupName: $resourceGroupName"
    Write-Host "MatchStorageSettingsToAppService.Parameter:functionAppName: $functionAppName"    
    Write-Host "MatchStorageSettingsToAppService.Parameter:storageAccountName: $storageAccountName"

    $keys = Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName -AccountName $storageAccountName

    $accountKey = $keys | Where-Object { $_.KeyName -eq "Key1" } | Select Value

    $storageAccountConnectionString = 'DefaultEndpointsProtocol=https;AccountName=' + $storageAccountName + ';AccountKey=' + $accountKey.Value

    $AppSettings = @{}

    $AppSettings = @{'AzureWebJobsDashboard' = $storageAccountConnectionString;

    'AzureWebJobsStorage' = $storageAccountConnectionString;

    'FUNCTIONS_EXTENSION_VERSION' = '~1';

    'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING' = $storageAccountConnectionString;

    'WEBSITE_CONTENTSHARE' = $storageAccountName;

}

    Set-AzureRMWebApp -Name $functionAppName -ResourceGroupName $resourceGroupName -AppSettings $AppSettings

}

我也从这个ARM模板中得到了一些提示:

https://github.com/Azure/azure-quickstart-templates/blob/master/101-function-app-create-dynamic/azuredeploy.json


根据SOF规则,我不能在2天内接受自己的答案。但是尽可能在可以的时候会这样做。 - granadaCoder

1

这并不是对你问题的回答,只是一些研究结果...

你会注意到,在旧版的 Microsoft.Web 模式中,没有支持 "Dynamic"。相反,我们看到了一个类似于错误消息中所看到的列表。

https://github.com/Azure/azure-resource-manager-schemas/blob/master/schemas/2015-08-01/Microsoft.Web.json#L37

在较新的架构(从2016-09-01开始),当我在resources.azure.com中查看我的Comsumption App Service Plan时,您将在computeMode下找到“Dynamic”作为有效值:https://github.com/Azure/azure-resource-manager-schemas/blob/master/schemas/2016-09-01/Microsoft.Web.json#L189 我不确定如何在PowerShell中设置computeMode值,因为文档没有显示其已公开:https://learn.microsoft.com/en-us/powershell/module/azurerm.websites/new-azurermappserviceplan?view=azurermps-5.0.0

有用的提示。感谢您抽出时间来阅读。(点赞但显然不是“答案”)。是的,我认为您找到了缺失的属性设置能力。 - granadaCoder
1
我终于弄明白了......添加了完整的答案。#不是小事。感谢你的提示,让火车继续前行。 - granadaCoder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接