I am creating a virtual machine on an OpenStack-based cloud using Terraform and I need to associate a floating IP with it.
The problem is that Terraform requires a port_id to associate the floating IP, but I cannot retrieve this value automatically from the created instance.
What I am trying to achieve
Create a new VM
Create or assign a floating IP
Associate this floating IP with the VM in a single Terraform run, without manual steps
Current behavior
With my current approach, I must:
Run Terraform once to create the instance
Manually retrieve the port ID using:
openstack port list -f json | grep -B8 "10.20.30.202" | grep ID
Copy this
port_idinto TerraformRun Terraform again
This is not acceptable for automation.
Terraform code
resource "openstack_compute_instance_v2" "instance" {
name = "test-vm"
image_name = "Debian 12 - Docker"
flavor_name = "d2-2"
key_pair = "my-key"
security_groups = [
openstack_networking_secgroup_v2.secgroup.name
]
network {
name = "my-network"
}
}
resource "openstack_networking_floatingip_associate_v2" "fip" {
floating_ip = "172.10.0.6"
port_id = "d8c48c8b-a5cf-4dcf-9d90-544bb6a3f51e" # must be set manually
}
Question
How can I retrieve the correct port_id of the newly created instance directly in Terraform, so that the floating IP can be associated automatically in one apply?