Create a security group and assign it to a cloud server port using 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 providers.
- Create a private network and a subnet.
- Create a security group.
- Create a rule.
- Create a port in the network.
- Assign the group to a port.
- Create a server.
Configuration files
Example file for configuring providers
terraform {
required_providers {
selectel = {
source = "selectel/selectel"
version = "~> 6.0"
}
openstack = {
source = "terraform-provider-openstack/openstack"
version = "2.1.0"
}
}
}
provider "selectel" {
domain_name = "123456"
username = "user"
password = "password"
auth_region = "ru-9"
auth_url = "https://cloud.api.selcloud.ru/identity/v3/"
}
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 security group and assigning it to a server port
resource "openstack_networking_network_v2" "network_1" {
name = "network"
port_security_enabled = "true"
}
resource "openstack_networking_subnet_v2" "subnet_1" {
name = "subnet"
network_id = openstack_networking_network_v2.network_1.id
cidr = "192.168.0.0/24"
}
resource "openstack_networking_secgroup_v2" "security_group_1" {
name = "sg"
description = "Test security group"
}
resource "openstack_networking_secgroup_rule_v2" "security_group_rule_1" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 443
port_range_max = 443
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.security_group_1.id
}
resource "openstack_networking_port_v2" "port_1" {
name = "port"
network_id = openstack_networking_network_v2.network_1.id
admin_state_up = "true"
fixed_ip {
subnet_id = openstack_networking_subnet_v2.subnet_1.id
}
}
resource "openstack_networking_port_secgroup_associate_v2" "security_group_associate_1" {
port_id = openstack_networking_port_v2.port_1.id
security_group_ids = [openstack_networking_secgroup_v2.security_group_1.id]
}
data "openstack_images_image_v2" "image_1" {
name = "Ubuntu 20.04 LTS 64-bit"
most_recent = true
visibility = "public"
}
resource "selectel_vpc_keypair_v2" "keypair_1" {
name = "keypair"
public_key = file("~/.ssh/id_rsa.pub")
user_id = selectel_iam_serviceuser_v1.serviceuser_1.id
}
resource "openstack_blockstorage_volume_v3" "volume_1" {
name = "boot-volume"
size = "5"
image_id = data.openstack_images_image_v2.image_1.id
volume_type = "fast.ru-9a"
availability_zone = "ru-9a"
enable_online_resize = true
lifecycle {
ignore_changes = [image_id]
}
}
resource "openstack_compute_instance_v2" "server_1" {
name = "server"
flavor_id = "4011"
key_pair = selectel_vpc_keypair_v2.keypair_1.name
availability_zone = "ru-9a"
network {
port = openstack_networking_port_v2.port_1.id
}
lifecycle {
ignore_changes = [image_id]
}
block_device {
uuid = openstack_blockstorage_volume_v3.volume_1.id
source_type = "volume"
destination_type = "volume"
boot_index = 0
}
vendor_options {
ignore_resize_confirmation = true
}
}
1. Optional: configure providers
If you have configured the Selectel and OpenStack providers, skip this step.
-
Make sure you have created a service user in the control panel
memberwith roles 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 and OpenStack providers to the file for provider configuration:
terraform {required_providers {selectel = {source = "selectel/selectel"version = "~> 7.1.0"}openstack = {source = "terraform-provider-openstack/openstack"version = "2.1.0"}}}Here
versionis the provider version. The current version can be found in the Selectel documentation (in Terraform Registry and GitHub) and OpenStack (in Terraform Registry and GitHub).For more information about products, services, and features that can be managed using providers, see 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 find it in the control panel in the top-right corner;username— the name of the service user with thememberrole in the Account access scope andiam.admin. You can view it in the control panel: in the top menu, click IAM → Service Users section (this 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 in theru-9format; do not use pools in theSPB-2format. The authorization pool might not match the pool in which you create resources. The list of available pools can be viewed in the guide Product availability by location.
-
Create a project:
resource "selectel_vpc_project_v2" "project_1" {name = "project"}See the detailed resource description for selectel_vpc_project_v2.
-
Create a service user for project access 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— user name; -
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 ASCII Printable 7-Bit Special Characters list:
!"#$%&'()*+,-./:;<=>?@[]^_{|}~;
- one uppercase and one lowercase Latin letter (
-
project_id— project ID. You can find it in the Control panel: in the top menu, click IAM → the Projects section → in the line of the required project, click .
View the detailed description of the resource selectel_iam_serviceuser_v1.
-
-
Initialize the OpenStack provider:
provider "openstack" {auth_url = "https://cloud.api.selcloud.ru/identity/v3"domain_name = "123456"tenant_id = selectel_vpc_project_v2.project_1.iduser_name = selectel_iam_serviceuser_v1.serviceuser_1.namepassword = selectel_iam_serviceuser_v1.serviceuser_1.passwordregion = "ru-9"}Where:
domain_name— Selectel account number. You can find it in the Control panel in the upper right corner;region— pool, for exampleru-9. All resources will be created in this pool. The list of available pools can be viewed in the guide Product availability by location.
-
If you create resources at the same time as configuring providers, add the
depends_onargument for OpenStack resources. For example, for the openstack_networking_network_v2 resource: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]} -
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/*/*"]}}Learn more about mirror settings in the CLI Configuration File HashiCorp documentation.
-
Open the CLI.
-
Initialize the Terraform configuration in the directory:
terraform init -
Verify that the configuration files are syntactically correct:
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 there are not enough quotas to create resources, increase quotas.
2. Create a private network and a subnet
resource "openstack_networking_network_v2" "network_1" {
name = "network"
port_security_enabled = "true"
}
resource "openstack_networking_subnet_v2" "subnet_1" {
name = "subnet"
network_id = openstack_networking_network_v2.network_1.id
cidr = "192.168.0.0/24"
}
3. Create a security group
resource "openstack_networking_secgroup_v2" "security_group_1" {
name = "sg"
description = "Test security group"
}
See the detailed resource description for openstack_networking_secgroup_v2.
4. Create a rule
resource "openstack_networking_secgroup_rule_v2" "security_group_rule_1" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 443
port_range_max = 443
remote_ip_prefix = "0.0.0.0/0"
security_group_id = openstack_networking_secgroup_v2.security_group_1.id
}
Where:
direction— traffic direction:ingressfor incoming traffic,egressfor outgoing;ethertype— protocol type:IPv4orIPv6;protocol— protocol, for exampletcp;port_range_min— the first port in the range of ports allowed for connection, for example443;port_range_max— the last port in the range;remote_ip_prefix— IP addresses from which traffic is allowed, for example0.0.0.0/0.
See the detailed resource description for openstack_networking_secgroup_rule_v2.
5. Create a port in the network
resource "openstack_networking_port_v2" "port_1" {
name = "port"
network_id = openstack_networking_network_v2.network_1.id
fixed_ip {
subnet_id = openstack_networking_subnet_v2.subnet_1.id
}
}
See the detailed description of the openstack_networking_port_v2 resource.
6. Assign the group to a port
resource "openstack_networking_port_secgroup_associate_v2" "security_group_associate_1" {
port_id = openstack_networking_port_v2.port_1.id
security_group_ids = [openstack_networking_secgroup_v2.security_group_1.id]
}
See the detailed resource description for openstack_networking_secgroup_associate_v2.
7. Create a cloud server
1. Add a public SSH key
resource "selectel_vpc_keypair_v2" "keypair_1" {
name = "keypair"
public_key = file("~/.ssh/id_rsa.pub")
user_id = selectel_iam_serviceuser_v1.serviceuser_1.id
}
Here public_key is the path to the public SSH key. If SSH keys are not generated, create them.
See the detailed description of the selectel_vpc_keypair_v2 resource.
2. Get an image
data "openstack_images_image_v2" "image_1" {
name = "Ubuntu 20.04 LTS 64-bit"
most_recent = true
visibility = "public"
}
See the detailed description of the openstack_images_image_v2 data source.
3. Create a bootable network volume
resource "openstack_blockstorage_volume_v3" "volume_1" {
name = "boot-volume-for-server"
size = "5"
image_id = data.openstack_images_image_v2.image_1.id
volume_type = "fast.ru-9a"
availability_zone = "ru-9a"
enable_online_resize = true
lifecycle {
ignore_changes = [image_id]
}
}
Here:
size— network volume size in GB. Observe the network volume limits for the maximum size;volume_type— ID or name of the network volume type. For example,fast.ru-9a— name to create a network volume with the Fast SSD type in the pool segment ru-9a. You can check the list of types in the table List of network volume types in all pool segments;availability_zone— pool segment where the network volume will be created, for example,ru-9a. You can check the list of available pool segments in the instructions Product availability by location.
See the detailed resource description in openstack_blockstorage_volume_v3.
4. Create a cloud server
resource "openstack_compute_instance_v2" "server_1" {
name = "server"
flavor_id = "1015"
key_pair = selectel_vpc_keypair_v2.keypair_1.name
availability_zone = "ru-9a"
network {
port = openstack_networking_port_v2.port_1.id
}
lifecycle {
ignore_changes = [image_id]
}
block_device {
uuid = openstack_blockstorage_volume_v3.volume_1.id
source_type = "volume"
destination_type = "volume"
boot_index = 0
}
vendor_options {
ignore_resize_confirmation = true
}
}
Where:
availability_zone— pool segment in which the cloud server will be created, for exampleru-9a. The list of available pool segments can be found in the instructions on Product availability by location;flavor_id— flavor ID. Flavors correspond to cloud server configurations and define the number of vCPUs, RAM, and the size of the local disk (optional) for the server. You can use fixed-configuration flavors. For example,1015is the ID for creating a server with a Standard line fixed configuration with 4 vCPUs and 16 GB of RAM in the ru-9 pool. A list of flavors is available in the table List of fixed-configuration flavors in all pools.
See the detailed description of the openstack_compute_instance_v2 resource.