Creating a VM
To create a virtual machine, you need to describe the resources of the virtual network in which the VM will work. For this:
-
Create a file
network.tf
, which will describe the configuration of the network being created. -
Add the text from the example below and correct the settings for your network. To create a VM, you need the following network objects:
-
Resources:
- vkcs_networking_network — network where the VM will be created. In the example below, a network is created with the name "compute-net".
- vkcs_networking_subnet - subnet from the network. In the example: subnet_1.
- vkcs_networking_router - a router for an external network and interaction with the outside world. In the example: vm_router.
- vkcs_networking_router_interface - connect the router to the internal network.
-
Data sources:
- vkcs_networking_network - external network for obtaining public IP (Floating IP).
-
Example network.tf
file:
1data "vkcs_networking_network" "extnet" { 2 name="extnet" 3} 4 5resource "vkcs_networking_network" "compute" { 6 name="compute net" 7} 8 9resource "vkcs_networking_subnet" "compute" { 10 name="subnet_1" 11 network_id = vkcs_networking_network.compute.id 12 cidr="192.168.199.0/24" 13} 14 15resource "vkcs_networking_router" "compute" { 16 name="vm-router" 17 admin_state_up = true 18 external_network_id = data.vkcs_networking_network.extnet.id 19} 20 21resource "vkcs_networking_router_interface" "compute" { 22 router_id = vkcs_networking_router.compute.id 23 subnet_id = vkcs_networking_subnet.compute.id 24}
To create a virtual machine, create an main.tf
file in the directory and add the following example text. Correct the setting values in the example according to your VM.
-
Resources:
-
vkcs_compute_instance - information about the created VM instance. This resource describes the VM and uses the previously described network resources:
-
name — VM name.
-
flavor_id — VM flavor used during creation.
-
security_groups — list of security group names assigned to this VM.
-
availability_zone — availability zone of this VM.
-
block_device — virtual disk for the created VM. In this example, two disks are created: one from the boot image, the other is empty. The
block_device
resource is described by the following parameters:-
uuid is a unique disk identifier.
-
source_type — OS boot source.
-
destination_type is the destination of the boot image.
-
volume_type is the volume type of the boot image target. To get a list of available types, run the
openstack volume type list
command. -
volume_size - volume block size of the boot image target.
-
boot_index - disk location in boot boot order.
-
delete_on_termination - If set to
True
, the disk will be deleted when the VM is deleted. -
network — network connected when creating a VM.
-
depends_on - The VM will not start until the specified resources are created.
-
-
vkcs_networking_floatingip - Gets an available floating IP ID from VK Cloud. Includes the following resource:
- pool is the name of the pool that the floating IP belongs to.
-
vkcs_compute_floatingip_associate - Assigns a floating IP to the created VM. Includes the following resources:
- floating_ip — ID of the floating IP that will be assigned to the VM.
- *instance_id — ID of the VM to which the floating IP will be assigned.
-
-
Data sources:
- vkcs_images_image - installation image for the created instance.
- vkcs_compute_flavor - flavor (CPU, RAM, Disk) of the VM. You can see it in the VM creation wizard through your personal account.
- output "instance_fip" - outputs the floating IP assigned to the VM to the console.
An example instance.tf
file:
1data "vkcs_compute_flavor" "compute" { 2 name="Basic-1-2-20" 3} 4 5data "vkcs_images_image" "compute" { 6 name="Ubuntu-18.04-Standard" 7} 8 9resource "vkcs_compute_instance" "compute" { 10 name="compute-instance" 11 flavor_id = data.vkcs_compute_flavor.compute.id 12 security_groups = ["default"] 13 availability_zone = "GZ1" 14 15 block_device { 16 uuid = data.vkcs_images_image.compute.id 17 source_type="image" 18 destination_type = "volume" 19 volume_type = "ceph-ssd" 20 volume_size = 8 21 boot_index = 0 22 delete_on_termination = true 23 } 24 25 block_device { 26 source_type="blank" 27 destination_type = "volume" 28 volume_type = "ceph-ssd" 29 volume_size = 8 30 boot_index = 1 31 delete_on_termination = true 32 } 33 34 network{ 35 uuid = vkcs_networking_network.compute.id 36 } 37 38 depends_on = [ 39 vkcs_networking_network.compute, 40 vkcs_networking_subnet.compute 41 ] 42} 43 44resource "vkcs_networking_floatingip" "fip" { 45 pool = data.vkcs_networking_network.extnet.name 46} 47 48resource "vkcs_compute_floatingip_associate" "fip" { 49 floating_ip = vkcs_networking_floatingip.fip.address 50 instance_id = vkcs_compute_instance.compute.id 51} 52 53output "instance_fip" { 54 value = vkcs_networking_floatingip.fip.address 55}
To apply the changes, add the network.tf
and main.tf
files to your working directory and run the following commands:
terraform init
terraform apply