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

Dynamically create google_compute_instance nat_ip #6810

Closed
joe-boyce opened this issue Jul 20, 2020 · 8 comments
Closed

Dynamically create google_compute_instance nat_ip #6810

joe-boyce opened this issue Jul 20, 2020 · 8 comments
Assignees
Labels

Comments

@joe-boyce
Copy link

joe-boyce commented Jul 20, 2020

We are currently using modules across the board to ease the use of terraform for many customers, one of the pain points at the moment is the use of external IPs being assigned to instances

We currently have the following configuration in place:

network_interface {
    network = var.network
    subnetwork_project = var.subnetwork_project
    subnetwork  = var.subnetwork
    network_ip     = length(var.address) > 0 ? element(concat(var.address, list("")), count.index) : ""

    access_config {
        nat_ip = length(var.external_ip) > 0 ? element(concat(var.external_ip, list("")), count.index) : ""
    }
  }

This works by either allowing the user to provide a static external IP for each instance or if left blank will assign a dynamic external IP

Following on from the following case: #2712

Are you able to provide a working example of how to conditionally or dynamically use the access_config {} block so that if no external IP is required this can be done using the same module

Thanks,

Joe

@edwardmedia edwardmedia self-assigned this Jul 21, 2020
@edwardmedia
Copy link
Contributor

@joe-boyce can you see if below code works for you?

resource "google_compute_instance" "default" {
  name         = "issue6810demo"
  machine_type = "n1-standard-1"
  zone         = "us-central1-a"
  tags = ["foo", "bar"]
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }
  // Local SSD disk
  scratch_disk {
    interface = "SCSI"
  }
  network_interface {
    network = "default"
    dynamic "access_config" {
       for_each = var.natip == ""? [] : [1]
       content {
         nat_ip = var.natip
       }
    }
  }
  service_account {
    scopes = ["userinfo-email", "compute-ro", "storage-ro"]
  }
}
variable "natip" {
  default = "35.222.xxx.xxx"
}

@edwardmedia
Copy link
Contributor

@joe-boyce does above code work for you?

@joe-boyce
Copy link
Author

joe-boyce commented Aug 13, 2020

Thanks for the response @edwardmedia, how does this work in terms of within a module thats using count for x instances, as your example only seems to work with a single instance being created

As mentioned above the code is currently:

module main.tf:

access_config {
        nat_ip = length(var.external_ip) > 0 ? element(concat(var.external_ip, list("")), count.index) : ""
 }

module variables.tf

variable "external_ip" {
  type        = list(string)
  default     = []
}

Resources being created via module:

module "example" {
    source             = "git@github.com:path/to/module.git?ref=v1.0"
    project            = var.project
    instance_count     = 2
    name               = ["instance-2", "instance-2"]
    zones              = ["us-central1a", "us-central1b"]
    address            = ["10.x.x.1", "10.x.x.2"]
    external_ip        = ["35.222.xxx.1", "35.222.xxx.2"]
}

As mentioned if external_ip is not provided above the resources would be created with an ephemeral set

Thanks,

Joe

@ghost ghost removed waiting-response labels Aug 13, 2020
@edwardmedia
Copy link
Contributor

edwardmedia commented Aug 19, 2020

@joe-boyce I was thinking of something like below code which can create multiple instances. You may notice their natip for each instance.

# main.tf
variable "vms" {
    type = map(string)
    default = {
        vm1 = "35.223.xxx.xxx"
        vm2 = ""
    }
}
module "servers" {
    source = "./modules/vm"
    for_each = var.vms
    natip = each.value 
    name = each.key
}
# modules/vm/main.tf
resource "google_compute_instance" "default" {
  name         = var.name
  machine_type = "n1-standard-1"
  zone         = "us-central1-a"
  tags = ["foo", "bar"]
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }
  // Local SSD disk
  scratch_disk {
    interface = "SCSI"
  }
  network_interface {
    network = "default"
     dynamic "access_config" {
       for_each = var.natip == ""? [] : [1]
       content {
         nat_ip = var.natip
       }
    }
  }
  service_account {
    scopes = ["userinfo-email", "compute-ro", "storage-ro"]
  }
}
# modules/vm/variables.tf
variable "natip" {
 type = "string" 
}
variable "name" {
  type = "string" 
}

@edwardmedia
Copy link
Contributor

@joe-boyce above example might not be exactly as what you wanted. But as you may see, it can create instances as many as you want. For each instance's natip, you can either provide yours, or let GCP provides. And the code are built as module. Please let me know if that works for you.

@joe-boyce
Copy link
Author

@edwardmedia I haven't tried the new example but when you say "provide yours, or let GCP provides", does this cater for not having an external IP at all, as that is the main purpose of the ticket to provide all 3 variations

Thanks,

Joe

@ghost ghost removed the waiting-response label Aug 26, 2020
@edwardmedia
Copy link
Contributor

edwardmedia commented Sep 8, 2020

@joe-boyce if the suggestions are not good enough to meet your use case, I would recommend you post the question at discuss.hashicorp.com as the solutions would be more Terraform, not the Provider specific. I am closing this issue then. Feel free to reopen it if you think needing assistance for the provider. Thank you

@ghost
Copy link

ghost commented Oct 9, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks!

@ghost ghost locked as resolved and limited conversation to collaborators Oct 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants