1

I'm able to create a Compute Engine with either a:

  • Predefined IP address
  • Dynamic IP address

But I can't create a Compute Engine with a semi-dynamic address, i.e. the first 3 numbers being static but not the last one, e.g. 255.255.255.*

The Compute Engine is being created on a VPC whose IP address range is too wide for my requirements. I'm attempting to created all of my servers on a close range of IP addresses but I also don't want make each one static and have to maintain it in the Terraform code.

Does anyone know a means to reserve and assign IP's in this restricted way?

JaysonM
  • 596
  • 1
  • 10
FreeZey
  • 2,382
  • 3
  • 11
  • 23
  • 3
    Is your question about public (external) or private (VPC) addressing? For public IP addresses, that is not possible to specify a range. For private, create a VPC subnet that meets your addressing requirements. – John Hanley Oct 14 '21 at 17:40
  • It's an internal address on a VPC. But we don't own/control the VPC so we can't adjust the range. – FreeZey Oct 15 '21 at 08:11

1 Answers1

1

The best fit I see for your need is to use the "Google Address Terraform Module", where you can define a list of ip addresses that match a list of instance names and assign the corresponding values. Here, you can find a document describing the module and how to use it [1].

Below, there is an example for the allocation of 3 ip addresses to different instances as a reference:

module "address-fe" {
  source  = "terraform-google-modules/address/google"
  version = "0.1.0"

  subnetwork = "projects/gcp-network/regions/us-west1/subnetworks/dev-us-west1-dynamic"

  names = [
    "gusw1-dev-fooapp-fe-0001-a-001-ip",
    "gusw1-dev-fooapp-fe-0001-a-002-ip",
    "gusw1-dev-fooapp-fe-0001-a-003-ip"
  ]

  addresses = [
    "10.11.0.10",
    "10.11.0.11",
    "10.11.0.12"
  ]
}

[1] https://registry.terraform.io/modules/terraform-google-modules/address/google/latest

  • Thanks but I have a solution for predefined IP addresses. I was hoping that Terraform would have a semi-dynamic alternative. – FreeZey Oct 18 '21 at 08:17
  • You can use loop to iterate over a count number that you can define, the first 3 octets can be hardcoded and just use the loop to change the last one, and do the same for the names of the instances. Here, you can find an example: `module "address-fe" { source = "terraform-google-modules/address/google" version = "0.1.0" subnetwork = "projects/gcp-network/regions/us-west1/subnetworks/dev-us-west1-dynamic" count = 3 name = "server-${count.index}" address = "10.10.10.${count.index}" }` – Gabriel Robledo Ahumada Oct 19 '21 at 15:15
  • A little issue with this code is that the iteration will start from 0, so the first output name will be server-0 and the first address will be 10.10.10.0 – Gabriel Robledo Ahumada Oct 19 '21 at 15:16
  • I'm not sure how this list of reserved IP addresses is used by a VM when it's created. I.e. how is the list referenced from the "google_compute_instance" resource and how does it know which reserved IP address is available for itself? As I understand it the VM instance name isn't the value defined in the "names" array, but if that's the case the solution wouldn't fit the requirement because the instance names need to be specified in another location/file. – FreeZey Nov 05 '21 at 10:52