Skip to main content

Create a prebuilt dedicated server using Terraform

Last update:

We recommend creating resources in order. If you create all resources at once, Terraform will account for dependencies between resources that you specified in the configuration file. If dependencies are not specified, resources will be created in parallel, which may lead to errors. For instance, a resource required for creating another resource might not have been created yet.

To assign a public dedicated IP address to a server, use the instructions Create a prebuilt dedicated server with a public dedicated IP address.


  1. Optional: configure the provider.
  2. Get the ID of the prebuilt configuration.
  3. Get the location ID.
  4. Get the OS image.
  5. Create a dedicated server.

Configuration files

Example file for provider configuration
terraform {
required_providers {
selectel = {
source = "selectel/selectel"
version = "~> 7.1.0"
}
}
}

provider "selectel" {
domain_name = "123456"
username = "user"
password = "password"
auth_region = "ru-1"
auth_url = "https://cloud.api.selcloud.ru/identity/v3/"
}
Example file for creating a prebuilt dedicated server
data "selectel_dedicated_configuration_v1" "server_config" {
project_id = selectel_vpc_project_v2.project_1.id
deep_filter = <<EOT
{
"name": "CL25-SSD"
}
EOT
}

data "selectel_dedicated_location_v1" "server_location" {
project_id = selectel_vpc_project_v2.project_1.id
filter {
name = "SPB-2"
}
}

data "selectel_dedicated_os_v1" "server_os" {
project_id = selectel_vpc_project_v2.project_1.id
filter {
name = "Ubuntu"
version_value = "2404"
configuration_id = data.selectel_dedicated_configuration_v1.server_config.configurations[0].id
location_id = data.selectel_dedicated_location_v1.server_location.locations[0].id
}
}

resource "selectel_dedicated_server_v1" "server_1" {
project_id = selectel_vpc_project_v2.project_1.id
configuration_id = data.selectel_dedicated_configuration_v1.server_config.configurations[0].id
location_id = data.selectel_dedicated_location_v1.server_location.locations[0].id
os_id = data.selectel_dedicated_os_v1.server_os.os[0].id
price_plan_name = "1 day"
}

1. Optional: configure the provider

  1. Make sure that in the control panel you have created a service user with the member role in the Account access scope and iam.admin.

  2. Create a directory to store configuration files and a separate file with the .tf extension to configure providers.

  3. Add the Selectel provider to the provider configuration file:

    terraform {
    required_providers {
    selectel = {
    source = "selectel/selectel"
    version = "~> 7.1.0"
    }
    }
    }

    Here version is the provider version. You can check the current version in the Selectel documentation (in the Terraform Registry and GitHub).

    Learn more about products, services, and features that can be managed using providers in the Selectel and OpenStack Providers guide.

  4. Initialize the Selectel provider:

    provider "selectel" {
    domain_name = "123456"
    username = "user"
    password = "password"
    auth_region = "ru-9"
    auth_url = "https://cloud.api.selcloud.ru/identity/v3/"
    }

    Where:

    • domain_name — Selectel account number. You can find it in the control panel in the top-right corner;
    • username — username of a service user with the member role in the Account access scope and iam.admin. You can find it in the control panel: in the top menu, click IAMService users section (this section is available only to the account owner and to users with the iam.admin role);
    • password — service user password. You can view it when creating the user or change it to a new one;
    • auth_regionpool for authorization, for example ru-9. You can create resources in other pools. The list of available pools can be viewed in the Availability Matrix.
  5. Create a project:

    resource "selectel_vpc_project_v2" "project_1" {
    name = "project"
    }

    See the detailed description of the selectel_vpc_project_v2 resource.

  6. Create a service user to access the project and assign the member role in the Project access scope:

    resource "selectel_iam_serviceuser_v1" "serviceuser_1" {
    name = "username"
    password = "password"
    role {
    role_name = "member"
    scope = "project"
    project_id = selectel_vpc_project_v2.project_1.id
    }
    }

    Where:

    • username — username;

    • password — user password. The password must be at least 20 characters long and include at least:

      • one uppercase and one lowercase Latin letter (A-Z, a-z);
      • one digit (0-9);
      • one special character from the list of ASCII Printable 7-Bit Special Characters:
        !"#$%&'()*+,-./:;<=>?@[]^_{|}~;
    • project_id — project ID. You can find it in the control panel: in the top menu, click Products and select Cloud Servers → open the projects menu → in the row of the required project, click .

    See the detailed description of the selectel_iam_serviceuser_v1 resource.

  7. Optional: if you want to use a mirror, create a separate Terraform CLI configuration file and add the following block to it:

    provider_installation {
    network_mirror {
    url = "https://tf-proxy.selectel.ru/mirror/v1/"
    include = ["registry.terraform.io/*/*"]
    }
    direct {
    exclude = ["registry.terraform.io/*/*"]
    }
    }

    Read more about mirror settings in the CLI Configuration File section of the HashiCorp documentation.

  8. Open the CLI.

  9. Initialize the Terraform configuration in the directory:

    terraform init
  10. Verify that the configuration files contain no errors:

    terraform validate
  11. Format the configuration files:

    terraform fmt
  12. Check which resources will be created:

    terraform plan
  13. Apply the changes and create the resources:

    terraform apply
  14. Confirm the creation — type yes and press Enter. Created resources will be displayed in the control panel.

  15. If you do not have enough quota to create resources, increase your quotas.

2. Get the ID of the prebuilt configuration

data "selectel_dedicated_configuration_v1" "server_config" {
project_id = selectel_vpc_project_v2.project_1.id
deep_filter = <<EOT
{
"name": "EL50-SSD"
}
EOT
}

Here deep_filter is a filter for the list of prebuilt configurations:

  • name — name of the prebuilt configuration, e.g. EL50-SSD. You can view it in the Control Panel: in the top menu, click Products and select Dedicated Servers. If there are created servers in the project, in the Servers section, click Order Server. If there are no servers in the project, the order page will open automatically.

See the detailed description of the data source selectel_dedicated_configuration_v1.

See the detailed description of the resource selectel_vpc_project_v2.

3. Get the location ID

data "selectel_dedicated_location_v1" "server_location" {
project_id = selectel_vpc_project_v2.project_1.id
filter {
name = "SPB-2"
}
}

Here filter is a filter for the list of locations:

  • namepool in which the dedicated server will be created, e.g. SPB-2. The list of available pools can be viewed in the Availability Matrix.

See the detailed description of the data source selectel_dedicated_location_v1.

4. Get the OS image

data "selectel_dedicated_os_v1" "server_os" {
project_id = selectel_vpc_project_v2.project_1.id
filter {
name = "Ubuntu"
version_value = "2404"
configuration_id = data.selectel_dedicated_configuration_v1.server_config.configurations[0].id
location_id = data.selectel_dedicated_location_v1.server_location.locations[0].id
}
}

Here filter is a filter for the list of OSs:

  • name — name of the OS family, e.g. Ubuntu. You can create a server without an OS; to do this, specify noos and do not specify a version. The list of available OSs can be viewed in the OS images for installation;

  • version_value — image version. You can view the OS image version using the API method List OS configurations.

See the detailed description of the data source selectel_dedicated_os_v1.

5. Create a dedicated server

resource "selectel_dedicated_server_v1" "server_1" {
project_id = selectel_vpc_project_v2.project_1.id
configuration_id = data.selectel_dedicated_configuration_v1.server_config.configurations[0].id
location_id = data.selectel_dedicated_location_v1.server_location.locations[0].id
os_id = data.selectel_dedicated_os_v1.server_os.os[0].id
price_plan_name = "1 day"
}

Here price_plan_name is the tariff plan, e.g. 1 day. You can view the tariff plan names using the API method Price Plans; more information about tariff plans can be found in the Payment model and dedicated server prices. Available values:

  • 1 day;
  • 1 month;
  • 3 months;
  • 6 months;
  • 12 months;
  • 12 months • monthly payment.

See the detailed description of the resource selectel_dedicated_server_v1.