Infrastructure description
To create a VM from the service boot image:
-
Get the VM virtual network data. Use the
vkcs_networking_subnetdata source.Example of getting virtual network datadata "vkcs_networking_subnet" subnet {# Subnet IDid = var.ds-subnet} -
If necessary, create a security group. Use the resources:
-
vkcs_networking_secgroup— the security group that will include the access rules. -
vkcs_networking_secgroup_rule— access rules for the security group.Example of creating a security groupresource "vkcs_networking_secgroup" "secgroup" {name = "security_group"description = "terraform security group"# SDN typesdn = data.vkcs_networking_subnet.subnet.sdn}resource "vkcs_networking_secgroup_rule" "rules" {# Defining the direction of rule application — for incoming (ingress) or outgoing (egress) connectionsdirection = "ingress"# Access port rangeport_range_max = 22port_range_min = 22# Access protocolprotocol = "tcp"# ID of the security group for which the rule is createdsecurity_group_id = vkcs_networking_secgroup.secgroup.iddescription = "secgroup_rule"}
For more information about SDN, see Using the SDN type in Terraform resources.
-
-
Create the root disk. Use the
vkcs_blockstorage_volumeresource.For the service image ID, specify the ID of the previously created image (for more details, see Creating a service image) displayed in the cloud platform management console.
In the resource, specify the disk metadata:
metadata = { "sid" : "xaas", "product" : "<SERVICE_NAME>" }Here
<SERVICE_NAME>is the service name.Example of creating a root diskresource "vkcs_blockstorage_volume" "boot" {name = "root"# Metadatametadata = { "sid" : "xaas", "product" : "test service" }# Service image IDimage_id = 2a7a77db-d902-49bc-9198-531bd646d86fvolume_type = "ceph-ssd"size = 10availability_zone = "ME1"} -
Create a separate volume for storing service data. Use the
vkcs_blockstorage_volumeresource. In it, specify the disk metadata the same way as for the root disk.Example of creating a separate disk volumeresource "vkcs_blockstorage_volume" "data" {name = "volume"# Metadatametadata = { "sid" : "xaas", "product" : "test service" }# Disk volume size, GBsize = 1# Availability zoneavailability_zone = "ME1"# Disk volume typevolume_type = "ceph-ssd"} -
Create a VM key pair. Use the
ivkcs_ssh_keypairresource.Example of creating a key pairresource "ivkcs_ssh_keypair" "keypair" {} -
Create a VM from the service boot image and install the agent. Use the resources:
-
vkcs_compute_instance. Specify the VM metadata the same way as for the root disk. This will allow the service instance VM to be displayed in the cloud platform management console on the Virtual machines page in a separate block for marketplace VMs. -
ivkcs_user_data.Example of creating a VM from the service boot image# Service deployment IDvariable "instance_uuid" {type = string}# Creating a cloud-config configuration. Getting data for initializing the agent on the hostresource "ivkcs_user_data" "init" {# Service deployment IDuuid = var.instance_uuid# Host names for which cloud-config must be generated and where the agent will be installedhosts = ["compute-instance"]# Target OStarget_os = "almalinux9"# SSH access keysssh_authorized_keys = [ivkcs_ssh_keypair.keypair.public_key,]}resource "vkcs_compute_instance" "compute" {name = "compute-instance"# VM flavor IDflavor_id = var.ds-flavor# List of security group IDs for the VMsecurity_group_ids = [vkcs_networking_secgroup.secgroup.id]# VM availability zoneavailability_zone = "ME1"# Metadatametadata = { "sid" : "xaas", "product" : "test service" }# Root diskblock_device {# Service image IDuuid = vkcs_blockstorage_volume.boot.id# Boot sourcesource_type = "volume"# Disk type. Specify volume (persistent)destination_type = "volume"# Disk volume typeboot_index = 0# If set to true, the disk will be deleted when the VM is deleteddelete_on_termination = true}# Applying the cloud-config configuration to configure the VM. Installing the agent# In [], specify the host number. If the service is deployed on a single VM, the value must be 0user_data = ivkcs_user_data.init.user_data[0]# Attempt to stop the VM before deletionstop_before_destroy = true# VM creation timeouttimeouts {create = "10m"}}
-
-
Attach the separate data volume to the VM. Use the
vkcs_compute_volume_attachresource.Example of attaching a disk volume to a VMresource "vkcs_compute_volume_attach" "attached" {# ID of the created VMinstance_id = vkcs_compute_instance.compute.id# ID of the disk volume to be attachedvolume_id = vkcs_blockstorage_volume.data.id# Dependencies. The disk will be attached to the VM only after the resources listed in the dependencies are createddepends_on = [# VMvkcs_compute_instance.compute,]} -
Pass the VM data (for example, the private SSH key) in the output parameters (for more details, see Description of output parameters).
The SDN type is defined in the vkcs_networking_subnet resource in the sdn parameter, which can take one of the following values:
neutron— SDN based on OpenStack Neutron.sprut— SDN based on Sprut.
The default value of the sdn parameter equals the SDN type set by default in the cloud platform project. If the SDN type based on Sprut is set in the default project, the default value of the sdn parameter will be sprut.
To use the subnet data, including the SDN type, in other manifest resources, retrieve them using the vkcs_networking_subnet data source of the VK CS provider.
data "vkcs_networking_subnet" "subnet" {# Subnet ID specified in the ds-subnet variableid = var.ds-subnet}
resource "vkcs_networking_secgroup" "secgroup" {name = "security_group"description = "terraform security group"sdn = data.vkcs_networking_subnet.subnet.sdn}
To get a pool of external IP addresses, use the vkcs_networking_floatingip resource of the VK CS provider. In the pool parameter, set the external network name in such a way that it is determined automatically depending on the SDN type:
- If the SDN is based on OpenStack Neutron —
ext-net. - If the SDN is based on Sprut —
internet.
resource "vkcs_networking_floatingip" "base_fip" {pool = data.vkcs_networking_subnet.subnet.sdn == "neutron" ? "ext-net" : "internet"}
To keep the VM IP address unchanged when the service is reinstalled, assign a fixed IP address to the VM:
-
Attach the IP address to the VM subnet port. Use the
vkcs_networking_portresource.Example of binding an IP address to a portresource "vkcs_networking_port" "port" {name = "port name"admin_state_up = "true"# Virtual network IDnetwork_id = data.vkcs_networking_subnet.subnet.network_id# ID of the security groups applied to the portsecurity_group_ids = [vkcs_networking_secgroup.secgroup.id]# SDN typesdn = data.vkcs_networking_subnet.subnet.sdnfixed_ip {# ID of the subnet from which the port IP address will be assignedsubnet_id = var.ds-subnet}}For more information about SDN, see Using the SDN type in Terraform resources.
-
In the
vkcs_compute_instanceresource, specify the port ID:resource "vkcs_compute_instance" "compute" {...network {port = vkcs_networking_port.port.id}}
To generate a password for accessing the service instance, use the random_password resource.
resource "random_password" "user" {# Whether to use special characters in the passwordspecial = false# Password lengthlength = 50}
To pass the generated password to the user, use an output parameter (for more details, see Description of output parameters).