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.
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.
Before you begin, ensure that you have Terraform installed on your machine. You can download it from the Terraform website.
Create a new directory for your Terraform project and create a file named main.tf. This file will contain your 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
}
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.
Initialize Terraform: Navigate to your project directory in your terminal and run:
terraform init
Review the Plan: To see what Terraform will create, run:
terraform plan
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.
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!