Skip to content

Commit

Permalink
feat!: allow to override umig availability zones
Browse files Browse the repository at this point in the history
Sometimes it's profitable to specify exact list of AZs to use instead of
spreading instances across the available AZs.
Another reason is if you want to iterate over AZs subset in the specific
order (e.g. create 1st VM in a zone different from 1st available).
  • Loading branch information
jay7x authored and Yury Bushmelev committed Aug 21, 2023
1 parent aea74d1 commit 2a4a49f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions modules/umig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ See the [simple](https://github.com/terraform-google-modules/terraform-google-vm
| static\_ips | List of static IPs for VM instances | `list(string)` | `[]` | no |
| subnetwork | Subnet to deploy to. Only one of network or subnetwork should be specified. | `string` | `""` | no |
| subnetwork\_project | The project that subnetwork belongs to | `string` | `""` | no |
| zones | Override the availability zones list to create the resources in. | `list(string)` | `[]` | no |

## Outputs

Expand Down
10 changes: 6 additions & 4 deletions modules/umig/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ locals {
# determine type" error when var.static_ips is empty
static_ips = concat(var.static_ips, ["NOT_AN_IP"])

zones = length(var.zones) == 0 ? data.google_compute_zones.available.names : var.zones

instance_group_count = min(
local.num_instances,
length(data.google_compute_zones.available.names),
length(local.zones),
)
}

Expand All @@ -48,7 +50,7 @@ resource "google_compute_instance_from_template" "compute_instance" {
count = local.num_instances
name = format("%s%s%s", local.hostname, var.hostname_suffix_separator, format("%03d", count.index + 1))
project = var.project_id
zone = data.google_compute_zones.available.names[count.index % length(data.google_compute_zones.available.names)]
zone = local.zones[count.index % length(local.zones)]

network_interface {
network = var.network
Expand Down Expand Up @@ -105,11 +107,11 @@ resource "google_compute_instance_group" "instance_group" {
count = local.instance_group_count
name = "${local.hostname}-instance-group-${format("%03d", count.index + 1)}"
project = var.project_id
zone = element(data.google_compute_zones.available.names, count.index)
zone = element(local.zones, count.index)
instances = matchkeys(
google_compute_instance_from_template.compute_instance.*.self_link,
google_compute_instance_from_template.compute_instance.*.zone,
[data.google_compute_zones.available.names[count.index]],
[local.zones[count.index]],
)

dynamic "named_port" {
Expand Down
6 changes: 6 additions & 0 deletions modules/umig/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,9 @@ variable "hostname_suffix_separator" {
description = "Separator character to compose hostname when add_hostname_suffix is set to true."
default = "-"
}

variable "zones" {
type = list(string)
description = "(Optional) List of availability zones to create VM instances in"
default = []
}

0 comments on commit 2a4a49f

Please sign in to comment.