---
title: "openstack_compute_interface_attach_v2"
description: ""
sidebar_position: 5
---

# openstack\_compute\_interface\_attach\_v2

:::info

This instruction is an adapted copy of the official OpenStack Terraform provider documentation from the [Terraform Registry](https://registry.terraform.io/providers/terraform-provider-openstack/openstack/latest/docs).

:::

Attaches a network interface (port) to an instance using the OpenStack Compute (Nova) v2 API.

## Example Usage \{#example-usage}

### Basic Attachment \{#basic-attachment}

```hcl
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 \{#attachment-specifying-a-fixed-ip}

```hcl
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 \{#attachment-using-an-existing-port}

```hcl
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 \{#attaching-multiple-interfaces}

```hcl
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:

```hcl
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 \{#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 associate with the port. *NOTE*: This option cannot be used with port\_id. You must specify a network\_id. The IP address must lie in a range on the supplied network.

## Attributes Reference \{#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 \{#import}

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

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