---
title: "Create a Standard line fixed configuration cloud server and add it to a placement group using Terraform"
sidebar_label: "Create a server and add it to a placement group"
sidebar_position: 6
description: "Learn how to create a Standard line fixed configuration cloud server and add it to a placement group"
---

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 CreatePublicFloatingIp from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/cloud-networks/create-public-floating-ip.mdx'
import AssociateIp from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/cloud-networks/associate-ip.mdx'

# Create a Standard line fixed configuration cloud server and add it to a placement group using Terraform

<CreateResourcesAlert />

<br />

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

2. [Create an SSH key pair](#create-ssh-keypair).

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

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

5. [Create a port for the cloud server](#create-port).

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

7. [Create a placement group with an anti-affinity policy](#create-placement-group).

8. [Create a cloud server](#create-server).

9. [Create a public floating IP address](#create-public-floating-ip).

10. [Associate the cloud server public and private IP addresses](#associate-ips).

## Configuration files \{#config}

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

  <ConfigureProvidersConfig />
</details>

<details>
  <summary>Example file for creating a server with a placement group</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_1" {
      name       = "port"
      network_id = openstack_networking_network_v2.network_1.id

      fixed_ip {
        subnet_id = openstack_networking_subnet_v2.subnet_1.id
      }
  }

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

  resource "openstack_compute_servergroup_v2" "server_group_1" {
    name     = "group1"
    policies = ["anti-affinity"]
  }

  resource "openstack_compute_instance_v2" "server_1" {
    name              = "server"
    flavor_id         = "1312"
    key_pair          = selectel_vpc_keypair_v2.keypair_1.name
    availability_zone = "ru-9a"

    dynamic "scheduler_hints" {
      for_each = openstack_compute_servergroup_v2.server_group_1.id != "" ? [openstack_compute_servergroup_v2.server_group_1.id] : []
      content {
        group = openstack_compute_servergroup_v2.server_group_1.id
      }
    }

    network {
      port = openstack_networking_port_v2.port_1.id
    }

    lifecycle {
      ignore_changes = [image_id]
    }

    vendor_options {
      ignore_resize_confirmation = true
    }
  }

  resource "openstack_networking_floatingip_v2" "floatingip_1" {
      pool = "external-network"
  }

  resource "openstack_networking_floatingip_associate_v2" "association_1" {
      port_id     = openstack_networking_port_v2.port_1.id
      floating_ip = openstack_networking_floatingip_v2.floatingip_1.address
  }
  ```
</details>

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

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

<ConfigureProviders />

## 2. Create an SSH key pair \{#create-ssh-keypair}

<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 a port for the cloud server \{#create-port}

<CreatePort />

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

<GetImage />

## 7. Create a placement group with an anti-affinity policy \{#create-placement-group}

```hcl
resource "openstack_compute_servergroup_v2" "server_group_1" {
  name     = "group1"
  policies = ["anti-affinity"]
}
```

See the detailed resource description [openstack\_compute\_servergroup\_v2](/terraform/openstack-provider-reference/compute-nova/resources/openstack_compute_servergroup_v2/)

## 8. Create a cloud server \{#create-server}

```hcl

resource "openstack_compute_instance_v2" "server_1" {
  name              = "server"
  flavor_id         = "1312"
  key_pair          = selectel_vpc_keypair_v2.keypair_1.name
  availability_zone = "ru-9a"

  dynamic "scheduler_hints" {
    for_each = openstack_compute_servergroup_v2.server_group_1.id != "" ? [openstack_compute_servergroup_v2.server_group_1.id] : []
    content {
      group = openstack_compute_servergroup_v2.server_group_1.id
    }
  }

  network {
    port = openstack_networking_port_v2.port_1.id
  }

  lifecycle {
    ignore_changes = [image_id]
  }

  vendor_options {
    ignore_resize_confirmation = true
  }
}
```

Where:

* `availability_zone` — [pool segment](/infrastructure/locations/#pool) in which the cloud server will be created, for example `ru-9a`. The list of available pool segments can be found in the instructions [Product Availability by Location](/infrastructure/product-availability-by-location/).
* `flavor_id` — flavor ID. Flavors correspond to [cloud server configurations](/cloud-servers/create/configurations/) and determine the number of vCPUs, RAM, and local disk size. For example, `1312` is the flavor for a Standard line fixed configuration with 1 vCPU, 2 GB of RAM, and a 16 GB local disk. You can view the list of available flavors in the [List of fixed configuration flavors across all pools](/cloud-servers/create/configurations/#server-flavors-full-list) subsection of the Cloud Server Configurations instructions.

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

## 9. Create a public floating IP address \{#create-public-floating-ip}

<CreatePublicFloatingIp />

## 10. Associate the cloud server public and private IP addresses \{#associate-ips}

<AssociateIp />

<Formbricks />
