Skip to content

Terraform Configuration Documentation

This Terraform configuration creates several servers on Hetzner Cloud (hcloud). Each server is attached to a private network and has a specific role label. The depends_on attribute is used to ensure that the network subnet is created before the server. The user_data attribute is used to specify a file that contains cloud-init configuration.

Web Server

The web server is created with the following configuration:

resource "hcloud_server" "web" {
  count = 1
  name = "web-server-${count.index}"
  server_type = "cax11"
  image = "ubuntu-22.04"
  ssh_keys = [hcloud_ssh_key.ansible_key.id]
  location = "hel1"
  labels = {
    role = "web"
    ssh = ""
  }
  public_net {
    ipv4_enabled = true
    ipv6_enabled = false
  }
  network {
    network_id = hcloud_network.private-lan.id
    ip = "10.10.1.1${min(count.index + 1, 10)}"
  }
  depends_on = [
    hcloud_network_subnet.private-subnet
  ]
  user_data = file("user_data_vm.yml")
}

Load Balancer Server

The load balancer server is created with the following configuration:

resource "hcloud_server" "load_balancer" {
  name = "lb-server"
  server_type = "cax11"
  image = "ubuntu-22.04"
  ssh_keys = [hcloud_ssh_key.ansible_key.id]
  location = "hel1"
  labels = {
    role = "lb"
  }
  public_net {
    ipv4_enabled = true
    ipv6_enabled = false
  }
  network {
    network_id = hcloud_network.private-lan.id
    ip = "10.10.1.20"
  }
  depends_on = [
    hcloud_network_subnet.private-subnet
  ]
  user_data = file("user_data_lb.yml")
}

API Server with MySQL

The API server with MySQL is created with the following configuration:

resource "hcloud_server" "api_server" {
  name = "api-server"
  server_type = "cax11"
  image = "ubuntu-22.04"
  ssh_keys = [hcloud_ssh_key.ansible_key.id]
  location = "hel1"
  labels = {
    role = "api"
    ssh = ""
  }
  public_net {
    ipv4_enabled = true
    ipv6_enabled = false
  }
  network {
    network_id = hcloud_network.private-lan.id
    ip = "10.10.1.30"
  }
  depends_on = [
    hcloud_network_subnet.private-subnet
  ]
  user_data = file("user_data_api.yml")
}