Skip to content

Terraform module to provision Server resource in Hetzner Cloud along with other optional resources e.g. Snapshot, Primary IP etc.

License

Notifications You must be signed in to change notification settings

zoro16/terraform-hcloud-server

Repository files navigation

Description

Terraform module to provision Server resource in Hetzner Cloud along with other optional resources e.g. Snapshot, Primary IP etc.

Usage

provider "hcloud" {
  token = var.hcloud_token
}

# Create and SSH Key with: `ssh-keygen -q -t ed25519 -N '' -f ./tf-example <<<y > /dev/null 2>&1`
resource "hcloud_ssh_key" "key" {
  name       = "tf_key"
  public_key = file("./tf-example.pub")
}

module "sv" {
  source = "../.."

  create_server = true
  server_name   = "example-server"
  server_type   = var.server_type_map.arm64_2_4GB
  location      = "fsn1"
  backups       = true
  image         = "debian-12"

  ssh_keys = [hcloud_ssh_key.key.id]
}

Requirements

Name Version
terraform >= 1.2.0
hcloud >= 1.48

Providers

Name Version
hcloud >= 1.48

Modules

No modules.

Resources

Name Type
hcloud_placement_group.placement resource
hcloud_primary_ip.primary resource
hcloud_server.server resource
hcloud_snapshot.snapshot resource

Inputs

Name Description Type Default Required
allow_deprecated_images Enable the use of deprecated images (default: false). Note Deprecated images will be removed after three months. Using them is then no longer possible. bool false no
backups Whether to enable or disable backups. bool true no
create_placement_group A boolean to check whether to create a Placement Group resource or not. bool false no
create_primary_ip A boolean to check whether to create a Primary IP resource or not. bool false no
create_server A boolean to check whether to create a Server resource or not. bool false no
create_snapshot A boolean to check whether to create a Snapshot resource or not. bool false no
datacenter The datacenter name to create the server in. nbg1-dc3, fsn1-dc14, hel1-dc2, ash-dc1 or hil-dc1. The following a mapping for the datacenters and their region string null no
delete_protection Enable or disable delete protection (Needs to be the same as rebuild_protection). bool false no
firewall_ids Firewall IDs the server should be attached to on creation. list(any) [] no
ignore_remote_firewall_ids Ignores any updates to the firewall_ids argument which were received from the server. This should not be used in normal cases. See the documentation of the hcloud_firewall_attachment resource for a reason to use this argument. bool false no
image The name of Hcloud OS image e.g. Debain, Ubuntu etc. string "debian-12" no
iso ID or Name of an ISO image to mount. string "" no
keep_disk If true, do not upgrade the disk. This allows downgrading the server type later. bool false no
labels User-defined labels (key-value pairs) should be created with. map(string) {} no
location The location name to create the server in. nbg1, fsn1, hel1, ash or hil string "fsn1" no
placement_group_id Placement Group ID the server added to on creation. string null no
placement_group_labels User-defined labels (key-value pairs) should be created with. map(string) {} no
placement_group_name Name of the Placement Group to be created. string "" no
placement_group_type Type of the Placement Group to be created. string "spread" no
primary_ip_assignee_id ID of the assigned resource. number null no
primary_ip_assignee_type The type of the assigned resource. Currently supported: server string "server" no
primary_ip_auto_delete Whether auto delete is enabled. Important note:It is recommended to set auto_delete to false, because if a server assigned to the managed ip is getting deleted, it will also delete the primary IP which will break the TF state. bool false no
primary_ip_datacenter The datacenter name to create the resource in. string null no
primary_ip_delete_protection Whether delete protection is enabled or not. bool false no
primary_ip_labels Description of the Primary IP. map(string) {} no
primary_ip_name Name of the Primary IP. string null no
primary_ip_type Type of the Primary IP. ipv4 or ipv6 string "ipv4" no
primary_ipv4 This is static IP that could be assign to any server in the time of server creation or after that. Note that the server and this IP should be in the same region e.g. eu-central string null no
primary_ipv6 This is static IP that could be assign to any server in the time of server creation or after that. Note that the server and this IP should be in the same region e.g. eu-central string null no
private_network_alias_ips Alias IPs the server should have in the Network. list(string) [] no
private_network_id ID of the network the server well be attached. number null no
private_network_ip Specify the IP the server should get in the network. string null no
public_net_ipv4_enabled Whether to enable or disable a public IPv4 (could be access from the Internet). bool true no
public_net_ipv6_enabled Whether to enable or disable a public IPv6 (could be access from the Internet). bool true no
rebuild_protection Enable or disable rebuild protection (Needs to be the same as delete_protection). bool false no
rescue Enable and boot in to the specified rescue system. This enables simple installation of custom operating systems. linux64 or linux32 string "" no
server_id ID of the server that already created. number null no
server_name Name of the server to create (must be unique per project and a valid hostname as per RFC 1123). string "some-server-name" no
server_type (Required, string) Name of the server type this server should be created with. string "cx22" no
shutdown_before_deletion Whether to try shutting the server down gracefully before deleting it. bool false no
snapshot_description Description of the snapshot. string "" no
snapshot_labels User-defined labels (key-value pairs) should be created with. map(string) {} no
ssh_keys SSH key IDs or names which should be injected into the server at creation time. list(string) [] no
user_data This could be a Bash script or cloud-init configurations that would run the first time the VM is provisioned. string "" no

Outputs

Name Description
placement_group_id Placement Group ID the server added to on creation.
placement_group_labels Placement Group ID the server added to on creation.
placement_group_name Name of the Placement Group to be created.
placement_group_type Type of the Placement Group to be created.
primary_ip_assignee_id ID of the assigned resource.
primary_ip_assignee_type The type of the assigned resource. Currently supported: server
primary_ip_auto_delete Whether auto delete is enabled. Important note:It is recommended to set auto_delete to false, because if a server assigned to the managed ip is getting deleted, it will also delete the primary IP which will break the TF state.
primary_ip_datacenter The datacenter name to create the resource in.
primary_ip_delete_protection Whether delete protection is enabled or not.
primary_ip_id Unique ID of the Primary IP.
primary_ip_ip_address IP Address of the Primary IP.
primary_ip_ip_network IPv6 subnet of the Primary IP for IPv6 addresses. (Only set if type is ipv6)
primary_ip_labels Description of the Primary IP.
primary_ip_name Name of the Primary IP.
primary_ip_type Type of the Primary IP. ipv4 or ipv6
server_backups Whether the backups are enabled or not.
server_datacenter The datacenter that the server is located in.
server_delete_protection Whether delete protection is enabled.
server_firewall_ids Firewall IDs the server is attached to.
server_id Unique ID of the server.
server_image Name or ID of the image the server was created from.
server_ipv4_address The IPV4 Address for the VM server
server_ipv6_address The IPV6 Address for the VM server
server_labels The User-defined labels (key-value pairs)
server_location The location of the server
server_name Name of the server.
server_private_network Private Network the server shall be attached to. The Network that should be attached to the server requires at least one subnetwork. Subnetworks cannot be referenced by Servers in the Hetzner Cloud API. Therefore Terraform attempts to create the subnetwork in parallel to the server. This leads to a concurrency issue. It is therefore necessary to use depends_on to link the server to the respective subnetwork. See examples.
server_private_network_alias_ips Private Network Alias IPs
server_private_network_id Private Network IDs
server_private_network_ip Private Network IPs
server_private_network_mac_address Private Network MAC Address'
server_rebuild_protection Whether rebuild protection is enabled.
server_shutdown_before_deletion Whether the server will try to shut down gracefully before being deleted.
server_status The status of the server.
server_type The type of the server.
snapshot_description The description of the snapshot.
snapshot_id Unique ID of the snapshot.
snapshot_labels The user-defined labels
snapshot_server_id The server that the snapshot was created from.