Azure Clouder

Creating Azure Resource Groups using Infrastructure as Code (IaC) with Azure PowerShell

In this post, we will create multiple Azure Resource Groups using Infrastructure as Code (IaC), leveraging Azure PowerShell and a parameter file (JSON) to automate the deployment process.

Azure Resource Group

An Azure Resource Group is a container that holds related resources for an Azure solution. You organize and manage your resources by grouping them in a logical way. Resources like VMs, databases, and storage accounts can be part of a resource group.

Key Points:

  • Each resource group has a specific location (Azure region).
  • It includes metadata like tags for tracking purposes (e.g., cost code, environment).
  • Resources within a resource group can be managed collectively or individually.

PowerShell Script to Deploy Resource Groups

The provided script deploy-rg.ps1 automates the creation of multiple resource groups based on the input from the JSON file parameter-rg.json. The JSON file contains the required parameters, including subscription ID, location, tags, and the list of resource groups to be created.

PowerShell Script Breakdown (deploy-rg.ps1)

# Define the parameters file path
$parametersFile = "./parameter-rg.json"

# Load the parameters from the JSON file
$parameters = Get-Content -Path $parametersFile | ConvertFrom-Json

# Set the subscription and location from the JSON file
$subscriptionId = $parameters.subscriptionId
$location = $parameters.location
$rgsConfig = $parameters.resourceGroups

# Convert tags to Hashtable (used for tagging resources)
$tags = @{}
$parameters.tags.PSObject.Properties | ForEach-Object {
    $tags[$_.Name] = $_.Value
}

# Connect to Azure (this will prompt you to log in)
Connect-AzAccount

# Set the subscription context (ensure correct subscription is used)
Set-AzContext -SubscriptionId $subscriptionId

# Create resource groups in a loop
foreach ($rgConfig in $rgsConfig) {
    $resourceGroupName = $rgConfig.name
    
    # Log the resource group creation
    Write-Host "Creating resource group: $resourceGroupName in $location"
    
    # Create the resource group with the specified name, location, and tags
    New-AzResourceGroup -Name $resourceGroupName '
                        -Location $location -Tag $tags

    # Confirm creation success
    Write-Host "Resource group $resourceGroupName created successfully."
}

Explanation of the Script

  1. Parameters File Path:

    • The $parametersFile variable defines the path to the JSON file that contains the configuration data.
  2. Loading JSON Parameters:

    • The Get-Content cmdlet reads the JSON file, and ConvertFrom-Json converts it into a PowerShell object for easy manipulation.
  3. Subscription and Location:

    • The $subscriptionId and $location variables are populated from the JSON file, which specify the Azure subscription and the region where the resource groups will be created.
  4. Tags Handling:

    • Tags (key-value pairs) from the JSON file are converted into a PowerShell hashtable ($tags) for easy application when creating the resource groups.
  5. Azure Login:

    • Connect-AzAccount connects to your Azure account.
  6. Subscription Context:

    • Set-AzContext sets the specific subscription where the resources will be created using the subscription ID from the JSON file.
  7. Resource Group Creation Loop:

    • The script iterates through the list of resource groups ($rgsConfig) defined in the JSON file.
    • For each resource group, it calls the New-AzResourceGroup cmdlet to create it with the defined name, location, and tags.
    • Messages are printed to confirm the creation of each resource group.

JSON Parameter File Breakdown (parameter-rg.json)

{
    "subscriptionId": "your subscription id",
    "location": "australiaeast",
    "tags": {
        "Costcode": "your cost code",
        "Owner": "Project Team",
        "Environment": "Prd"
    },
    "resourceGroups": [
        {
            "name": "resource group - 01"
        },
        {
            "name": "resource group - 02"
        },
        {
            "name": "resource group - 03"
        },
        {
            "name": "resource group - 04"
        },
        {
            "name": "resource group - 05"
        },
        {
            "name": "resource group - 06"
        }
    ]
}

 Explanation of the JSON File

  1. Subscription ID:

    • "subscriptionId": "your subscription id" specifies the Azure subscription in which the resource groups will be created.
  2. Location:

    • "location": "australiaeast" defines the Azure region where the resource groups will reside. In this case, it’s Australia East.
  3. Tags:

    • "tags" defines a series of metadata about the resource groups, such as cost code, owner, environment, etc. Tags help in organizing and managing Azure resources effectively.
  4. Resource Groups:

    • "resourceGroups" is an array of resource group names to be created. The script iterates through this array and creates each resource group one by one.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.