Hcloud With Terraform

Hetzner Cloud is an excellent cloud provider.

Here is a quick example of how to create a server on Hetzner Cloud using Terraform.

variable "hcloud_token" {}

# Configure the Hetzner Cloud Provider
provider "hcloud" {
  token = "${var.hcloud_token}"
}

#  Main ssh key
resource "hcloud_ssh_key" "default" {
  name       = "main ssh key"
  public_key = "${file("~/.ssh/id_rsa.pub")}"
}

resource "hcloud_server" "testnode1" {
  name        = "testnode1"
  image       = "ubuntu-16.04"
  server_type = "cx11"
  ssh_keys    = ["${hcloud_ssh_key.default.name}"]
}

output "public_ip4" {
  value = "${hcloud_server.testnode1.ipv4_address}"
}
  • Create server by running:
export TF_VAR_hcloud_token=YOUR_SECRET_TOKEN
terraform init
terraform plan
terraform apply
  • You can now log in into the server with command:
ssh root@$(terraform output public_ip4)
  • When you are done, delete the server by running:
terraform destroy