Skip to content

Managing cloud instances with terraform

This page describes how to create new servers or cloud instances with various German IaaS hosting providers using terraform.

API tokens

Terraform requires an API token to manage instances with your cloud provider.

Hetzner

Create a new datacenter (Example name: cus_www_prod) in the hetzner cloud and go to Security -> API tokens to create a read & write token. Then add the API token in the playbook:

pass edit env_hcloud_cus_www_prod
compile-passwords.py
source .env

Terraform provider Hetzner cloud documentation

IONOS

IONOS cloud only wants your regular user and password to use terraform:

pass edit env_ionos_username
pass edit env_ionos_password
compile-passwords.py
source .env

Terraform provider 1und1 IONOS cloud documentation

SysEleven

SysEleven uses OpenStack, which requires an API token:

pass edit env_os_auth_url
pass edit env_os_region_name
pass edit env_os_auth_token
compile-passwords.py
source .env

All environment variables

Terraform provider Openstack documentation

Creating new servers

To manage a new group of servers with Ansible Cake, create the terraform configuration files below the terraform/ directory:

mkdir -p terraform/hcloud/cus_www_prod/
editor terraform/hcloud/cus_www_prod/main.tf

To take the config files of a similar project as a template:

cp -r terraform/hcloud/cus_util_prod terraform/hcloud/cus_www_prod
rm -rf terraform/hcloud/cus_www_prod/{.terraform,terraform.tfstate,terraform.tfstate.backup,versions.tf}
editor terraform/hcloud/cus_www_prod/main.tf

To apply a new configuration:

# This might be required, if so the next step will also tell you that
terraform 0.13upgrade .

# Initialize terraform directory and install plugins
terraform init

# Dry-run
terraform plan

# Actually apply (will ask for your "yes" confirmation)
terraform apply

Adding to cake.conf.sh

To simplify the process of managing the new terraform setup for the next time, create a function like this in cake.conf.sh:

# cus_www_prod
cus_www_prod_terraform() {
    cd terraform/cus_www_prod
    terraform init
    terraform apply
}
cus_www_prod_terraform_destroy() {
    cd terraform/cus_www_prod
    terraform destroy
}

This function can then be called with:

CAKE master * cake -f cus_www_prod_terraform

Modifying servers

If you later on want to change the servers and add RAM, CPUs or resize disks, simply edit the main.tf file and re-run cake -f cus_www_prod_terraform.