Skip to content

Terraform module for managing servers in the Hetzner Cloud

License

Notifications You must be signed in to change notification settings

peterpramb/terraform-hcloud-servers

Repository files navigation

License Latest Release Terraform Version

terraform-hcloud-servers

Terraform module for managing servers in the Hetzner Cloud.

It implements the following provider resources:

Usage

module "server" {
  source  = "github.com/peterpramb/terraform-hcloud-servers?ref=<release>"

  servers = [
    {
      name        = "server-1.example.net"
      image       = "rocky-9"
      server_type = "cx11"
      backups     = false
      datacenter  = null
      dns_ptr     = "server-1.example.net"
      firewalls   = []
      iso         = null
      keep_disk   = true
      location    = "nbg1"
      networks    = [
        {
          name      = "network-1"
          subnet_id = "171740-10.0.0.0/24"
          alias_ips = []
          ip        = "10.0.0.10"
        }
      ]
      placement   = "57826"
      protection  = true
      public_net  = [
        "11011913",
        "disabled"
      ]
      rescue      = null
      ssh_keys    = [
        "ssh-key-1"
      ]
      user_data   = file("${path.module}/cloud-init.yml")
      labels      = {
        "managed"    = "true"
        "managed_by" = "Terraform"
      }
    }
  ]
}

See examples for more usage details.

Requirements

Name Version
terraform ≥ 0.13

Providers

Name Version
hcloud ≥ 1.35

Inputs

Name Description Type Default Required
servers List of server objects to be managed. list(map(server)) See below yes

server

Name Description Type Required
name Unique name of the server. string yes
image Name or ID of the server image. string yes
server_type Type of the server to be created. string yes
backups Enable backups for the server. bool no
datacenter Name of the datacenter to create the server in. string no
dns_ptr DNS name the host IPs should resolve to. string no
firewalls List of firewall IDs to assign to the server. list(number) no
iso Name or ID of the ISO image to mount. string no
keep_disk Keep disk unchanged on server rescale. bool no
location Name of the location to create the server in. string no
networks List of network objects. list(map(network)) no
placement ID of the placement group to assign to the server. string no
protection Protect server from deletion. bool no
public_net The primary IP(s) to assign to the server. tuple(public_net) no
rescue Name of the rescue system to boot into. string no
ssh_keys List of SSH key names or IDs to be deployed. list(string) no
user_data Cloud-Init user data to be used for setup. string no
labels Map of user-defined labels. map(string) no

network

Name Description Type Required
name Name of the network to assign to the server. string yes
subnet_id ID of the subnet to assign to the server. string yes
alias_ips List of additional IPs to assign to the server. list(string) no
ip Main IP address to assign to the server. string no

public_net

Index Description Type Required
[0] The primary IPv4 address to assign to the server. string yes
[1] The primary IPv6 network to assign to the server. string yes

Defaults

servers = [
  {
    name        = "server-1"
    image       = "rocky-9"
    server_type = "cx11"
    backups     = false
    datacenter  = null
    dns_ptr     = null
    firewalls   = []
    iso         = null
    keep_disk   = false
    location    = null
    networks    = []
    placement   = null
    protection  = false
    public_net  = null
    rescue      = null
    ssh_keys    = []
    user_data   = null
    labels      = {}
  }
]

Outputs

Name Description
servers List of all server objects.
server_ids Map of all server objects indexed by ID.
server_names Map of all server objects indexed by name.
server_rdns List of all server RDNS objects.
server_rdns_ids Map of all server RDNS objects indexed by ID.
server_rdns_names Map of all server RDNS objects indexed by name.
server_networks List of all server network objects.
server_network_ids Map of all server network objects indexed by ID.
server_network_names Map of all server network objects indexed by name.

Defaults

servers = [
  {
    "backup_window" = ""
    "backups" = false
    "datacenter" = "nbg1-dc3"
    "delete_protection" = false
    "firewall_ids" = []
    "id" = "8002775"
    "image" = "rocky-9"
    "ipv4_address" = "192.0.2.1"
    "ipv6_address" = "2001:DB8::1"
    "ipv6_network" = "2001:DB8::/64"
    "keep_disk" = false
    "labels" = {}
    "location" = "nbg1"
    "name" = "server-1"
    "networks" = []
    "public_net" = [
      {
        "ipv4" = 0
        "ipv4_enabled" = true
        "ipv6" = 0
        "ipv6_enabled" = true
      },
    ]
    "rdns" = []
    "rebuild_protection" = false
    "server_type" = "cx11"
    "ssh_keys" = []
    "status" = "running"
  },
]

server_ids = {
  "8002775" = {
    "backup_window" = ""
    "backups" = false
    "datacenter" = "nbg1-dc3"
    "delete_protection" = false
    "firewall_ids" = []
    "id" = "8002775"
    "image" = "rocky-9"
    "ipv4_address" = "192.0.2.1"
    "ipv6_address" = "2001:DB8::1"
    "ipv6_network" = "2001:DB8::/64"
    "keep_disk" = false
    "labels" = {}
    "location" = "nbg1"
    "name" = "server-1"
    "networks" = []
    "public_net" = [
      {
        "ipv4" = 0
        "ipv4_enabled" = true
        "ipv6" = 0
        "ipv6_enabled" = true
      },
    ]
    "rdns" = []
    "rebuild_protection" = false
    "server_type" = "cx11"
    "ssh_keys" = []
    "status" = "running"
  }
}

server_names = {
  "server-1" = {
    "backup_window" = ""
    "backups" = false
    "datacenter" = "nbg1-dc3"
    "delete_protection" = false
    "firewall_ids" = []
    "id" = "8002775"
    "image" = "rocky-9"
    "ipv4_address" = "192.0.2.1"
    "ipv6_address" = "2001:DB8::1"
    "ipv6_network" = "2001:DB8::/64"
    "keep_disk" = false
    "labels" = {}
    "location" = "nbg1"
    "name" = "server-1"
    "networks" = []
    "public_net" = [
      {
        "ipv4" = 0
        "ipv4_enabled" = true
        "ipv6" = 0
        "ipv6_enabled" = true
      },
    ]
    "rdns" = []
    "rebuild_protection" = false
    "server_type" = "cx11"
    "ssh_keys" = []
    "status" = "running"
  }
}

server_rdns = []

server_rdns_ids = {}

server_rdns_names = {}

server_networks = []

server_network_ids = {}

server_network_names = {}

License

This module is released under the MIT License.