Skip to content

Instantly share code, notes, and snippets.

@bjarneo
Created November 1, 2024 16:46
Show Gist options
  • Save bjarneo/51b26b0055356819656472aa37044026 to your computer and use it in GitHub Desktop.
Save bjarneo/51b26b0055356819656472aa37044026 to your computer and use it in GitHub Desktop.
Hetzner + OpenTofu
# The Hetzner Cloud token
variable "hcloud_token" {
type = string
default = ""
}
# Your public SSH key
variable "ssh_key" {
type = string
default = "~/.ssh/id_1337.pub"
}
# Configure the Hetzner Cloud Provider
provider "hcloud" {
token = var.hcloud_token
}
# Fetch the SSH key
resource "hcloud_ssh_key" "sudo" {
name = "My ssh key"
public_key = file(var.ssh_key)
}
# Set up firewall rule for https
resource "hcloud_firewall" "https" {
name = "allow_https"
rule {
direction = "in"
protocol = "tcp"
port = 443
# Allowed IPs
source_ips = [
"0.0.0.0/0",
"::/0"
]
}
}
# Set up the SSH firewall ruke
resource "hcloud_firewall" "ssh" {
name = "allow_ssh"
rule {
direction = "in"
protocol = "tcp"
port = 22
# Allow only your public IP
source_ips = ["<your_public_ip>"]
}
}
# Create a server
resource "hcloud_server" "node" {
name = "my-awesome-server"
image = "ubuntu-24.04"
server_type = "cx42"
location = "hel1"
firewall_ids = [hcloud_firewall.https.id, hcloud_firewall.ssh.id]
ssh_keys = [hcloud_ssh_key.sudo.id]
labels = {
"purpose" = "coolify"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment