VK Cloud logo
Updated at April 15, 2024   08:50 AM

Configuration files

When creating resources, Terraform works with configuration files. They have the .tf extension.

It is recommended to create a separate working directory for each VK Cloud project. The following files are placed in the working directory:

  • the provider config file (project-specific);
  • the configuration files of the resources you are planning to create.

For correct Terraform operation, an additional file is required — the Terraform mirror config file.

The Terraform provider config file

The file contains the settings of Terraform providers and user authentication details for the current project.

The file can have any name. It is recommended to name it provider.tf to highlight that the file contains the key settings for connecting to VK Cloud.

The file must be present in all Terraform working directories.

The file is available for download in your personal account, on the Project settings page, on the Terraform tab. The downloaded file has the name vkcs_provider.tf and contains the settings of one provider (vkcs). After downloading the file, edit it — specify your account password in the password argument.

An example of the file contents:

terraform {    required_providers {        vkcs = {            source = "vk-cs/vkcs"        }    }}provider "vkcs" {    # Your user account.    username = "<user_email>"    # The password of the account    password = "<user_password>"    # The tenant token can be taken from the project Settings tab - > API keys.    # Project ID will be our token.    project_id = "<project_ID>"    # Region name    region = "<Region>"    auth_url = "<auth_url>"}

The file consists of the following sections:

  • terraform.required_providers {} — a list of Terraform providers required for operation. To work with VK Cloud, the vkcs provider must be specified, its source and versions. If you are going to use additional providers, add them to this section.

  • provider "vkcs" {} — settings for user authentication in the project:

    • password: specify your account password;
    • username, project_id, region: the values in the file that is downloaded from the personal account are valid for working with the current project. These values can also be obtained in your personal account, on the Project settings page, on the Terraform tab.

    The values of some arguments depend on the region:

    • region:

      • for Moscow region: RegionOne;
      • for Kazakhstan region: kz;
    • auth_url:

      • for Moscow region: https://infra.mail.ru:35357/v3/;
      • for Kazakhstan region: https://kz.infra.mail.ru:35357/v3/.

Resources configuration files

Resource configuration files describe the resources created via Terraform, their settings and dependencies.

Resource configuration files can have any names. Examples in this documentation use the following naming standards:

  • variables.tf — variables that are used in the configuration. Setting frequently changed parameters as variables makes it easier to change the infrastructure configuration for a new project.
  • main.tf — contains a description of the main resource that is created in a scenario. For example, in the scenario for creating a virtual machine the main.tf file should typically contain the description of a virtual machine.

Sometimes files are named after the types of the resources they describe:

  • network.tf — the description of the cloud network.
  • kubernetes.tf — the description of Kubernetes resources.

A typical configuration contains:

  • resource "resource_type" "resource_name" {} — describes the resource being created, such as a network, a subnet, a Kubernetes cluster, or a database cluster.
  • data "data_type" "data_name" {} — allows you to use data specified outside the Terraform configuration that exists in the cloud or locally. For example, VM configuration, cluster template/version, etc.
  • variable "parameter" {}input variables. Used to declare variables that are used in the configuration (variables.tf).
  • output "parameter" {}output variables. Used to output data to the command line.

To determine the sequence of creating resources and their dependencies, you can use the depends_on meta-argument. The meta-argument indicates the resource on which the resource being created depends:

depends_on = [    vkcs_kubernetes_cluster.k8s-cluster,]

First, the resource set by the meta-argument is created, then the resource for which the dependency is specified is created.

The Terraform mirror config file

The file is required for Terraform startup, it contains the address of the VK Cloud Terraform mirror. The file must be named terraform.rc for Windows and .terraformrc for other OS. The file content is the same for all projects and regions.

The file is available for download on the Project settings page in your personal account, on the Terraform tab. After downloading, the file can be used as is.

The file must be located:

  • For Windows — in the Application Data directory of the user.
  • For other OS — in the home directory of the user.

The file content for VK Cloud projects:

provider_installation {    network_mirror {        url = "https://terraform-mirror.mcs.mail.ru"        include = ["registry.terraform.io/*/*"]    }    direct {        exclude = ["registry.terraform.io/*/*"]    }}