Wednesday 4 January 2023

Resource groups

Before we can configure just about anything in azure we need to create a 'Resource group' to hold our 'stuff'. If your are wondering what a resource group is in azure, you can think of it as a logical container for resources linked with your subscription. Basically it's a way to organise the things out deploy, those could be virtual machines, storage accounts, virtual networks, app services, app service plans, blob storage, etc. Basically any resource within azure must be stored within a resource group. One caveat is Azure services, they generally run at the subscription level, which is more ore less the thing you pay for.

To setup a resource group is pretty simple, log into your azure portal, and click the resource groups icon

you should see something like the following
I've blacked a few things out, for no particular reason, all of those details would be different for you anyway, the important thing is to click the create button.
once you've filled out all the mandatory details, you can add some tags if you like, this could prove useful especially if you are managing lots of resource groups and would like an easy way to filter them by, however you can always do it later... which is my favourite time to think of new key value pairs. 

Rather than expend mental energy thinking of good contrived tags, we're just gonna go ahead and create our resource group.
Let's click the 'Create' button and see what happens.... Shockingly, there's a resource group now listed with the name we provided.
We have our resource group, now that is good and dandy, now personally I prefer to script the creation of my Azure resources, so rather than going through the UI I can run a powershell script and create everything that I need. This way if i mess something up, I can just delete everything and start over. 


#create resource group
function CreateResourceGroup {
Param(
[Parameter(Mandatory=$true)][String]$name,
[Parameter(Mandatory=$true)][String]$location,
[Parameter(Mandatory=$true)][String]$environment)
$rgName = "rg-$name-$environment"
#check if resource group already exists
$resourceGroup = Get-AzResourceGroup -Name $rgName -ErrorAction SilentlyContinue
if($resourceGroup){
Write-host "Resource group already exists" -foregroundcolor yellow
return
}

#create resource group
Write-Host "Createing $rgName resource group in $location" -foregroundcolor magenta
Write-Host "With tags [application = $name; environment = $environment]" -foregroundcolor magenta
try {
$resourceGroup = New-AzResourceGroup -Name $rgName -Location $location -Tag @{Application=$name; Environment=$environment}
Write-host "Resource group created" -foregroundcolor green
write-host $resourceGroup
return
}
catch {
#failed to create resource group
Write-error "Resource group NOT created" -ErrorAction Stop
throw
}
}

# 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
}

# create resource group
CreateResourceGroup -name pav-example4 -location westeurope -environment dev

# Disconnect active account
Disconnect-AzAccount


this is the way I prefer to automate my deployments, there are other methods, namely config files you can use, and maybe one day I'll take the time to learn them.