New version of Windows Azure deployment script

We have updated our deployment script with new cmdlets included in the latest Windows Azure Powershell (May 2013). If you want to know what’s new, please read my short blog post.

One of the most valuable improvements in the newly updated Windows Azure cmdlets are commands used for managing Windows Azure SQL servers, databases, and firewall rules. These new commands are used in our Azure deployment Powershell script:

  • New-AzureSqlDatabaseServer
  • New-AzureSqlDatabaseServerFirewallRule
  • New-AzureSqlDatabaseServerContext
  • New-AzureSqlDatabase
If you want to know what the differences in managing SQL servers are, let’s take a look at an example. In the old Azure deployment powershell script you had to:
  1. load SQL Server Management Objects assembly:
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null
  1. and create database this way:
# Create a new connection $serverConnection = New-Object Microsoft.SqlServer.Management.Common.ServerConnection $serverConnection.ServerInstance = $sqlServerName $serverConnection.LoginSecure = $sqlLoginSecure $serverConnection.Login = $sqlLogin $serverConnection.Password = $sqlPassword $srv = New-Object ('Microsoft.SqlServer.Management.SMO.Server') ($serverConnection) # Create a new database $db = New-Object ('Microsoft.SqlServer.Management.SMO.Database') ($srv, $databaseName) $db.Create() # Close the connection $srv.ConnectionContext.Disconnect()

Using the new Windows Azure cmdlets you don’t have to load any additional assemblies. Instead of the code in step 2, you can easily use this command:
# Create new Azure SQL server $server = New-AzureSqlDatabaseServer -Location $geoLocation -AdministratorLogin $sqlLogin -AdministratorLoginPassword $sqlPassword # Create a new connection context to the specified SQL Server Management Service $ctx = New-AzureSqlDatabaseServerContext -ServerName $serverName -Credential $servercredential # Create a new SQL Database New-AzureSqlDatabase $ctx -DatabaseName $databaseName -MaxSizeGB 1 -Edition Web -Collation "SQL_Latin1_General_CP1_CI_AS"
If you are interested in all the new Azure SQL commands you can visit the official Windows Azure documentation.

The second interesting improvement in the script is the option which allows you to create Windows Azure services – cloud service, cloud storage and Azure SQL server with a database – all at once. Running the script with parameter “-c $true” allows you to specify:

  • path ­­– path to the Azure web site
  • subscriptionId
  • thumbprint – management certificate thumbprint
  • slot - deployment slot where you want to deploy your website
  • new geo-location – geographical location where Windows Azure services should be created
    • The script will create an affinity group which  allows you to group all your Windows Azure services in the specified location. You can choose from these locations:
      • East US - Virginia
      • West US - California
      • West Europe - Amsterdam, Netherlands
      • North Europe - Dublin, Ireland
      • East Asia - Hong Kong, China
      • South East Asia - Singapore
  • new service name
  • new storage name
  • new SQL login
  • new SQL password
I’ll try to demonstrate how the script works on this example:
.\AzureDeployment.ps1 -c "$true" -path "<path to your project>" -slot "production" -thumbprint "<your certificate thumbprint>" -subscriptionId "<your subscription ID>" -geoLocation "East US" -serviceName "MyCloudService" -storageName "MyStorageName" -sqlLogin "SQLUsername" -sqlPassword "MySQLPassword" databaseName "MyDatabase"

  1. Affinity group in East US location is created:
# Create new affinity group New-AzureAffinityGroup -Name $affinityGroupName -Location $geoLocation
  1. Cloud service MyCloudService is created:
# Create new cloud service New-AzureService -ServiceName $serviceName -AffinityGroup $affinityGroupName
  1. Azure storage MyAzureStorage is created:
# Create new storage account New-AzureStorageAccount -StorageAccountName $storageName -AffinityGroup $affinityGroupName
  1. Storage key is downloaded (it is needed for updating configuration file) and subscription is updated with newly created storage:
# Get storage keys $storageKeys = Get-AzureStorageKey -StorageAccountName $storageName # Update primary storage key variable used in configuration file $global:storageSharedKey = $storageKeys.Primary # Update subscription with storage account information Set-AzureSubscription $serviceName -CurrentStorageAccount $storageName
  1. Azure SQL server is created (Azure SQL Server name is generated automatically):
# Create new Azure SQL server $server = New-AzureSqlDatabaseServer -Location $geoLocation -AdministratorLogin $sqlLogin -AdministratorLoginPassword $sqlPassword
  1. New firewall rule is created. If you want to call database commands, a new firewall rule with your current IP has to be added:
# Create new IP firewall for SQL server New-AzureSqlDatabaseServerFirewallRule -ServerName $server.ServerName -RuleName "Public IP" -StartIPAddress $publicIp -EndIPAddress $publicIp
  1. New database MyDatabase is created:
# Create a new connection context to the specified SQL Server Management Service. $ctx = New-AzureSqlDatabaseServerContext -ServerName $serverName -Credential $servercredential # Creates a new SQL Database. New-AzureSqlDatabase $ctx -DatabaseName $databaseName -MaxSizeGB 1 -Edition Web -Collation "SQL_Latin1_General_CP1_CI_AS"
  1. Finally your project is deployed to the newly created cloud service.

That’s all. So if you decide to run the script this way you only need to upload the management certificate to your subscription and new coud services will be created and your Azure project will be deployed to the created cloud service.

You can download the script from Kentico Marketplace.

Share this article on   LinkedIn

Juraj Komlosi

Chief Information Security Officer at Kentico Software