Friday 6 January 2023

Web App Service

Now that we have our Resource group and App service plans set up, let's create a Web App service. A web app service can contain either a traditional web application such as on made using html or an API.

It's pretty straight forward, this time go to your resource group and create the 'Web App service' from there.



Then, as before select the create drop down at the bottom of the web app card and choose new 'web app'


Now this configuration is a bit more complicated


to select the previous plan that we created, our OS and region have to match, in our previous post we configured it to be west Europe and Windows at least in the PowerShell, these two properties filter which App service plans we can connect our web app service two, meaning that an App service and it's plan must be of the same OS type as well as in the same geographic region, however they do not have to be in the same resource group.

Before we create our application, go to the monitoring tab, this is because, by default app-insights is enabled, though this can be a useful service, it can also be a pricy one depending on your app usage, so I generally disable it.


Next, click the 'Review + create' button where you do one last final sanity check, make sure your spelling and naming conventions are correct and that you have the right plan connected to your web app, double check that cause it can prove to be an expensive mistake.


Now if we create our 'Web app service' we should see it in our resource group along with our app service plan (Assuming the app service plan is in the same resource, generally I like to distinguish between my environments by resource groups). 

With that done, as aways it's PowerShell time.


# create app service
function CreateAppService {
Param(
[Parameter(Mandatory = $true)] [String] $name,
[Parameter(Mandatory = $true)] [String] $prefix,
[Parameter(Mandatory = $true)] [String] $location,
[Parameter(Mandatory = $true)] [String] $environment,
[Parameter(Mandatory = $false)] [Microsoft.Azure.Management.WebSites.Models.AppServicePlan] $servicePlan,
[Parameter(Mandatory = $false)] [String] $servicePlanName)
$rgName = "rg-$name-$environment"
$appName = "app-$prefix-$name-$environment"
Write-Host "Createing $appName web app in $rgName resource group" -ForegroundColor Magenta
#check if web app already exists
$webApp = Get-AzWebApp -ResourceGroupName $rgName -Name $appName -ErrorAction SilentlyContinue
if ($webApp) {
Write-host "Web app ""$appName"" already exists" -foregroundcolor yellow
return $webApp
}
#create web app
try {
if ($servicePlan) {
$servicePlanName = $servicePlan.Name
}
$webApp = New-AzWebApp -ResourceGroupName $rgName -Name $appName -Location $location -AppServicePlan $servicePlanName
Write-host "App service ""$appName"" Created" -foregroundcolor Green
return $webApp
}
catch {
Write-error "App service NOT created" -ErrorAction Stop
}
}

# these guids will be unique to your Azure tenant and subscription
# fyi these are randomly generated and not my actual guids
$tenantId = "ba9ff786-e26a-4a09-ac62-412a0d1f1d76"
$subId = "d8cddcf7-1050-4524-89e3-28cea221fd80"

$azContext = Get-AzContext

if (!$azContext) {
Connect-AzAccount -Tenant $tenantId -SubscriptionId $subId -UseDeviceAuthentication
}

# create resource group
CreateAppService -name pav-example -prefix api -location westeurope -environment dev -servicePlanName plan-pav-example-dev

# Disconnect active account
Disconnect-AzAccount