VK Cloud logo
Updated at April 15, 2024   11:13 AM

Creating a DB instance

The article provides examples of creating database instances with various settings using Terraform.

Preparatory steps

  1. Check out the available resources and quotas for the region in which you plan to create a DB instance. Different quotas can be set up for different regions.

    If you want to increase quotas, contact technical support.

  2. Install Terraform and configure the provider, if not already done.

    Put the provider settings in the Terraform configuration file provider.tf.

1. Create a file with a description of the DB instance

In all the examples below, DB instances are created with the following properties:

  • Region: by default (the same as the project region).
  • Availability zone: by default (for the Moscow region — GZ1).
  • DBMS type and version: MySQL 8.0.
  • Configuration of the DB instance: Single.
  • External IP address: assigned.

Select one of the cluster creation examples and create a Terraform configuration file main.tf with the appropriate content:

The flavor for the instance VM is set via the db-instance-flavor variable.

variable "db-instance-flavor" {  type    = string  default = "STD3-2-6"}data "vkcs_compute_flavor" "db" {  name = var.db-instance-flavor}resource "vkcs_networking_network" "db" {  name           = "db-net"  admin_state_up = true}resource "vkcs_networking_subnet" "db-subnetwork" {  name            = "db-subnet"  network_id      = vkcs_networking_network.db.id  cidr            = "10.100.0.0/16"  dns_nameservers = ["8.8.8.8", "8.8.4.4"]}data "vkcs_networking_network" "extnet" {  name = "internet"}resource "vkcs_networking_router" "db-router" {  name                = "db-router"  admin_state_up      = true  external_network_id = data.vkcs_networking_network.extnet.id}resource "vkcs_networking_router_interface" "db" {  router_id = vkcs_networking_router.db-router.id  subnet_id = vkcs_networking_subnet.db-subnetwork.id}resource "vkcs_db_instance" "db-instance" {  name        = "db-instance"  datastore {    type    = "mysql"    version = "8.0"  }  floating_ip_enabled = true  flavor_id   = data.vkcs_compute_flavor.db.id  size        = 8  volume_type = "ceph-ssd"  disk_autoexpand {    autoexpand    = true    max_disk_size = 1000  }  network {    uuid = vkcs_networking_network.db.id  }}

2. Create resources using Terraform

  1. Put the Terraform configuration files terraform.rc, vkcs_provider.tf, main.tf and secret.tfvars (if created) in the same directory.

  2. Go to this directory.

  3. Run the command:

    terraform init

    Wait for Terraform initialization to complete.

  4. Run the command:

    terraform apply

    When prompted for confirmation, print yes.

  5. Wait for the operation to complete.

3. Check the configuration application

Go to your VK Cloud personal account, DatabasesDatabases instances. Make sure that all the objects described in the configuration have been created successfully:

  • The DB instance.
  • The external IP address for the instance — is displayed on the instance page.
  • The Prometheus Node exporter extension (if installed) — is displayed on the instance page on the Extensions tab.
  • DB on the instance (if created) — is displayed on the instance page on the List of databases tab.
  • DB user (if created) — is displayed on the instance page on the Users tab.

Delete unused resources

Some objects created in this scenario consume resources. If you no longer need them, delete them:

  1. Go to the directory with the Terraform configuration files.

  2. Run the command:

    terraform destroy

    When prompted for confirmation, print yes.

  3. Wait for the operation to complete.