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:
1New-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:
1param(
2 [Parameter(Mandatory=$True)]
3 [string]
4 $ServiceName,
5
6 [Parameter(Mandatory=$True)]
7 [string]
8 $Location,
9
10 [Parameter(Mandatory=$True)]
11 [string]
12 $ResourceGroupName
13)
14
15#This script prepares cloud service. If cloud service exists by name it does nothing.
16#Otherwise a new cloud service is created in specified resource group.
17
18Write-Host "Preparing cloud service '$ServiceName' in resource group '$ResourceGroupName' in '$Location'..."
19
20Function WaitForService {
21 Param(
22 [string]$ResourceGroupName,
23
24 [int]$Retries = 10
25 )
26
27 $tried = 0;
28
29 while($tried -le $Retries)
30 {
31 try
32 {
33 $cloudService = Get-AzureRmResource -ResourceName "$ServiceName" -ResourceGroupName "$ServiceName"
34 Write-Host "[service ready]" -ForegroundColor Green
35 return $cloudService
36 }
37 catch
38 {
39 Write-Host "[service not ready yet]" -ForegroundColor Red
40 #Write-Host "$($_.Exception)"
41 Start-Sleep 5
42 }
43 }
44}
45
46#create or get service
47$service = Get-AzureService -ServiceName "$ServiceName" -ErrorAction SilentlyContinue
48if($service)
49{
50 Write-Host "Using existing service '$ServiceName'";
51}
52else
53{
54 #Note that Azure doesn't support creating cloud services inside specified resource groups
55 Write-Host "No service exists, creating new..."
56 New-AzureService -ServiceName $ServiceName -Location "$Location"
57
58 Write-Host "Looking for the new cloud service..."
59 #it turns out that service does not become available immediately hence wait for it in a loop
60 $cloudService = WaitForService($ServiceName, 50);
61 $cloudServiceId = $cloudService.ResourceId;
62
63 #In order to have the service in desired resource group it has to be moved explicitly
64 Write-Host "Moving '$ServiceName' ($cloudServiceId) to resource group '$ResourceGroupName'..."
65 Move-AzureRmResource -DestinationResourceGroupName "$ResourceGroupName" -ResourceId $cloudService.ResourceId -Force
66
67 #Delete the pre-created resource group. New-AzureService will create a new resource group with same name
68 #as service name, and because service is now moved to a new group old one can be killed.
69 Write-Host "Removing resoure group '$ServiceName'..."
70 Remove-AzureRmResourceGroup -Name "$ServiceName" -Force
71}
72
73Write-Host "[service online]" -ForegroundColor Green
P.S. Update in 2021: As cloud services are dying, snapshotting some pages here.


Have feedback or questions? Feel free to email me.