---
title: "Create an SSH key pair and add a public SSH key via Terraform"
sidebar_label: "Create an SSH key pair and add a public SSH key"
sidebar_position: 8
description: "How to create an SSH key pair and add a public SSH key for a user"
---

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 CreateResourcesAlert from '@site/i18n/en/docusaurus-plugin-content-docs-terraform/current/_partials/common/create-resources-alert.mdx'
import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
import {TabItemLabel} from '@selectel/docux/components'

# Create an SSH key pair and add a public SSH key via Terraform

<CreateResourcesAlert />

<br />

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

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

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

## Configuration files \{#config}

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

  <ConfigureProvidersConfig />
</details>

<details>
  <summary>Example configuration file for adding a public SSH key for all regions</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
  }
  ```
</details>

<details>
  <summary>Example configuration file for adding a public SSH key for a specific region</summary>

  ```hcl
  resource "selectel_vpc_keypair_v2" "keypair_2" {
    name       = "keypair-ru-9"
    public_key = file("~/.ssh/id_rsa.pub")
    user_id    = selectel_vpc_user_v2.user_1.id
    regions    = [
        "ru-9"
        ]
  }
  ```
</details>

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

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

<ConfigureProviders />

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

[Create an SSH key pair](/cloud-servers/manage/create-and-place-ssh-key/#create-ssh-keys) on your local computer.

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

<Tabs queryString="add-ssh-public-key">
  <TabItem value="ssh-for-all-regions">
    <TabItemLabel>
      For all regions
    </TabItemLabel>

    ```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
    }
    ```

    Here `public_key` is the path to the public SSH key you [created](#create-ssh-keypair).

    See the detailed description of the [selectel\_vpc\_keypair\_v2](/terraform/selectel-provider-reference/resources/vpc_keypair_v2/) resource.
  </TabItem>

  <TabItem value="ssh-for-specific-region">
    <TabItemLabel>
      For a specific region
    </TabItemLabel>

    ```hcl
    resource "selectel_vpc_keypair_v2" "keypair_2" {
      name       = "keypair-ru-9"
      public_key = file("~/.ssh/id_rsa.pub")
      user_id    = selectel_vpc_user_v2.user_1.id
      regions    = [
          "ru-9"
          ]
    }
    ```

    Here `public_key` is the path to the public SSH key you [created](#create-ssh-keypair).

    See the detailed description of the [selectel\_vpc\_keypair\_v2](/terraform/selectel-provider-reference/resources/vpc_keypair_v2/) resource.
  </TabItem>
</Tabs>

<Formbricks />
