I need to create multiple compute instances and and multiple floating IPs and assign those floating IPs to the instances. I'm using "count" for that. However the problem is I can't declare a resource inside another resource.
provider "openstack" {
user_name = "xxxx"
}
variable "comp-count" {}
###########################################
resource "openstack_compute_floatingip_v2" "fl-ip" {
region = ""
pool = "DNY1"
count = "${var.comp-count}"
}
resource "openstack_compute_instance_v2" "comp-instance" {
count = "${var.comp-count}"
name = "compute-${count.index}"
image_name = "xxxx"
flavor_name = "m1.medium"
security_groups = ["sadooghiTest"]
floating_ip = "${openstack_compute_floatingip_v2.fl-ip.???.address}"
key_pair = "xxx"
}
I am trying to pass the name of floating ips that I have assigned earlier which are ("fl-ip.0","fl-ip.1","fl-ip.2",...) to the floating_ip attribute of my compute instances.
I tried the following and all of them had syntax errors:
floating_ip = "${openstack_compute_floatingip_v2.fl-ip.${fl-ip.count.index}.address}"
or
floating_ip = "${openstack_compute_floatingip_v2.fl-ip.fl-ip.${count.index}.address}"
Variable interpolation is not allowed! so the following line wont work:
variable "tst" {default="something.${var.comp-count}"}
Errors:* Variable 'tst': cannot contain interpolations
Do you know how is this possible? This is a common use case when someone wants to deploy a bunch of instances and assign folating IPs to those instances.
Try if below config works for you:
resource "openstack_compute_floatingip_v2" "fip" {
count = "${var.count}"
region = "myregion"
pool = "mypool"
}
resource "openstack_compute_instance_v2" "amqp_instances" {
count = "${var.count}"
region = "myregion"
name = "myinstance"
image_name = "myimage"
flavor_name = "myflavor"
key_pair = "key_pair"
network {
name = "mynw"
}
floating_ip = "${element(openstack_compute_floatingip_v2.fip.*.address, count.index)}"
}
@bbayani I wanted to thank you and confirm that for me your suggestion worked (all this time later)!
@bbayani's answer doesn't work for me on newer versions of terraform due to deprecation of resources, however something like this works for me:
resource "openstack_compute_floatingip_associate_v2" "kube_ips" {
count = 3
floating_ip = "${element(openstack_networking_floatingip_v2.floating_ips.*.address, count.index)}"
instance_id = "${element(openstack_compute_instance_v2.kube.*.id, count.index)}"
}
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 have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.