feat: added hetzner cloud terraform configuration

This commit is contained in:
Ceferino Patino 2025-10-25 23:33:01 -05:00
commit d99e77ac8d
Signed by: c4patino
SSH key fingerprint: SHA256:Wu+cU1t+7zVT9wzew/4meNmeulo66NzMqc22MdDBgXI
4 changed files with 109 additions and 0 deletions

31
infra/hetzner/README.md Normal file
View file

@ -0,0 +1,31 @@
# Hetzner Cloud Terraform Setup
This directory manages Hetzner Cloud resources using Terraform.
## Prerequisites
- Create a Hetzner Cloud API token: https://console.hetzner.cloud/projects
- (Optional) Register your SSH key in Hetzner Cloud for secure access.
## Usage
1. Set your API token as an environment variable:
```bash
export TF_VAR_hcloud_token="<your-token>"
```
2. (Optional) Set SSH key names, image, and location:
```bash
export TF_VAR_hcloud_ssh_keys='["your-key-name"]'
```
3. Run Terraform:
```bash
terraform init
terraform plan
terraform apply
```
## Outputs
- The server's public IPv4 address and ID will be shown after apply.
## Destroying the Server
```bash
terraform destroy
```

58
infra/hetzner/main.tf Normal file
View file

@ -0,0 +1,58 @@
terraform {
backend "pg" {
schema_name = "yumeami_hetzner"
conn_str = "postgres://shiori:5600/terraform?sslmode=disable"
}
required_providers {
hcloud = {
source = "hetznercloud/hcloud"
version = "~> 1.42.0"
}
}
}
provider "hcloud" {
token = var.hcloud_token
}
resource "hcloud_server" "tobira" {
name = "tobira"
server_type = "cx22"
image = "ubuntu-24.04"
location = "ash"
public_net {
ipv4_enabled = true
ipv6_enabled = true
}
firewall_ids = [hcloud_firewall.primary.id]
ssh_keys = var.hcloud_ssh_keys
}
resource "hcloud_firewall" "primary" {
name = "primary"
rule {
destination_ips = [
"0.0.0.0/0",
"::/0",
]
direction = "out"
port = "any"
protocol = "tcp"
source_ips = []
}
rule {
destination_ips = []
direction = "in"
port = "any"
protocol = "tcp"
source_ips = [
"0.0.0.0/0",
"::/0",
]
}
}

9
infra/hetzner/outputs.tf Normal file
View file

@ -0,0 +1,9 @@
output "server_id" {
description = "ID of the Hetzner server."
value = hcloud_server.tobira.id
}
output "server_ipv4" {
description = "Public IPv4 address of the server."
value = hcloud_server.tobira.ipv4_address
}

View file

@ -0,0 +1,11 @@
variable "hcloud_token" {
description = "Hetzner Cloud API token."
type = string
sensitive = true
}
variable "hcloud_ssh_keys" {
description = "List of SSH key names registered in Hetzner Cloud."
type = list(string)
default = []
}