Skip to main content
openstack_compute_interface_attach_v2
Last update:

openstack_compute_interface_attach_v2

For your information

This instruction is an adapted copy of the official OpenStack Terraform provider documentation in the Terraform Registry.

Attaches a Network Interface (a Port) to an Instance using the OpenStack Compute (Nova) v2 API.

Example Usage

Basic Attachment

resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
admin_state_up = "true"
}

resource "openstack_networking_subnet_v2" "subnet_1" {
network_id = openstack_networking_network_v2.network_1.id
name = "my-subnet"
cidr = "10.10.0.0/24"
}

resource "openstack_compute_instance_v2" "instance_1" {
name = "instance_1"
flavor_id = "1311"
image_id = "<image-id>"
security_groups = ["default"]
}

resource "openstack_compute_interface_attach_v2" "ai_1" {
instance_id = openstack_compute_instance_v2.instance_1.id
network_id = openstack_networking_network_v2.network_1.id
}

Attachment Specifying a Fixed IP

resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
admin_state_up = "true"
}

resource "openstack_networking_subnet_v2" "subnet_1" {
network_id = openstack_networking_network_v2.network_1.id
name = "my-subnet"
cidr = "10.10.0.0/24"
}

resource "openstack_compute_instance_v2" "instance_1" {
name = "instance_1"
flavor_id = "1311"
image_id = "<image-id>"
security_groups = ["default"]
}

resource "openstack_compute_interface_attach_v2" "ai_1" {
instance_id = openstack_compute_instance_v2.instance_1.id
network_id = openstack_networking_network_v2.network_1.id
fixed_ip = "10.0.10.10"
}

Attachment Using an Existing Port

resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
admin_state_up = "true"
}

resource "openstack_networking_subnet_v2" "subnet_1" {
network_id = openstack_networking_network_v2.network_1.id
name = "my-subnet"
cidr = "10.10.0.0/24"
}

resource "openstack_networking_port_v2" "port_1" {
name = "port_1"
network_id = openstack_networking_network_v2.network_1.id
admin_state_up = "true"
}

resource "openstack_compute_instance_v2" "instance_1" {
name = "instance_1"
flavor = "1311"
image_id = "<image-id">
security_groups = ["default"]
}

resource "openstack_compute_interface_attach_v2" "ai_1" {
instance_id = openstack_compute_instance_v2.instance_1.id
port_id = openstack_networking_port_v2.port_1.id
}

Attaching Multiple Interfaces

resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
admin_state_up = "true"
}

resource "openstack_networking_subnet_v2" "subnet_1" {
network_id = openstack_networking_network_v2.network_1.id
name = "my-subnet"
cidr = "10.10.0.0/24"
}

resource "openstack_networking_port_v2" "ports" {
count = 2
name = format("port-%02d", count.index + 1)
network_id = openstack_networking_network_v2.network_1.id
admin_state_up = "true"
}

resource "openstack_compute_instance_v2" "instance_1" {
name = "instance_1"
flavor_id = "1311"
image_id = "<image-id>"
security_groups = ["default"]
}

resource "openstack_compute_interface_attach_v2" "attachments" {
count = 2
port_id = openstack_networking_port_v2.ports[count.index].id
instance_id = openstack_compute_instance_v2.instance_1.id
}

Note that the above example will not guarantee that the ports are attached in a deterministic manner. The ports will be attached in a seemingly random order.

If you want to ensure that the ports are attached in a given order, create explicit dependencies between the ports, such as:

resource "openstack_networking_network_v2" "network_1" {
name = "network_1"
admin_state_up = "true"
}

resource "openstack_networking_subnet_v2" "subnet_1" {
network_id = openstack_networking_network_v2.network_1.id
name = "my-subnet"
cidr = "10.10.0.0/24"
}

resource "openstack_networking_port_v2" "ports" {
count = 2
name = format("port-%02d", count.index + 1)
network_id = openstack_networking_network_v2.network_1.id
admin_state_up = "true"
}

resource "openstack_compute_instance_v2" "instance_1" {
name = "instance_1"
flavor_id = "1311"
image_id = "<image-id>"
security_groups = ["default"]
}

resource "openstack_compute_interface_attach_v2" "ai_1" {
instance_id = openstack_compute_instance_v2.instance_1.id
port_id = openstack_networking_port_v2.ports[0].id
}

resource "openstack_compute_interface_attach_v2" "ai_2" {
instance_id = openstack_compute_instance_v2.instance_1.id
port_id = openstack_networking_port_v2.ports[1].id
}

Argument Reference

The following arguments are supported:

  • region — (Optional) The region in which to create the interface attachment. If omitted, the region argument of the provider is used. Changing this creates a new attachment.

  • instance_id — (Required) The ID of the Instance to attach the Port or Network to.

  • port_id — (Optional) The ID of the Port to attach to an Instance.NOTE: This option and network_id are mutually exclusive.

  • network_id — (Optional) The ID of the Network to attach to an Instance. A port will be created automatically.NOTE: This option and port_id are mutually exclusive.

  • fixed_ip — (Optional) An IP address to assosciate with the port.NOTE: This option cannot be used with port_id. You must specifiy a network_id. The IP address must lie in a range on the supplied network.

Attributes Reference

The following attributes are exported:

  • region — See Argument Reference above.
  • instance_id — See Argument Reference above.
  • port_id — See Argument Reference above.
  • network_id — See Argument Reference above.
  • fixed_ip  — See Argument Reference above.

Import

Interface Attachments can be imported using the Instance ID and Port ID separated by a slash, e.g.

$ terraform import openstack_compute_interface_attach_v2.ai_1 89c60255-9bd6-460c-822a-e2b959ede9d2/45670584-225f-46c3-b33e-6707b589b666