This repo contains configurations for provisioning cloud resources in Hetzner using Terraform. Specifically, the following resources will be provisioned:
- A Hetzner server running Linux Ubuntu OS 24.04 LTS
- Both
root
user and a custom-specified non-root
user present on provisioning - Reference/starter bash profile present on provisioning
- Both
- A Hetzner volume, attached to the server
- Docker Engine preinstalled on the server
Configurations are given in main.tf
. Furthermore, user-specified values are listed in reference file sample.terraform.tfvars
. Also, reference/starter bash aliases are provided in file .bash_aliases
.
Note
These instructions assume a Unix-like terminal and executable (i.e., terraform
, paths /path/to/file
, etc.). For Windows host systems, make corresponding adjustments (e.g., executable terraform.exe
rather than Unix-like terraform
, Windows-style paths, etc.) and/or use an appropriate Unix-based terminal application for Windows (e.g., Git Bash).
- Clone this repo locally as follows and change into it:
git clone https://github.com/awpala/terraform-for-hetzner-ubuntu-vm.git
cd terraform-for-hetzner-ubuntu-vm
-
Create an account in Hetzner, and set up billing for this account.
-
Download the Terraform executable for the corresponding host system of choice. Extract the executable from the downloaded
.zip
file into the locally cloned repository (i.e.,.../terraform-for-hetzner-ubuntu-vm/
, now containing corresponding executable.../terraform-for-hetzner-ubuntu-vm/terraform
). -
If not present already, create a new SSH public/private key pair on the host system (see here for reference). This setup assumes an
ed25519
key. Otherwise, if using a different key (e.g.,rsa
,ecdsa
, etc.), make corresponding changes inmain.tf
before proceeding (i.e., change values fromid_ed25519
to appropriate key types). Furthermore, copy these keys (i.e.,id_ed25519
andid_ed25519.pub
, or equivalent) into the locally cloned repository (i.e., from~/.ssh/
to.../terraform-for-hetzner-ubuntu-vm/
). -
In the Hetzner Cloud Console, create a new project. Additionally, add a public key to the
SSH keys
underSecurity
settings for this project (use the same public key as in the previous step), and additionally create a new API token underAPI tokens
(save/retain this value for reference on its creation). -
Create a new file
terraform.tfvars
(i.e.,.../terraform-for-hetzner-ubuntu-vm/terraform.tfvars
) and populate values accordingly as specified in reference filesample.terraform.tfvars
(user-specified values denoted by<...>
, replaced with correspondingstring
/number
types as defined inmain.tf
). All non-default values must be specified, otherwise defaults (cf.main.tf
) are pre-specified as follows (these can additionally be overridden accordingly, if so desired):
Variable Name | Default Value | Unit of Measure | Reference |
---|---|---|---|
"server_image" |
"ubuntu-24.04" |
(N/A) | https://docs.hetzner.com/robot/dedicated-server/operating-systems/standard-images/ |
"server_location" |
"ash" |
(N/A) | https://docs.hetzner.com/cloud/general/locations/ |
"server_type" |
"cpx11" |
(N/A) | https://docs.hetzner.com/cloud/servers/overview/ |
"volume_size" |
10 |
GB | https://docs.hetzner.com/cloud/volumes/overview/ |
- Run the following command to update dependencies (this will correspondingly modify lockfile
.terraform.lock.hcl
if necessary via flag--upgrade
, as well as generate dependencies in local directory/.terraform/
):
./terraform init --upgrade
- Run the following command to provision the resources in the Hetzner project (supply affirmative response
yes
in the terminal when prompted):
./terraform apply
-
Once the terminal prompt completes, the server should now be provisioned, along with the attached volume. This can be verified in the Hetzner Cloud Console for the corresponding project. To access the provisioned server, simply SSH from the host machine (using the same machine from which the public key was generated/derived). Furthermore, note that the volume is mounted in the server at location
/mnt/<volume_name>
(with<volume_name>
as specified interraform.tfvars
). -
To destroy/deprovision the resources, simply run the following command (supply affirmative response
yes
in the terminal when prompted):
./terraform destroy