Skip to content

10.1 Resource Group Setup

Overview

The resource group serves as a logical container for all Azure resources in this infrastructure. This implementation uses a dynamic naming strategy with the random_pet provider to ensure unique resource group names.

Terraform Configuration

Random Pet Resource

resource "random_pet" "rg_name" {
  prefix = var.resource_group_name_prefix
}

Purpose: Generates a random, human-readable name for the resource group.

How it works: - Uses the random_pet provider to create names like "rg-happy-dolphin" or "rg-brave-tiger" - Combines the prefix variable with randomly generated animal names - Ensures uniqueness across Azure subscriptions

Resource Group Creation

resource "azurerm_resource_group" "example" {
  location = var.resource_group_location
  name     = random_pet.rg_name.id
}

Key Properties: - name: Uses the randomly generated name from random_pet.rg_name.id - location: Specified by variable (default: "eastus")

Variables

resource_group_name_prefix

variable "resource_group_name_prefix" {
  type        = string
  default     = "rg"
  description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
}

Default: "rg" Customization: Change to match your naming convention (e.g., "prod-rg", "dev-rg")

resource_group_location

variable "resource_group_location" {
  type        = string
  default     = "eastus"
  description = "Location of the resource group."
}

Default: "eastus" Options: Any valid Azure region (e.g., "westus2", "centralus", "westeurope")

Why Use Random Naming?

Advantages:

  1. Uniqueness: Prevents naming conflicts in Azure
  2. Testing: Easy to create multiple test environments
  3. Automation: No manual name management required
  4. Cleanup: Easy to identify and delete test resources

Considerations:

  • Production: Consider using fixed names for production environments
  • Tracking: Random names can be harder to track in billing
  • Compliance: Some organizations require specific naming conventions

Resource Dependencies

All other resources in this infrastructure depend on this resource group:

random_pet.rg_name
        ↓
azurerm_resource_group.example
        ↓
    ┌───┴───┬───────┬──────────┐
    ↓       ↓       ↓          ↓
  VNet     VMs     LB      Public IP

Example Output

When deployed, you might see:

# Terraform Apply Output
random_pet.rg_name: Creating...
random_pet.rg_name: Creation complete after 0s [id=rg-golden-panther]

azurerm_resource_group.example: Creating...
azurerm_resource_group.example: Creation complete after 2s [id=/subscriptions/.../resourceGroups/rg-golden-panther]

Best Practices

For Development/Testing:

resource_group_name_prefix = "dev-rg"

For Production:

Consider replacing the random naming with a fixed name:

resource "azurerm_resource_group" "example" {
  location = var.resource_group_location
  name     = "prod-lb-infrastructure-rg"
}

For Multi-Environment:

Use workspace-specific prefixes:

resource_group_name_prefix = "${terraform.workspace}-rg"

Azure CLI Verification

After deployment, verify the resource group:

# List all resource groups
az group list --output table

# Show specific resource group details
az group show --name <resource-group-name> --output json

# List resources in the group
az resource list --resource-group <resource-group-name> --output table

Cleanup

To delete the resource group and all contained resources:

# Using Azure CLI
az group delete --name <resource-group-name> --yes --no-wait

# Using Terraform
terraform destroy

Next Steps

Proceed to 10.2-Virtual-Network-Setup.md to configure the networking infrastructure.