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.
- Optional: configure the provider.
- Get the prebuilt configuration ID.
- Get the location ID.
- Get the OS image.
- 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
-
Make sure that in the control panel you have created a service user with the
memberrole in the Account scope andiam.admin. -
Create a directory to store configuration files and a separate file with the
.tfextension to configure providers. -
Add the Selectel provider to the provider configuration file:
terraform {required_providers {selectel = {source = "selectel/selectel"version = "~> 7.1.0"}}}Where
versionis 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.
-
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 thememberrole in the Account scope andiam.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 theiam.adminrole);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 formatru-9; do not use pools in the formatSPB-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.
-
Create a project:
resource "selectel_vpc_project_v2" "project_1" {name = "project"}View the detailed description of the selectel_vpc_project_v2 resource.
-
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.
-
Open the CLI.
-
Initialize the Terraform configuration in the directory:
terraform init -
Check that the configuration files are free of errors:
terraform validate -
Format the configuration files:
terraform fmt -
Check which resources will be created:
terraform plan -
Apply the changes and create the resources:
terraform apply -
Confirm the creation — enter yes and press Enter. The created resources will be displayed in the control panel.
-
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:
nameis 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:
nameis the pool where the dedicated server will be created, for example,SPB-2. You can find the list of available pools in the Product availability by location instructions.
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:
-
nameis the OS family name, for example,Ubuntu. You can create a server without an OS; to do this, specifynoosand do not specify the version. You can find the list of available OSs in the OS images for installation instructions; -
version_valueis 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.