FEATURES OPEN SOURCE ABOUT DOCS LOGIN REGISTER

openstack_compute_instance_v2

Manages a V2 VM instance resource within OpenStack.

Example Usage

Basic Instance

resource "openstack_compute_instance_v2" "basic" {
  name = "basic"
  image_id = "ad091b52-742f-469e-8f3c-fd81cadf0743"
  flavor_id = "3"
  key_pair = "my_key_pair_name"
  security_groups = ["default"]

  metadata {
    this = "that"
  }

  network {
    name = "my_network"
  }
}

Instance With Attached Volume

resource "openstack_blockstorage_volume_v1" "myvol" {
  name = "myvol"
  size = 1
}

resource "openstack_compute_instance_v2" "volume-attached" {
  name = "volume-attached"
  image_id = "ad091b52-742f-469e-8f3c-fd81cadf0743"
  flavor_id = "3"
  key_pair = "my_key_pair_name"
  security_groups = ["default"]

  network {
    name = "my_network"
  }

  volume {
    volume_id = "${openstack_blockstorage_volume_v1.myvol.id}"
  }
}

Boot From Volume

resource "openstack_compute_instance_v2" "boot-from-volume" {
  name = "boot-from-volume"
  flavor_id = "3"
  key_pair = "my_key_pair_name"
  security_groups = ["default"]

  block_device {
    uuid = "<image-id>"
    source_type = "image"
    volume_size = 5
    boot_index = 0
    destination_type = "volume"
    delete_on_termination = true
  }

  network {
    name = "my_network"
  }
}

Boot From an Existing Volume

resource "openstack_blockstorage_volume_v1" "myvol" {
  name = "myvol"
  size = 5
  image_id = "<image-id>"
}

resource "openstack_compute_instance_v2" "boot-from-volume" {
  name = "bootfromvolume"
  flavor_id = "3"
  key_pair = "my_key_pair_name"
  security_groups = ["default"]

  block_device {
    uuid = "${openstack_blockstorage_volume_v1.myvol.id}"
    source_type = "volume"
    boot_index = 0
    destination_type = "volume"
    delete_on_termination = true
  }

  network {
    name = "my_network"
  }
}

Instance With Multiple Networks

resource "openstack_compute_floatingip_v2" "myip" {
  pool = "my_pool"
}

resource "openstack_compute_instance_v2" "multi-net" {
  name = "multi-net"
  image_id = "ad091b52-742f-469e-8f3c-fd81cadf0743"
  flavor_id = "3"
  key_pair = "my_key_pair_name"
  security_groups = ["default"]

  network {
    name = "my_first_network"
  }

  network {
    name = "my_second_network"
    floating_ip = "${openstack_compute_floatingip_v2.myip.address}"
    # Terraform will use this network for provisioning
    access_network = true
  }
}

Instance With Personality

resource "openstack_compute_instance_v2" "personality" {
  name = "personality"
  image_id = "ad091b52-742f-469e-8f3c-fd81cadf0743"
  flavor_id = "3"
  key_pair = "my_key_pair_name"
  security_groups = ["default"]

  personality {
    file = "/path/to/file/on/instance.txt
    content = "contents of file"
  }

  network {
    name = "my_network"
  }
}

Instance with Multiple Ephemeral Disks

resource "openstack_compute_instance_v2" "multi-eph" {
  name = "multi_eph"
  image_id = "ad091b52-742f-469e-8f3c-fd81cadf0743"
  flavor_id = "3"
  key_pair = "my_key_pair_name"
  security_groups = ["default"]

  block_device {
    boot_index = 0
    delete_on_termination = true
    destination_type = "local"
    source_type = "image"
    uuid = "<image-id>"
  }

  block_device {
    boot_index = -1
    delete_on_termination = true
    destination_type = "local"
    source_type = "blank"
    volume_size = 1
  }

  block_device {
    boot_index = -1
    delete_on_termination = true
    destination_type = "local"
    source_type = "blank"
    volume_size = 1
  }
}

Argument Reference

The following arguments are supported:

The network block supports:

The block_device block supports:

The volume block supports:

The scheduler_hints block supports:

The personality block supports:

Attributes Reference

The following attributes are exported:

Notes

Floating IPs

Floating IPs can be associated in one of two ways:

Only one of the above methods can be used.

Multiple Ephemeral Disks

It’s possible to specify multiple block_device entries to create an instance with multiple ephemeral (local) disks. In order to create multiple ephemeral disks, the sum of the total amount of ephemeral space must be less than or equal to what the chosen flavor supports.

The following example shows how to create an instance with multiple ephemeral disks:

resource "openstack_compute_instance_v2" "foo" {
  name = "terraform-test"
  security_groups = ["default"]

  block_device {
    boot_index = 0
    delete_on_termination = true
    destination_type = "local"
    source_type = "image"
    uuid = "<image uuid>"
  }

  block_device {
    boot_index = -1
    delete_on_termination = true
    destination_type = "local"
    source_type = "blank"
    volume_size = 1
  }

  block_device {
    boot_index = -1
    delete_on_termination = true
    destination_type = "local"
    source_type = "blank"
    volume_size = 1
  }
}

Instances and Ports

Neutron Ports are a great feature and provide a lot of functionality. However, there are some notes to be aware of when mixing Instances and Ports:

resource "openstack_networking_port_v2" "port_1" {
  name = "port_1"
  admin_state_up = "true"

  network_id = "0a1d0a27-cffa-4de3-92c5-9d3fd3f2e74d"
  security_group_ids = [
    "2f02d20a-8dca-49b7-b26f-b6ce9fddaf4f",
    "ca1e5ed7-dae8-4605-987b-fadaeeb30461",
  ]

}

resource "openstack_compute_instance_v2" "instance_1" {
  name        = "instance_1"

  network {
    port = "${openstack_networking_port_v2.port_1.id}"
  }

  connection {
    user = "root"
    host = "${openstack_networking_port_v2.port_1.fixed_ip.0.ip_address}"
    private_key = "~/path/to/key"
  }

  provisioner "remote-exec" {
    inline = [
      "echo terraform executed > /tmp/foo"
    ]
  }
}

See the source of this document at Terraform.io