in terraform, hcloud provider. I want to define a ssh key and have 2 VPSs use it. Show code

Terraform hcloud provider define SSH key for multiple VPS examples 2025

This refinement includes specific terms like 'define SSH key' and 'multiple VPS', as well as the current year to ensure the search returns relevant and up-to-date code examples related to the Terraform hcloud provider.

To define an SSH key and use it across multiple Virtual Private Servers (VPSs) using the Hetzner Cloud (hcloud) provider in Terraform, follow the guidelines and example code provided below. This approach will help you manage your resources effectively while ensuring secure access through SSH.

Defining SSH Keys and Multiple VPSs in Terraform

When working with the Hetzner Cloud provider, defining an SSH key that can be shared among different VPS instances is straightforward. Below are the steps and the corresponding Terraform code to achieve this.

Step 1: Install and Configure Terraform

Before you begin, ensure that you have Terraform installed on your machine. You can download it from the Terraform website.

Step 2: Create a Terraform Configuration File

Create a new directory for your Terraform project and create a file named main.tf. This file will contain your Terraform configuration.

Example Terraform Configuration

Here’s an example configuration that defines an SSH key and creates two VPS instances using that key.

provider "hcloud" {
  token = var.hcloud_token  # Replace with your Hetzner Cloud API token
}

resource "hcloud_ssh_key" "my_ssh_key" {
  name       = "my-ssh-key"
  public_key = file("~/.ssh/id_rsa.pub")  # Path to your SSH public key
}

resource "hcloud_server" "vps1" {
  name        = "my-vps-1"
  server_type = "cx11"  # Choose your desired server type
  image       = "ubuntu-22.04"  # Change to the desired OS image
  ssh_keys    = [hcloud_ssh_key.my_ssh_key.id]
}

resource "hcloud_server" "vps2" {
  name        = "my-vps-2"
  server_type = "cx11"  # Choose your desired server type
  image       = "ubuntu-22.04"  # Change to the desired OS image
  ssh_keys    = [hcloud_ssh_key.my_ssh_key.id]
}

# Variables
variable "hcloud_token" {
  description = "Hetzner Cloud API token"
  type        = string
}

Explanation of the Configuration

  • Provider Block: This section specifies the hcloud provider and requires an API token. You should input your Hetzner Cloud API token directly or as an environment variable.

  • SSH Key Resource: The hcloud_ssh_key resource is created to define an SSH key. The public_key attribute points to a file containing your SSH public key.

  • Server Resources: Two hcloud_server resources are defined, each representing a separate VPS instance. Both instances are assigned the SSH key created in the previous step, allowing SSH access using the same key.

Step 3: Initializing and Applying the Configuration

  1. Initialize Terraform: Navigate to your project directory in your terminal and run:

    terraform init
    
  2. Review the Plan: To see what Terraform will create, run:

    terraform plan
    
  3. Apply the Configuration: Finally, to create the resources defined in your configuration, execute:

    terraform apply
    

    You will be prompted to confirm the action. Type yes to proceed.

Conclusion

With the above configuration, you have successfully defined an SSH key and provisioned two VPS instances that use the same key for SSH access in the Hetzner Cloud using Terraform. This setup not only simplifies access management but also enhances security by using SSH keys instead of passwords.

For further customization or advanced setups, you may refer to the official documentation on the Terraform Registry for hcloud. If you face any issues or need additional functionality, feel free to ask for help!

Related Searches

Sources

10
1
Easily Deploy Virtual Machines in Hetzner Cloud Using Terraform
Blog

Missing: multiple VPS

2
hcloud_ssh_key | Resources | hetznercloud/hcloud | Terraform
Registry

Missing: multiple VPS

3
Setup your own scalable Kubernetes cluster with the Terraform ...
Community

This article covers how to create, scale, access, and destroy your own cluster, and how to calculate the costs of running the cluster.

4
hegerdes/hetzner-node-pool/hcloud | Terraform Registry
Registry

Missing: define VPS

5
Terraforming Proxmox & VPS - my Home Lab journey
Homelab

Configure Cloud-Init settings such as User, SSH public key, IP Config etc. – Use an admin user (not root) with an SSH key and set IP to DHCP.

6
How do you handle automatically generated private SSH keys for ...
Reddit

Missing: hcloud 2025

7
Deploy VM Instances on Hetzner Cloud with Terraform
Computingforgeeks

In this article, I'll show you how to create three instances on Hetzner Cloud using Terraform. We will add an ssh key to the instances used for remote access.

8
[Bug]: hcloud_server.xxx must be replaced when add new SSH key ...
GitHub

I'm trying to add a new SSH key to the server, but terraform apply is re-creating that server. How to avoid this behavior?

9
Terraform Provider Configurations: Examples and Tips - Scalr
Scalr

This blog explains what provider configurations are, how to use them, and best practices, with examples of the top providers in use today.

10
hcloud-k8s - kubernetes - Terraform Registry
Registry

You can define multiple storage classes for your cluster: name: The name of the StorageClass (string, required). encrypted: Enable LUKS encryption for ...