Multi-IP aliases on interface using alias_ip_range in GCP

Hi,

This is my first post, so I hope somebody can help me! Still new to terraforming!

In GCP instance, you can use their admin portal to create an instance, and in the networking section you can add multiple IP aliases to the default interface.

I am trying to replicate this via TF.

This code block works, and it creates a single IP alias to the instance:

network_interface {
        subnetwork         = "${var.subnet}"
        subnetwork_project = "${var.subnet_project}"
        alias_ip_range {
                ip_cidr_range = "/32"
        }       
} 

This code creates the first additional IP alias correctly.
What I cannot get to work is adding a second alias_ip_range to create a second IP alias.

Hope that makes sense, and thanks.

What configuration did you try when adding a second alias_ip_range, and what was the result?

Depending on how you syntax the second entry, you get either ‘no changes’ to various errors.

EG:

network_interface {
        subnetwork         = "${var.subnet}"
        subnetwork_project = "${var.subnet_project}"
        alias_ip_range {
                ip_cidr_range = "/32"
        }
        alias_ip_range {
                ip_cidr_range = "/32"
        }
}

Yields this error when you ‘plan’:

Error: google_compute_instance.gcnpo-pex1: network_interface.0.alias_ip_range: attribute supports 1 item maximum, config has 2 declared
make: *** [plan] Error 1

This one:

network_interface {
        subnetwork         = "${var.subnet}"
        subnetwork_project = "${var.subnet_project}"
        alias_ip_range {
                ip_cidr_range = ["/32","/32"]
        }
}

Yields this error:

Error: google_compute_instance.gcnpo-pex1: network_interface.0.alias_ip_range.0.ip_cidr_range must be a single value, not a list

Tried various things with braces, nothing seems to work.

EG:

network_interface {
	subnetwork         = "${var.subnet}"
	subnetwork_project = "${var.subnet_project}"
	alias_ip_range {
		[
			ip_cidr_range = "/32",
			ip_cidr_range = "/32"
		]
	}
}

Gives you:

There are some problems with the configuration, described below.

The Terraform configuration must be valid before initialization so that
Terraform can determine which modules and providers need to be installed.

Error: Error parsing /Users/karl/WORK/GoogleCloud/builds/live_tmg_code/linux_servers/tmg-iac-gcnpo/terraform/vm_gcnpo-pex1.tf: At 87:4: expected: IDENT | STRING | ASSIGN | LBRACE got: LBRACK

One thing I haven’t considered is perhaps it’s the old version of the terraform binary I’m using. Perhaps a bug?

Terraform v0.11.11

Thanks.

I wasn’t able to reproduce this problem, and your syntax looks fine. It could be a Terraform version issue! I would recommend upgrading to Terraform 0.13.0.

This configuration generates a valid plan for me:

terraform {
  required_providers {
    google = {
      source = "hashicorp/google"
      version = "3.35.0"
    }
  }
  required_version = ">= 0.13"
}

resource "google_compute_instance" "default" {
  name         = "test"
  machine_type = "n1-standard-1"
  zone         = "us-central1-a"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }

  network_interface {
    network = "default"

    alias_ip_range {
      ip_cidr_range = "/32"
    }

    alias_ip_range {
      ip_cidr_range = "/32"
    }
  }
}

Thanks.
I think that is the next step to grab a newer TF binary.
Will let you know if that fixes it!

Using tf version 0.13.0 now throws many non-related compatibility errors. Will need to resolve those now…god I hate i.t. …