Skip to content

Commit

Permalink
fix!: set default availability zones to null and find azs that suppor…
Browse files Browse the repository at this point in the history
…t the instance type
  • Loading branch information
danielpanzella committed Sep 3, 2024
1 parent 9ed24d8 commit 6c14225
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
10 changes: 9 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ module "app_lb" {
tags = var.tags
}

data "external" "az_zones" {
program = ["bash", "${path.module}/vmtype_to_az.sh", local.kubernetes_instance_type, azurerm_resource_group.default.location]
}

locals {
node_pool_zones = (var.node_pool_zones == null) ? jsondecode(data.external.az_zones.result.zones) : var.node_pool_zones
}

module "app_aks" {
source = "./modules/app_aks"
depends_on = [module.app_lb]
Expand All @@ -126,7 +134,7 @@ module "app_aks" {
node_pool_min_vm_count = local.kubernetes_min_node_count
node_pool_max_vm_count = local.kubernetes_max_node_count
node_pool_vm_size = local.kubernetes_instance_type
node_pool_zones = var.node_pool_zones
node_pool_zones = local.node_pool_zones
public_subnet = module.networking.public_subnet
resource_group = azurerm_resource_group.default
sku_tier = var.cluster_sku_tier
Expand Down
2 changes: 1 addition & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ variable "cluster_sku_tier" {
variable "node_pool_zones" {
type = list(string)
description = "Availability zones for the node pool"
default = ["1", "2"]
default = null
}

variable "node_max_pods" {
Expand Down
32 changes: 32 additions & 0 deletions vmtype_to_az.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#! /usr/bin/env bash

# Given a Azure VM instance type and a region return the availability zones that support the instance type

# Example:
# ./vmtype_to_az.sh Standard_D2_v3 westeurope
#
# Output:
# ["1", "2", "3"]

# Copy script arguments to named environment variables
VM_TYPE="$1"
REGION="$2"

# Check if both arguments are provided
if [ -z "$VM_TYPE" ] || [ -z "$REGION" ]; then
echo "Error: Both VM type and region must be provided." >&2
echo "Usage: $0 <vm_type> <region>" >&2
exit 1
fi

# Query Azure CLI for availability zones in the region for the specified VM type
ZONES=$(az vm list-skus --location "$REGION" --size "$VM_TYPE" --query "[0].locationInfo[0].zones" -o json | jq -r -c 'sort | .[0:3]')

# Check if the query returned any results
if [ -z "$ZONES" ] || [ "$ZONES" == "null" ]; then
echo "Error: No availability zones found for VM type $VM_TYPE in region $REGION." >&2
exit 1
fi

# Output the result
jq -n --arg zones "$ZONES" '{"zones":$zones}'

0 comments on commit 6c14225

Please sign in to comment.