Create a PostgreSQL cluster
We recommend create resources in order. If you create all the resources that are described in the configuration file The Terraform creates resources regardless of the order in which they are listed in the file.
- Optional: configure your ISPs.
- Create a private network and subnet.
- Create a cluster.
- Create a user.
- Create a database and assign an owner.
Configuration files
Example file for configuring providers
terraform {
required_providers {
selectel = {
source = "selectel/selectel"
version = "6.0.0"
}
openstack = {
source = "terraform-provider-openstack/openstack"
version = "2.1.0"
}
}
}
provider "selectel" {
domain_name = "123456"
username = "user"
password = "password"
}
resource "selectel_vpc_project_v2" "project_1" {
name = "project"
}
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
}
}
provider "openstack" {
auth_url = "https://cloud.api.selcloud.ru/identity/v3"
domain_name = "123456"
tenant_id = selectel_vpc_project_v2.project_1.id
user_name = selectel_iam_serviceuser_v1.serviceuser_1.name
password = selectel_iam_serviceuser_v1.serviceuser_1.password
region = "ru-9"
}
Example file for creating a fixed configuration PostgreSQL cluster
resource "openstack_networking_network_v2" "network_1" {
name = "private-network"
admin_state_up = "true"
}
resource "openstack_networking_subnet_v2" "subnet_1" {
name = "private-subnet"
network_id = openstack_networking_network_v2.network_1.id
cidr = "192.168.199.0/24"
}
data "selectel_dbaas_datastore_type_v1" "datastore_type_1" {
project_id = selectel_vpc_project_v2.project_1.id
region = "ru-9"
filter {
engine = "postgresql"
version = "14"
}
}
data "selectel_dbaas_flavor_v1" "flavor_1" {
project_id = selectel_vpc_project_v2.project_1.id
region = "ru-9"
filter {
datastore_type_id = data.selectel_dbaas_datastore_type_v1.datastore_type_1.datastore_types[0].id
fl_size = "standard"
vcpus = 4
ram = 16384
disk = 128
}
}
resource "selectel_dbaas_postgresql_datastore_v1" "datastore_1" {
name = "datastore-1"
project_id = selectel_vpc_project_v2.project_1.id
region = "ru-9"
type_id = data.selectel_dbaas_datastore_type_v1.datastore_type_1.datastore_types[0].id
subnet_id = selectel_vpc_subnet_v2.subnet.subnet_id
node_count = 3
flavor_id = data.selectel_dbaas_flavor_v1.flavor_1.flavors[0].id
pooler {
mode = "transaction"
size = 50
}
}
resource "selectel_dbaas_user_v1" "user_1" {
project_id = selectel_vpc_project_v2.project_1.id
region = "ru-9"
datastore_id = selectel_dbaas_postgresql_datastore_v1.datastore_1.id
name = "user"
password = "secret"
}
resource "selectel_dbaas_postgresql_database_v1" "database_1" {
project_id = selectel_vpc_project_v2.project_1.id
region = "ru-9"
datastore_id = selectel_dbaas_postgresql_datastore_v1.datastore_1.id
owner_id = selectel_dbaas_user_v1.user_1.id
name = "database_1"
}
Example file for creating a PostgreSQL cluster of arbitrary configuration
resource "openstack_networking_network_v2" "network_1" {
name = "private-network"
admin_state_up = "true"
}
resource "openstack_networking_subnet_v2" "subnet_1" {
name = "private-subnet"
network_id = openstack_networking_network_v2.network_1.id
cidr = "192.168.199.0/24"
}
data "selectel_dbaas_datastore_type_v1" "datastore_type_1" {
project_id = selectel_vpc_project_v2.project_1.id
region = "ru-9"
filter {
engine = "postgresql"
version = "14"
}
}
resource "selectel_dbaas_postgresql_datastore_v1" "datastore_1" {
name = "datastore-1"
project_id = selectel_vpc_project_v2.project_1.id
region = "ru-9"
type_id = data.selectel_dbaas_datastore_type_v1.datastore_type_1.datastore_types[0].id
subnet_id = selectel_vpc_subnet_v2.subnet.subnet_id
node_count = 3
flavor {
vcpus = 1
ram = 4096
disk = 32
}
pooler {
mode = "transaction"
size = 50
}
}
resource "selectel_dbaas_user_v1" "user_1" {
project_id = selectel_vpc_project_v2.project_1.id
region = "ru-9"
datastore_id = selectel_dbaas_postgresql_datastore_v1.datastore_1.id
name = "user"
password = "secret"
}
resource "selectel_dbaas_postgresql_database_v1" "database_1" {
project_id = selectel_vpc_project_v2.project_1.id
region = "ru-9"
datastore_id = selectel_dbaas_postgresql_datastore_v1.datastore_1.id
owner_id = selectel_dbaas_user_v1.user_1.id
name = "database_1"
}
optional: configure providers
If you're set up the ISPs Selectel and OpenStack, skip this step.
-
Make sure that in the control panel you created a service user with the Account Administrator and User Administrator roles.
-
Create a directory to store the configuration files and a separate file with the extension
.tf
to configure the ISPs. -
Add Selectel and OpenStack providers to the file to configure the providers:
terraform {
required_providers {
selectel = {
source = "selectel/selectel"
version = "6.0.0"
}
openstack = {
source = "terraform-provider-openstack/openstack"
version = "2.1.0"
}
}
}Here
version
— версии провайдеров. Актуальную версию можно посмотреть в документации Selectel (в Terraform Registry и GitHub) и OpenStack (в Terraform Registry и GitHub).Подробнее о продуктах, услугах и сервисах, которыми можно управлять с помощью провайдеров, в инструкции Провайдеры Selectel и OpenStack.
-
Инициализируйте провайдер Selectel:
provider "selectel" {
domain_name = "123456"
username = "user"
password = "password"
}Здесь:
domain_name
— номер аккаунта Selectel. Можно посмотреть в панели управления в правом верхнем углу;username
— имя сервисного пользователя с ролями Администратор аккаунта и Администратор пользователей. Можно посмотреть в панели управления: раздел Управление доступом → Управление пользователями → вкладка Сервисные пользователи (раздел доступен только Владельцу аккаунта и Администратору пользователей);password
— пароль сервисного пользователя. Можно посмотреть при создании пользователя или изменить на новый.
-
Создайте проект:
resource "selectel_vpc_project_v2" "project_1" {
name = "project"
}Посмотрите подробное описание ресурса selectel_vpc_project_v2.
-
Создайте сервисного пользователя для доступа к проекту и назначьте ему роль Администратор проекта:
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
}
}Здесь:
username
— имя пользователя;password
— пароль пользователя. Пароль должен быть не короче восьми символов и содержать латинские буквы разных регистров и цифры;project_id
— ID проекта. Можно посмотреть в панели управления: раздел Облачная платформа → откройте меню проектов (название текущего проекта) → в строке нужного проекта нажмите .
Посмотрите подробное описание ресурса selectel_iam_serviceuser_v1.
-
Инициализируйте провайдер OpenStack:
provider "openstack" {
auth_url = "https://cloud.api.selcloud.ru/identity/v3"
domain_name = "123456"
tenant_id = selectel_vpc_project_v2.project_1.id
user_name = selectel_iam_serviceuser_v1.serviceuser_1.name
password = selectel_iam_serviceuser_v1.serviceuser_1.password
region = "ru-9"
}Здесь:
domain_name
— номер аккаунта Selectel. Можно посмотреть в панели управления в правом верхнем углу;region
— пул, напримерru-9
. Все ресурсы будут создаваться в этом пуле. Список доступных пулов можно посмотреть в инструкции Матрицы доступности.
-
Если одновременно с настройкой провайдеров вы создаете ресурсы, то для ресурсов OpenStack добавьте аргумент
depends_on
. Например, для ресурса openstack_networking_network_v2:resource "openstack_networking_network_v2" "network_1" {
name = "private-network"
admin_state_up = "true"
depends_on = [
selectel_vpc_project_v2.project_1,
selectel_iam_serviceuser_v1.serviceuser_1
]
} -
Опционально: если вы хотите использовать зеркало, создайте отдельный конфигурационный файл Terraform CLI и добавьте в него блок:
provider_installation {
network_mirror {
url = "https://tf-proxy.selectel.ru/mirror/v1/"
include = ["registry.terraform.io/*/*"]
}
direct {
exclude = ["registry.terraform.io/*/*"]
}
}Подробнее о настройках зеркал в инструкции CLI Configuration File документации HashiCorp.
-
Откройте CLI.
-
Инициализируйте конфигурацию Terraform в директории:
terraform init
-
Проверьте, что конфигурационные файлы составлены без ошибок:
terraform validate
-
Отформатируйте конфигурационные файлы:
terraform fmt
-
Проверьте, какие ресурсы будут созданы:
terraform plan
-
Примените изменения и создайте ресурсы:
terraform apply
-
Подтвердите создание — введите yes и нажмите Enter. Созданные ресурсы отобразятся в панели управления.
-
Если для создания ресурсов оказалось недостаточно квот, увеличьте квоты.
Create a private network and subnet
resource "openstack_networking_network_v2" "network_1" {
name = "private-network"
admin_state_up = "true"
}
resource "openstack_networking_subnet_v2" "subnet_1" {
name = "private-subnet"
network_id = openstack_networking_network_v2.network_1.id
cidr = "192.168.199.0/24"
}
Here. cidr
— CIDR of a private subnet, e.g. 192.168.199.0/24
.
See a detailed description of the resources:
Create a cluster
Fixed configuration
Arbitrary configuration
data "selectel_dbaas_datastore_type_v1" "datastore_type_1" {
project_id = selectel_vpc_project_v2.project_1.id
region = "ru-9"
filter {
engine = "postgresql"
version = "14"
}
}
data "selectel_dbaas_flavor_v1" "flavor_1" {
project_id = selectel_vpc_project_v2.project_1.id
region = "ru-9"
filter {
datastore_type_id = data.selectel_dbaas_datastore_type_v1.datastore_type_1.datastore_types[0].id
fl_size = "standard"
vcpus = 4
ram = 16384
disk = 128
}
}
resource "selectel_dbaas_postgresql_datastore_v1" "datastore_1" {
name = "datastore-1"
project_id = selectel_vpc_project_v2.project_1.id
region = "ru-9"
type_id = data.selectel_dbaas_datastore_type_v1.datastore_type_1.datastore_types[0].id
subnet_id = openstack_networking_subnet_v2.subnet_1.id
node_count = 3
flavor_id = data.selectel_dbaas_flavor_v1.flavor_1.flavors[0].id
pooler {
mode = "transaction"
size = 50
}
}
Here:
region
— pool, for exampleru-9
. The list of available pools can be found in the instructions Availability matrix;filter
— A filter of cloud database types:engine
— cloud database type;version
— cloud database version. The list of available versions can be found in the instructions Versions and configurations;
filter
— filter of fixed cluster configurations. The list of available configurations can be found in the manual Versions and configurations;fl_size
— range of fixed configurations. The available values are.standard
(for Standard, CPU and Memory lines) andhigh_freq
(for the HighFreq range);vcpus
— number of vCPUs;ram
— the amount of RAM in MB;disk
— disk size in GB;
nodes_count
— number of nodes. The maximum number of nodes is 6;pooler
— connection pooler configuration:mode
— pooling mode;size
— pool size.
View a detailed description of the data sources and resources:
data "selectel_dbaas_datastore_type_v1" "datastore_type_1" {
project_id = selectel_vpc_project_v2.project_1.id
region = "ru-9"
filter {
engine = "postgresql"
version = "14"
}
}
resource "selectel_dbaas_postgresql_datastore_v1" "datastore_1" {
name = "datastore-1"
project_id = selectel_vpc_project_v2.project_1.id
region = "ru-9"
type_id = data.selectel_dbaas_datastore_type_v1.datastore_type_1.datastore_types[0].id
subnet_id = openstack_networking_subnet_v2.subnet_1.id
node_count = 3
flavor {
vcpus = 1
ram = 4096
disk = 32
}
pooler {
mode = "transaction"
size = 50
}
}
Here:
region
— pool, for exampleru-9
. The list of available pools can be found in the instructions Availability matrix;filter
— A filter of cloud database types:engine
— cloud database type;version
— cloud database version. The list of available versions can be found in the instructions Versions and configurations;
nodes_count
— number of nodes. The maximum number of nodes is 6;flavor
— arbitrary cluster configuration. The available values of arbitrary configurations can be found in the instructions Versions and configurations:vcpus
— number of vCPUs;ram
— the amount of RAM in MB;disk
— disk size in GB;
pooler
— connection pooler configuration:mode
— pooling mode;size
— pool size.
Check out the detailed description of the data source selectel_dbaas_datastore_type_v1 and resource selectel_dbaas_postgresql_datastore_v1.
Create a user
resource "selectel_dbaas_user_v1" "user_1" {
project_id = selectel_vpc_project_v2.project_1.id
region = "ru-9"
datastore_id = selectel_dbaas_postgresql_datastore_v1.datastore_1.id
name = "user"
password = "secret"
}
Here:
region
— pool in which the cluster resides;name
— username;password
— user password.
Check out the detailed description of the resource selectel_dbaas_user_v1.
Create a database and assign an owner
resource "selectel_dbaas_postgresql_database_v1" "database_1" {
project_id = selectel_vpc_project_v2.project_1.id
region = "ru-9"
datastore_id = selectel_dbaas_postgresql_datastore_v1.datastore_1.id
owner_id = selectel_dbaas_user_v1.user_1.id
name = "database_1"
}
Here. region
— pool in which the cluster resides.
Check out the detailed description of the resource selectel_dbaas_postgresql_database_v1.