Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Azure website with Standard pricing tier via Powershell

I'm trying to fix my continious deployment scenario and for this to work an Azure website has to exist and be able to swap between the Staging and Production. We want this website to work in the Standard pricing tier.

The script I have at the moment creates a new ResourceGroup, Hosting Plan and after these are created, the website itself. The problem I'm facing is the website is always in Free mode. I should be able to fix this by using the Set-AzureResource cmdlet, but this one is throwing an message telling me I should specify the Location. Problem with this is this specific cmdlet doesn't has a Location parameter.

Set-AzureResource : {
  "Error": {
    "Code": "LocationRequired",
    "Message": "The location property is required for this definition.",
    "Target": null,
    "Details": null
  }
}

This is the (simplified) script I'm using to create all of the resources:

#Create the resource group
New-AzureResourceGroup -Location $location -Name $resourceGroupName -Force

#Create the hosting plan
$hostingPlanParameters = @{"name" = $hostingPlanName; "sku" = "Standard"; "computeMode" = "Standard"; "workerSize" = "0"; "numberOfWorkers" = "1"}

New-AzureResource -ApiVersion 2014-04-01 -Name $hostingPlanName -ResourceGroupName $resourceGroupName `
                            -ResourceType Microsoft.Web/serverFarms -Location $location `
                            -PropertyObject $hostingPlanParameters -Verbose -Force
#Create the website
$analyticsSite = @{"sku" = "Standard"; "webHostingPlan" = $hostingplan; "computeMode" = "Standard"; }
New-AzureResource  -Name $label -ResourceType Microsoft.Web/sites `
                             -ResourceGroupName $resourceGroupName -Location $location `
                             -ApiVersion $apiVersion -PropertyObject $analyticsSite -Force

Set-AzureResource -Name $label -ResourceType Microsoft.Web/sites `
                             -ResourceGroupName $resourceGroupName -ApiVersion $apiVersion `
                             -PropertyObject $analyticsSite -Force

I've read the website should inherit the sku of the specified hosting plan, so I should not need to update it. This does not appear to work for my above script. The hosting plan is specified, but the settings aren't inheritted.

The created hosting plan properties look like this:

PropertiesText    : {
                      "name": "devHostingPlanWestEU10",
                      "sku": "Standard",
                      "workerSize": 0,
                      "workerSizeId": 0,
                      "numberOfWorkers": 1,
                      "currentWorkerSize": 0,
                      "currentWorkerSizeId": 0,
                      "currentNumberOfWorkers": 1,
                      "status": 0,
                      "webSpace": "ResourceGroupWestEU10-WestEuropewebspace",
                      "subscription": "ad7add9b-8b7a-45df-8e95-0e7fccbr78a5",
                      "adminSiteName": null,
                      "hostingEnvironment": null,
                      "maximumNumberOfWorkers": 0,
                      "planName": null,
                      "perSiteScaling": null,
                      "hostingEnvironmentId": null
                    }

This looks kind of ok to me. Once the website is created, these properties are printed:

PropertiesText    : {
                      "name": "Testert10",
                      "state": "Running",
                      "hostNames": [
                        "testert10.azurewebsites.net"
                      ],
                      "webSpace": "ResourceGroupWestEU10-WestEuropewebspace",
                      ...
                      "repositorySiteName": "Testert10",
                      "owner": null,
                      "usageState": 0,
                      "enabled": true,
                      ...
                      "computeMode": null,
                      "serverFarm": "Default1",
                      "serverFarmId": null,
                      "lastModifiedTimeUtc": "2015-05-21T11:52:30.773",
                      "storageRecoveryDefaultState": "Running",
                      "contentAvailabilityState": 0,
                      "runtimeAvailabilityState": 0,
                      "siteConfig": null,
                      "deploymentId": "Testert10",
                      "trafficManagerHostNames": null,
                      "sku": "Free",
                      "premiumAppDeployed": null,
                      "scmSiteAlsoStopped": false,
                      "targetSwapSlot": null,
                      "hostingEnvironment": null,
                      "microService": "WebSites",
                      "gatewaySiteName": null,
                      "kind": null,
                      "cloningInfo": null,
                      "hostingEnvironmentId": null
                    }

As you can see, the computeMode, serverFarm, hostingEnvironment and sku aren't set with the properties I set in the $analyticsSite object.

Therefore I probably need to update the resource, but this throws the error mentioned above.

I've also tried using the New-AzureWebsite, using Troy Hunt's blogpost as an example. However, this post also relies on using the Set-AzureResource, so I'll fall into the same problem. A different problem with this example is you can't control on which resource group and hosting plan the site is created which will cause a bit of trouble when searching for the site.

like image 795
Jan_V Avatar asked May 21 '15 12:05

Jan_V


People also ask

What is the Azure PowerShell command for creating a new web app?

Description. The New-AzWebApp cmdlet creates an Azure Web App in a given a resource group that uses the specified App Service plan and data center.

What is Webspace in Azure?

Azure deploys each new App Service plan into a deployment unit, internally called a webspace. Each region can have many webspaces, but your app can only move between plans that are created in the same webspace.


1 Answers

This is possible so easily in the new RM cmdlets. Make sure that you have the latest version of Azure PowerShell first.

First create an App service plan that defines the Standard Price tier, then create a web app with the app service plan.

function Create-AppServicePlan()
{
    #https://msdn.microsoft.com/en-us/library/mt619306.aspx
    $resource = Find-AzureRmResource -ResourceNameContains $ServicePlanName -ResourceGroupNameContains $ResourceGroupName -ResourceType "Microsoft.Web/ServerFarms"
    if(!$resource)
    {
        # Specify the Tier type that you would like
        $servicePlan = New-AzureRmAppServicePlan -ResourceGroupName $ResourceGroupName -Name $ServicePlanName -Location $WebAppLocation -Tier Standard -NumberofWorkers 1 -WorkerSize "Small"
    }
}

Next create the web app with the app service plan as a parameter.

function Create-AzureRmWebApp()
{
    #https://msdn.microsoft.com/en-us/library/mt619250.aspx
    $resource = Find-AzureRmResource -ResourceNameContains $WebAppName -ResourceGroupNameContains $ResourceGroupName -ResourceType "Microsoft.Web/sites"
    if(!$resource)
    {
        $webApp = New-AzureRmWebApp -ResourceGroupName $ResourceGroupName -Name $WebAppName -Location $WebAppLocation -AppServicePlan $ServicePlanName
    }
}

This is the full working script that is verified working.

$ServicePlanName = "PSScriptAppServicePlann"
$WebAppName = "WebAppByPSlooksCool"
$ResourceGroupName = "MyResourceGroup"
$WebAppLocation = "australiaeast"
$ErrorActionPreference = "Stop"

# Step 1: Create the application service plan
Create-AppServicePlan

# Step 2: Create the web app using the service plan name.
Create-AzureRmWebApp

function Create-AzureRmWebApp()
{
    #https://msdn.microsoft.com/en-us/library/mt619250.aspx
    $resource = Find-AzureRmResource -ResourceNameContains $WebAppName -ResourceGroupNameContains $ResourceGroupName -ResourceType "Microsoft.Web/sites"
    if(!$resource)
    {
        $webApp = New-AzureRmWebApp -ResourceGroupName $ResourceGroupName -Name $WebAppName -Location $WebAppLocation -AppServicePlan $ServicePlanName
    }
}

function Create-AppServicePlan()
{
    #https://msdn.microsoft.com/en-us/library/mt619306.aspx
    $resource = Find-AzureRmResource -ResourceNameContains $ServicePlanName -ResourceGroupNameContains $ResourceGroupName -ResourceType "Microsoft.Web/ServerFarms"
    if(!$resource)
    {
        # Specify the Tier type that you would like
        $servicePlan = New-AzureRmAppServicePlan -ResourceGroupName $ResourceGroupName -Name $ServicePlanName -Location $WebAppLocation -Tier Standard -NumberofWorkers 1 -WorkerSize "Small"
    }
}
like image 100
Ahmed IG Avatar answered Sep 30 '22 10:09

Ahmed IG