Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: allow to override umig availability zones #329

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = []
}