Create Azure Cloud Service in a specific Resource Group from PowerShell

By default you can’t create a Cloud Service in a resource group. I think this might be due to the fact that when Cloud Services where created Azure didn’t have a concept of Resource Groups.

The only way to create a Cloud Service is via this command:

New-AzureService -ServiceName $ServiceName -Location "$Location"

If you look at the result of execution in a new Azure Portal you’ll notice that behind the scenes this creates a new resource group with name identical to the service name and places the cloud service inside.

It doesn’t give you many options. However with a combination of new RM commandlets we can still achieve awesomeness. RM commandlets let you move resources (there a limited list). Fortunately Cloud Service is supported. Below is the complete PowerShell script I’ve created which achieves the task:

param(
 [Parameter(Mandatory=$True)]
 [string]
 $ServiceName,
  
 [Parameter(Mandatory=$True)]
 [string]
 $Location,
  
 [Parameter(Mandatory=$True)]
 [string]
 $ResourceGroupName
)
 
#This script prepares cloud service. If cloud service exists by name it does nothing.
#Otherwise a new cloud service is created in specified resource group.
 
Write-Host "Preparing cloud service '$ServiceName' in resource group '$ResourceGroupName' in '$Location'..."
 
Function WaitForService {
    Param(
        [string]$ResourceGroupName,
         
        [int]$Retries = 10
    )
 
    $tried = 0;
     
    while($tried -le $Retries)
    {
        try
        {
            $cloudService = Get-AzureRmResource -ResourceName "$ServiceName" -ResourceGroupName "$ServiceName"
            Write-Host "[service ready]" -ForegroundColor Green
            return $cloudService
        }
        catch
        {
            Write-Host "[service not ready yet]" -ForegroundColor Red
            #Write-Host "$($_.Exception)"
            Start-Sleep 5
        }
    }
}
 
#create or get service
$service =  Get-AzureService -ServiceName "$ServiceName" -ErrorAction SilentlyContinue
if($service)
{
    Write-Host "Using existing service '$ServiceName'";
}
else
{
    #Note that Azure doesn't support creating cloud services inside specified resource groups
    Write-Host "No service exists, creating new..."
    New-AzureService -ServiceName $ServiceName -Location "$Location"
     
    Write-Host "Looking for the new cloud service..."
    #it turns out that service does not become available immediately hence wait for it in a loop
    $cloudService = WaitForService($ServiceName, 50); 
    $cloudServiceId = $cloudService.ResourceId;
     
    #In order to have the service in desired resource group it has to be moved explicitly
    Write-Host "Moving '$ServiceName' ($cloudServiceId) to resource group '$ResourceGroupName'..."
    Move-AzureRmResource -DestinationResourceGroupName "$ResourceGroupName" -ResourceId $cloudService.ResourceId -Force
     
    #Delete the pre-created resource group. New-AzureService will create a new resource group with same name
    #as service name, and because service is now moved to a new group old one can be killed.
    Write-Host "Removing resoure group '$ServiceName'..."
    Remove-AzureRmResourceGroup -Name "$ServiceName" -Force
}
 
Write-Host "[service online]" -ForegroundColor Green

P.S. Update in 2021: As cloud services are dying, snapshotting some pages here.


To contact me, send an email anytime or leave a comment below.