Create a prebuilt dedicated server with a public dedicated IP address via Terraform
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.
- Optional: configure the provider.
- Connect additional public IP addresses.
- Get the ID of the prebuilt configuration.
- Get the location ID.
- Get the OS image.
- Select a public subnet.
- 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
}
}
data "selectel_dedicated_public_subnet_v1" "public_subnets" {
project_id = selectel_vpc_project_v2.project_1.id
filter {
location_id = data.selectel_dedicated_location_v1.server_location.locations[0].id
subnet = "203.0.113.0/28"
ip = "203.0.113.12"
}
}
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
public_subnet_id = data.selectel_dedicated_public_subnet_v1.public_subnets.subnets[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 access 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"}}}Here
version— provider versions. You can check the current version in the Selectel documentation (in the Terraform Registry and on GitHub).Learn more about products, services, and features that can be managed using providers in the Selectel and OpenStack Providers guide.
-
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 view it in the control panel in the top right corner;username— name of the service user withmemberroles in the Account access scope andiam.admin. You can view it in the control panel: in the top menu, click IAM → Service Users section (the section is only available to the Account Owner and users 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, for example,ru-9. You can create resources in other pools. A list of available pools can be viewed in the Availability Matrix instruction.
-
Create a project:
resource "selectel_vpc_project_v2" "project_1" {name = "project"}See the detailed description of the selectel_vpc_project_v2 resource.
-
Create a service user to access the project and assign the
memberrole 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:
!"#$%&'()*+,-./:;<=>?@[]^_{|}~;
- one uppercase and one lowercase Latin letter (
-
project_id— project ID. You can view it in the control panel: in the top menu, click Products and select Cloud Servers → open the projects menu → in the row for the required project, click .
See the detailed description of the selectel_iam_serviceuser_v1 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/*/*"]}}Read more about mirror settings in the CLI Configuration File section of the HashiCorp documentation.
-
Open the CLI.
-
Initialize the Terraform configuration in the directory:
terraform init -
Verify that the configuration files contain no 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 creation — enter yes and press Enter. The created resources will be displayed in the control panel.
-
If you do not have enough quota to create resources, increase your quotas.
2. Connect additional public IP addresses
Use the Connect additional public IP addresses instruction.
3. 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 fixed configurations:
name— configuration name, for example,EL50-SSD. You can view it in the control panel: in the top menu, click Products and select Dedicated Servers. If the project already has servers, 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.project_1 resource.
4. 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:
name— pool where the dedicated server will be created, for exampleSPB-2. The list of available pools can be found in the Availability Matrix instructions.
See the detailed description of the selectel_dedicated_location_v1 data source.
5. 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— the OS family name, for exampleUbuntu. You can create a server without an OS; to do this, specifynoosand do not specify a version. The list of available OSs can be found in the OS Images for Installation instructions;version_value— image version. You can view the OS image version using the API method List OS configurations.
See the detailed description of the selectel_dedicated_os_v1 data source.
6. Select a public subnet
data "selectel_dedicated_public_subnet_v1" "public_subnets" {
project_id = selectel_vpc_project_v2.project_1.id
filter {
location_id = data.selectel_dedicated_location_v1.server_location.locations[0].id
subnet = "203.0.113.0/28"
ip = "203.0.113.12"
}
}
Where:
location_id— ID of the location of the dedicated public subnet. The public subnet location must match the location where the server is created;subnet— (Optional) the address of the dedicated public subnet that you ordered at step 2. If you do not specify the public subnet address, any available public subnet for the location will be selected. You can view it in the control panel: in the top menu click Products → Dedicated Servers → Network → Public Subnets tab;ip— (Optional) an available IP address from the dedicated public subnet that you ordered at step 2. If you do not specify an IP address, any available IP address from the public subnet will be selected.
7. 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
public_subnet_id = data.selectel_dedicated_public_subnet_v1.public_subnets.subnets[0].id
os_id = data.selectel_dedicated_os_v1.server_os.os[0].id
price_plan_name = "1 day"
}
Here price_plan_name — the price plan, for example, 1 day. You can view the price plan names using the API method Price Plans; more information about price plans is in the Payment models and prices for a dedicated server 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.
See the detailed description of the selectel_dedicated_public_subnet_v1 data source.