PowerShell Automation for Azure App Service Deployment - Marcin Gastol
15651
post-template-default,single,single-post,postid-15651,single-format-standard,bridge-core-3.0.7,bridge,qode-page-transition-enabled,ajax_fade,page_not_loaded,,qode-title-hidden,qode-child-theme-ver-1.0.0,qode-theme-ver-29.4,qode-theme-bridge,qode_header_in_grid,wpb-js-composer js-comp-ver-6.10.0,vc_responsive
Full 3
Marcin Gastol
DevOps • Azure • Python • AI & ML • Security

Full 3

PowerShell Automation for Azure App Service Deployment

Introduction to Azure App Service

PaaS stands for platform as a service, and Azure App Service from Microsoft Azure is a fully managed version of this model. It is intended to assist developers in the process of building, deploying and scaling applications without placing the weight of responsibility on the developers to manage the underlying infrastructure. It is capable of hosting web apps in addition to REST APIs, and it supports a wide variety of programs, including Java, Node.js, Python, Ruby, and.NET as well as.NET and.NET Core.

Scalability is without a doubt the strongest selling point of Microsoft’s Azure App Service. It makes it possible for applications to automatically scale up or down based on the volume of traffic, ensuring that applications are constantly responsive even when they are experiencing high load periods.

Let’s have a look at how to automate the deployment of Azure App Service by utilizing PowerShell now that we’ve covered the basics of the service.

Automating Azure App Service Deployment using PowerShell

PowerShell is a task-based command-line shell and scripting language built on .NET. Automating deployment using PowerShell scripts can greatly reduce the risk of human errors and increase efficiency.

Let’s consider a scenario where Garson IT company wants to deploy multiple App Services under different App Service Plans, all within the same resource group. This could be beneficial if different apps have different scaling or runtime requirements. This script provides Garson IT with a fully automated process to deploy and manage multiple web apps with different requirements in Azure App Service and facilitates seamless promotion of the staging app to production.

PowerShell
# Define common parameters
$resourceGroupName = 'GarsonResourceGroup'
$location = 'West Europe'
$productionPlanName = 'GarsonPremiumPlan'
$stagingPlanName = 'GarsonFreePlan'
$productionAppName = 'GarsonProdApp'
$stagingAppName = 'GarsonStagingApp'
$stagingSlotName = 'Staging'
$archivePath = 'staging_site.zip'

# Log in to Azure
Connect-AzAccount

# Create a resource group
New-AzResourceGroup -Name $resourceGroupName -Location $location

# Create a premium App Service plan for the production web app
New-AzAppServicePlan -ResourceGroupName $resourceGroupName -Name $productionPlanName -Location $location -Tier 'PremiumV2'

# Create a free App Service plan for the staging web app
New-AzAppServicePlan -ResourceGroupName $resourceGroupName -Name $stagingPlanName -Location $location -Tier 'Free'

# Create a production web app in the premium App Service plan
New-AzWebApp -ResourceGroupName $resourceGroupName -Name $productionAppName -Location $location -AppServicePlan $productionPlanName

# Create a staging web app in the free App Service plan
New-AzWebApp -ResourceGroupName $resourceGroupName -Name $stagingAppName -Location $location -AppServicePlan $stagingPlanName

# Set up deployment slots for the production web app
New-AzWebAppSlot -ResourceGroupName $resourceGroupName -Name $productionAppName -Slot $stagingSlotName

# Deploy the staging web app to the production web app's staging slot
Publish-AzWebApp -ResourceGroupName $resourceGroupName -Name $productionAppName -Slot $stagingSlotName -ArchivePath $archivePath

# Swap the staging slot with the production slot
Switch-AzWebAppSlot -ResourceGroupName $resourceGroupName -Name $productionAppName -SourceSlotName $stagingSlotName -DestinationSlotName 'Production'

In this script:

  • We first connect to Azure using Connect-AzAccount.
  • We create a new resource group ‘GarsonResourceGroup’ in the ‘West Europe’ region.
  • We create a new premium App Service Plan ‘GarsonPremiumPlan’ in the ‘GarsonResourceGroup’. The premium tier supports more advanced features and greater scalability than the free tier.
  • We create a free App Service Plan ‘GarsonFreePlan’ in the same resource group. The free tier can be used for development and testing.
  • We create a new Web App ‘GarsonProdApp’ under the ‘GarsonPremiumPlan’ and another Web App ‘GarsonStagingApp’ under the ‘GarsonFreePlan’.
  • We then set up a staging slot for the ‘GarsonProdApp’. Deployment slots allow developers to test their apps in the production environment without affecting the live app
  • We deploy the ‘GarsonStagingApp’ to the staging slot of the ‘GarsonProdApp’. Here we assume that ‘staging_site.zip’ contains the web app content for the staging site.
  • Finally, we swap the staging slot with the production slot, effectively promoting the staging app to production.

Bis

Azure App Service offers many other configurations that can be set up using PowerShell. Let’s add to our script to configure a few of these settings for our ‘GarsonProdApp’:

  • App Settings and Connection Strings
  • Custom Domain and HTTPS
  • Auto Scaling

Remember to update the connection string, custom domain and SSL certificate thumbprint to match your actual settings. This more complex script ensures that Garson IT’s production web app is scalable, secure and ready to connect to databases or other services.

PowerShell
# Define common parameters
$resourceGroupName = 'GarsonResourceGroup'
$appName = 'GarsonProdApp'
$domainName = 'www.garsonit.com'
$appServicePlanName = 'GarsonPremiumPlan'

# Define App Settings
$appSettings = @{
    'APP_SETTING_NAME' = 'AppSettingValue'
}

# Define Connection Strings
$connectionStrings = @(
    New-AzWebAppConnectionString -Name 'ConnectionStringName' -Type SQLAzure -Value 'Server=tcp:your_server.database.windows.net;Database=your_db;User ID=your_username;Password=your_password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;'
)

# Set app settings and connection strings
Set-AzWebApp -ResourceGroupName $resourceGroupName -Name $appName -AppSettings $appSettings -ConnectionStrings $connectionStrings

# Configure custom domain
Set-AzWebApp -ResourceGroupName $resourceGroupName -Name $appName -HostNames @($domainName)

# Bind SSL certificate for HTTPS
$thumbprint = (Get-AzWebAppCertificate -ResourceGroupName $resourceGroupName).Thumbprint
New-AzWebAppSSLBinding -ResourceGroupName $resourceGroupName -WebAppName $appName -Name $domainName -Thumbprint $thumbprint -SslState 'SniEnabled'

# Configure auto scaling based on CPU percentage
$targetResourceId = "/subscriptions/{subscriptionId}/resourceGroups/$resourceGroupName/providers/Microsoft.Web/serverfarms/$appServicePlanName"
$metricResourceId = "/subscriptions/{subscriptionId}/resourceGroups/$resourceGroupName/providers/Microsoft.Web/serverfarms/$appServicePlanName"

$autoScaleSetting = New-AzAutoscaleSetting -Location 'West Europe' -Name 'AutoScaleSetting' -ResourceGroupName $resourceGroupName -TargetResourceId $targetResourceId

$autoScaleRule = New-AzAutoscaleRule -MetricName 'CpuPercentage' -MetricResourceId $metricResourceId -Operator 'GreaterThan' -Threshold 70 -TimeGrain 00:05:00 -ScaleActionCooldown 00:05:00 -ScaleActionDirection 'Increase' -ScaleActionValue '1' -TimeWindowStart '00:00:00' -TimeWindowEnd '23:59:59'

$autoScaleProfile = New-AzAutoscaleProfile -Name 'Profile1' -DefaultCapacity 1 -MaximumCapacity 3 -MinimumCapacity 1 -Rule $autoScaleRule

$autoScaleSetting.Profiles.Add($autoScaleProfile)
$autoScaleSetting | Add-AzAutoscaleSetting

In this script:

  • We set App Settings and Connection Strings for the web app. App Settings are used to store key-value pairs that your app can consume at runtime. Connection Strings are used to connect to databases or other services.
  • We configure a custom domain for the web app and bind an SSL certificate to enable HTTPS.
  • We configure auto scaling based on CPU percentage. This means that if the CPU usage of the App Service Plan exceeds 70%, an additional instance will be added. The number of instances will be between 1 and 3.

If you wish to start you journey from beggining check out how to start with Powershell in Azure:

No Comments

Post A Comment

Verified by MonsterInsights