使用Powershell创建基于~~消费~~的应用服务计划 [英] Create a ~~consumption~~ based App Service Plan with Powershell

查看:53
本文介绍了使用Powershell创建基于~~消费~~的应用服务计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这个问题的答案: 是否可以使用Azure Powershell创建应用程序服务计划?

I found this answer for the question : It's possible to create App Service Plans using Azure Powershell?

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

Does anyone know how to create a CONSUMPTION app-service-plan with Azure?

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

When I look at the properties (using https://resources.azure.com/ ) of one I made (by the Gui), I see the following properties;

  },
  "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",

但是,如果我尝试(重要的部分是-层动态")

But if I try (the important part being "-Tier Dynamic")

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

我得到了例外:

异常-+无法验证参数层"上的参数.这 参数动态"不属于集合 由以下用户指定的免费,共享,基本,标准,Premium,PremiumV2" ValidateSet属性.提供集合中的参数,然后 再次尝试该命令.

Exception - + Cannot validate argument on parameter 'Tier'. The argument "Dynamic" does not belong to the set "Free,Shared,Basic,Standard,Premium,PremiumV2" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.

推荐答案

Geeze Louise.

Geeze Louise.

这已经让我折磨了三天.我终于找到了一些帮助(我提到的网址).

This has been racking me for 3 days. I finally found some help (urls I mention).

看起来(当前)使用New-AzureRmAppServicePlan无法实现.

It looks like this cannot be accomplished (currently) with New-AzureRmAppServicePlan.

但是您可以使用更通用的New-AzureRmResource.

But you can fall back onto the more generic New-AzureRmResource.

我终于可以使用的代码:

The code I finally got to work:

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

(在上面的网址中搜索"SkuName"以查找魔术线.

(search the url above for "SkuName" to find the magic lines.

请注意,这只是将INFRASTRUCTURE部署用于天蓝色函数的整体方程式的一部分(如果不使用手臂模板).当我说基础架构时,下面的代码本身不会部署azure函数,但是下面的代码将设置执行此操作所需的基础架构.

Please note, this is only part of the overall equation to deploy the INFRASTRUCTURE for azure functions (if you are not using arm templates). When I say the infrastructure, the code below does not deploy the azure-functions themselves, but the below will setup the infrastructure needed to do this.

这个人很好地解释了:

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

"clouddeveloper"基本上说,对于使用计划天蓝色功能,您需要有一个存储帐户.这与通过天蓝色门户手动添加Function-App时获得的信息"按钮对齐.

"clouddeveloper" basically says that for consumption plan azure functions, you need to have a storage account. This lines up with the "info" button you get when you manually add a Function-App via the azure portal.

支持Blob,队列和表存储的存储帐户是 必需的.使用消费计划时,功能定义为 存储在文件存储中."

"A storage account that supports Blob, Queue, and Table Storage is required. When using a Consumption plan function definitions are stored in File Storage."

因此,我的完整代码将创建一个应用程序服务计划,创建一个存储帐户,创建一个应用程序服务(这是天蓝色的功能/对应用程序友好的功能),并将存储帐户与该应用程序服务进行匹配是:

So my full code, that will create an App-Service-Plan, create a Storage Account, create an App-Service (that is azure functions/function app friendly) and match up the storage-account to the app-service is:

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模板中得到了一些提示:

I got a few hints from this ARM template as well:

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

这篇关于使用Powershell创建基于~~消费~~的应用服务计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆