-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Comments
@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"
} |
@joe-boyce does above code work for you? |
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:
module variables.tf
Resources being created via module:
As mentioned if external_ip is not provided above the resources would be created with an ephemeral set Thanks, Joe |
@joe-boyce I was thinking of something like below code which can create multiple instances. You may notice their # 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"
} |
@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. |
@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 |
@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 |
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! |
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:
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
The text was updated successfully, but these errors were encountered: