---
title: "Create several identical cloud servers in one private network via Terraform"
sidebar_label: "Create several identical servers in one private network"
sidebar_position: 7
description: "How to create several identical cloud servers in one private network"
---

import Formbricks from '@theme/MDXComponents/Formbricks'
import ConfigureProviders from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/common/configure-providers.mdx'
import ConfigureProvidersConfig from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/common/configure-providers-config.mdx'
import CreateRouter from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/cloud-networks/create-router.mdx'
import CreateNetworks from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/cloud-networks/create-networks.mdx'
import AddPublicSsh from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/cloud-servers/add-public-ssh-key.mdx'
import CreatePort from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/cloud-servers/create-port.mdx'
import GetImage from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/cloud-servers/get-image.mdx'
import CreateResourcesAlert from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/common/create-resources-alert.mdx'
import CreatePublicIp from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/cloud-networks/create-public-ip.mdx'
import AssociateIp from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/cloud-networks/associate-ip.mdx'
import GetIp from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/cloud-networks/get-ip.mdx'

# Create several identical cloud servers in one private network via Terraform

<CreateResourcesAlert />

<br />

1. Optional: [configure providers](#configure-providers).

2. [Add a public SSH key](#add-public-ssh-key).

3. [Create a private network and subnet](#create-networks).

4. [Create a cloud router connected to the internet](#create-router).

5. [Create ports for cloud servers](#create-ports).

6. [Get an image](#get-image).

7. [Create cloud servers](#create-servers).

8. [Create public IP addresses](#create-public-ips).

9. [Associate public and private IP addresses of cloud servers](#associate-ips).

## Configuration files \{#config}

<details>
  <summary>Example file for configuring providers</summary>

  <ConfigureProvidersConfig />
</details>

<details>
  <summary>Example file for creating servers</summary>

  ```hcl
      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_networking_network_v2" "network_1" {
          name           = "private-network"
          admin_state_up = "true"
      }

      resource "openstack_networking_subnet_v2" "subnet_1" {
          network_id = openstack_networking_network_v2.network_1.id
          cidr       = "192.168.199.0/24"
      }

      data "openstack_networking_network_v2" "external_network_1" {
          external = true
      }

      resource "openstack_networking_router_v2" "router_1" {
          name                = "router"
          external_network_id = data.openstack_networking_network_v2.external_network_1.id
      }

      resource "openstack_networking_router_interface_v2" "router_interface_1" {
          router_id = openstack_networking_router_v2.router_1.id
          subnet_id = openstack_networking_subnet_v2.subnet_1.id
      }

      resource "openstack_networking_port_v2" "port" {
          name       = "port"
          count      = 3
          network_id = openstack_networking_network_v2.network2.id

          fixed_ip {
              subnet_id = openstack_networking_subnet_v2.subnet_2.id
          }

      }

      data "openstack_images_image_v2" "image_1" {
          name        = "Ubuntu 20.04 LTS 64-bit"
          most_recent = true
          visibility  = "public"
      }

      resource "openstack_compute_instance_v2" "server" {
          name              = "server"
          count             = 3
          image_id          = data.openstack_images_image_v2.ubuntu.id
          flavor_id         = "1312"
          key_pair          = selectel_vpc_keypair_v2.keypair_1.name
          availability_zone = "ru-9a"

          network {
              port = openstack_networking_port_v2.port[count.index].id
          }

          lifecycle {
              ignore_changes = [image_id]
          }

          vendor_options {
              ignore_resize_confirmation = true
          }

      }

      resource "openstack_networking_floatingip_v2" "floatingip" {
          pool  = "external-network"
          count = 3
      }

      resource "openstack_networking_floatingip_associate_v2" "association" {
          count       = 3
          port_id     = openstack_networking_port_v2.port[count.index].id
          floating_ip = openstack_networking_floatingip_v2.floatingip[count.index].address
      }
  ```
</details>

## 1. Optional: configure providers \{#configure-providers}

If you have already configured Selectel and OpenStack providers, skip this step.

<ConfigureProviders />

## 2. Add a public SSH key \{#add-public-ssh-key}

<AddPublicSsh />

## 3. Create a private network and subnet \{#create-networks}

<CreateNetworks />

## 4. Create a cloud router connected to the internet \{#create-router}

<CreateRouter />

## 5. Create ports for cloud servers \{#create-ports}

```hcl
resource "openstack_networking_port_v2" "port" {
  name       = "port"
  count      = 3
  network_id = openstack_networking_network_v2.network2.id

  fixed_ip {
    subnet_id = openstack_networking_subnet_v2.subnet_2.id
  }
}

```

Here `count` is the number of ports.

See the detailed description of the [openstack\_networking\_port\_v2](/terraform/openstack-provider-reference/networking-neutron/resources/openstack_networking_port_v2/) resource.

## 6. Get an image \{#get-image}

<GetImage />

## 7. Create cloud servers \{#create-servers}

```hcl
resource "openstack_compute_instance_v2" "server" {
  name              = "server"
  count             = 3
  image_id          = data.openstack_images_image_v2.ubuntu.id
  flavor_id         = "1312"
  key_pair          = selectel_vpc_keypair_v2.keypair_1.name
  availability_zone = "ru-9a"

  network {
    port = openstack_networking_port_v2.port[count.index].id
  }

  lifecycle {
    ignore_changes = [image_id]
  }

  vendor_options {
    ignore_resize_confirmation = true
  }
}
```

Where:

* `count` is the number of servers;
* `flavor_id` — flavor ID. For example, `1312` — ID of the fixed-configuration flavor from the Standard line with 1 vCPU, 2048 GB RAM, 16 GB local disk. The list of flavors can be viewed using the `openstack flavor list` command or in the [List of fixed-configuration flavors in all pools](/cloud-servers/create/configurations/#server-flavors-full-list);
* `availability_zone` — [pool segment](/infrastructure/locations/#pool) in which cloud servers will be created, for example `ru-9a`. The list of available pool segments can be viewed in the [Availability Matrix](/infrastructure/availability-matrix/).

See the detailed description of the [openstack\_compute\_instance\_v2](/terraform/openstack-provider-reference/compute-nova/resources/openstack_compute_instance_v2/) resource.

## 8. Create public IP addresses \{#create-public-ips}

```hcl
resource "openstack_networking_floatingip_v2" "floatingip" {
  count = 3
  pool  = "external-network"
}
```

Here `count` is the number of public IP addresses.

See the detailed description of the [openstack\_networking\_floatingip\_v2](/terraform/openstack-provider-reference/networking-neutron/resources/openstack_networking_floatingip_v2/) resource.

## 9. Associate public and private IP addresses of cloud servers \{#associate-ips}

Public IP addresses will be attached to cloud server ports and associated with private IP addresses.

```hcl
resource "openstack_networking_floatingip_associate_v2" "association" {
  count       = 3
  port_id     = openstack_networking_port_v2.port[count.index].id
  floating_ip = openstack_networking_floatingip_v2.floatingip[count.index].address
}
```

Here `count` is the number of public and private IP address associations.

See the detailed description of the [openstack\_networking\_floatingip\_associate\_v2](/terraform/openstack-provider-reference/networking-neutron/resources/openstack_networking_floatingip_associate_v2/) resource.

<Formbricks />
