| name | infra |
| description | Manage Hetzner Cloud infrastructure with hcloud CLI and Terraform. Use when: provisioning servers, managing SSH keys, creating firewalls, setting up networks, writing Terraform modules, setting up CI/CD pipelines, or debugging infrastructure issues. Supports clean composable resource patterns. |
Quick Reference
| Action | Command |
|---|---|
| List servers | hcloud server list |
| Server types | hcloud server-type list |
| SSH keys | hcloud ssh-key list |
| Firewalls | hcloud firewall list |
| Networks | hcloud network list |
| Images | hcloud image list --type system |
| Locations | hcloud location list |
hcloud Patterns
Server Lifecycle
# Create server
hcloud server create \
--name myserver \
--type cx22 \
--image ubuntu-24.04 \
--location fsn1 \
--ssh-key my-key \
--firewall my-fw
# With cloud-init
hcloud server create \
--name myserver \
--type cx22 \
--image ubuntu-24.04 \
--user-data-from-file cloud-init.yaml
# Delete
hcloud server delete myserver
# Rebuild (wipe and reinstall)
hcloud server rebuild myserver --image ubuntu-24.04
Common Server Types
| Type | vCPU | RAM | Disk | Use Case |
|---|---|---|---|---|
| cx22 | 2 | 4GB | 40GB | Dev, small services |
| cx32 | 4 | 8GB | 80GB | Medium workloads |
| cx42 | 8 | 16GB | 160GB | Production services |
| cx52 | 16 | 32GB | 320GB | Heavy workloads |
| ccx13 | 2 | 8GB | 80GB | CPU-optimized |
SSH Keys
# Add key
hcloud ssh-key create --name mykey --public-key-from-file ~/.ssh/id_ed25519.pub
# List
hcloud ssh-key list
# Delete
hcloud ssh-key delete mykey
Firewalls
# Create firewall
hcloud firewall create --name web-fw
# Add rules
hcloud firewall add-rule web-fw --direction in --protocol tcp --port 22 --source-ips 0.0.0.0/0 --description "SSH"
hcloud firewall add-rule web-fw --direction in --protocol tcp --port 80 --source-ips 0.0.0.0/0 --description "HTTP"
hcloud firewall add-rule web-fw --direction in --protocol tcp --port 443 --source-ips 0.0.0.0/0 --description "HTTPS"
# Apply to server
hcloud firewall apply-to-resource web-fw --type server --server myserver
Private Networks
# Create network
hcloud network create --name internal --ip-range 10.0.0.0/16
# Add subnet
hcloud network add-subnet internal --type cloud --network-zone eu-central --ip-range 10.0.1.0/24
# Attach server
hcloud server attach-to-network myserver --network internal --ip 10.0.1.10
Terraform Patterns
See references/terraform.md for module templates and patterns.
Init & Apply
# Initialize
terraform init
# Plan
terraform plan -out=tfplan
# Apply
terraform apply tfplan
# Destroy specific resource
terraform destroy -target=hcloud_server.web
State Management
# List resources
terraform state list
# Show resource
terraform state show hcloud_server.web
# Remove from state (keep resource)
terraform state rm hcloud_server.web
# Import existing resource
terraform import hcloud_server.web 12345678
Cloud-Init Templates
See references/cloud-init.md for reusable templates.
CI/CD Pipelines
See references/cicd.md for GitHub Actions, GitLab CI, and deployment scripts.
Basic Pattern
#cloud-config
package_update: true
packages:
- docker.io
- docker-compose
runcmd:
- systemctl enable docker
- systemctl start docker
Troubleshooting
Server Won't Start
- Check server status:
hcloud server describe myserver - Check console:
hcloud server request-console myserver - Verify image exists:
hcloud image list --type system
SSH Connection Failed
- Check firewall:
hcloud firewall describe my-fw - Verify SSH key:
hcloud server describe myserver -o json | jq '.public_net.ipv4.ip' - Test connectivity:
ssh -v root@<ip>
Terraform State Drift
- Refresh state:
terraform refresh - Plan to see diff:
terraform plan - Import missing:
terraform import <resource> <id>