Skip to main content

Create a prebuilt dedicated server using Terraform

We recommend creating resources sequentially. If you create all resources at the same time, Terraform will take into account the 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 example, a resource required to create another resource may not be created yet.

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


  1. Optional: configure the provider.
  2. Get the prebuilt configuration ID.
  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 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"
    }
    }
    }

    Where version is provider versions. You can find the latest version in the Selectel documentation (in Terraform Registry and GitHub).

    For more information on products and services that can be managed using providers, see the Selectel and OpenStack Providers manual.

  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 upper-right corner;
    • username — name of the service user with the member role in the Account scope and iam.admin. You can find it in the control panel: in the top menu, click IAM → Service Users (the section is available only to the Account Owner and a user 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_region — pool for authorization in the format ru-9; do not use pools in the format SPB-2. The authorization pool might not match the pool where you create resources. You can view the list of available pools in the Product Availability by Location manual.
  5. Create a project:

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

    View the detailed description of the selectel_vpc_project_v2 resource.

  6. 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/*/*"]
    }
    }

    For more information on mirror configuration, see the CLI Configuration File manual in the HashiCorp documentation.

  7. Open the CLI.

  8. Initialize the Terraform configuration in the directory:

    terraform init
  9. Check that the configuration files are free of errors:

    terraform validate
  10. Format the configuration files:

    terraform fmt
  11. Check which resources will be created:

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

    terraform apply
  13. Confirm the creation — enter yes and press Enter. The created resources will be displayed in the control panel.

  14. If the quotas are insufficient to create resources, increase the 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
}

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

  • name is the prebuilt configuration name, for example, EL50-SSD. You can find 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 selectel_dedicated_configuration_v1 data source.

See the detailed description of the selectel_vpc_project_v2 resource.

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"
}
}

Where filter is a filter for the list of locations:

See the detailed description of the selectel_dedicated_location_v1 data source.

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
}
}

Where filter is a filter for the list of OSs:

  • name is the OS family name, for example, Ubuntu. You can create a server without an OS; to do this, specify noos and do not specify the version. You can find the list of available OSs in the OS images for installation instructions;

  • version_value is the image version. You can find the OS image version using the List OS configurations API method.

See the detailed description of the selectel_dedicated_os_v1 data source.

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"
}

Where price_plan_name is the billing plan, for example, 1 day. You can find the billing plan names using the Price Plans API method, learn more about billing plans in the Dedicated server payment model and prices instructions. Available values:

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

See the detailed description of the selectel_dedicated_server_v1 resource.