News

Back
2017
November
01
2017

"Infrastructure as Code" with Terraform

When projects grow beyond a single server, it usually makes sense to manage the required infrastructure automatically. After Libcloud and Ansible it is now also possible to use Terraform to define complete setups at cloudscale.ch "as code".

What distinguishes Terraform

Terraform is a prominent representative of the term "Infrastructure as Code" and fits perfectly into the DevOps philosophy of today's projects. Where servers were previously commissioned manually, rarely configured identically and often not documented at all, this reverse approach now ensures efficiency and order: a configuration file that is easy to read for both humans and computers describes the required infrastructure – and Terraform does the rest.

Terraform works declaratively: You describe the desired state of your setup and Terraform derives the necessary actions from it. As soon as you apply the configuration you have defined, Terraform will make the respective adjustments via our API until the described state is reached. And because your infrastructure – just like code – changes over time, Terraform configurations are best managed in versioning systems such as Git.

Terraform is an open-source project. The software is available free of charge.

How to use cloudscale.ch with Terraform

If you are already using Terraform and would like to manage your servers at cloudscale.ch, simply include the plugin for the provider "cloudscale". It runs as a separate process and communicates with the main Terraform process via an RPC interface. In addition, you need an API token with read/write access, which you need to create once in our Cloud Control Panel. Using this token, Terraform authenticates itself to our API on every change to your infrastructure.

Starting a server with Terraform is very easy, as the following section shows:

# Create a new Server
resource "cloudscale_server" "web-worker01" {
  name           = "web-worker01"
  flavor_slug    = "flex-4"
  image_slug     = "debian-9"
  volume_size_gb = 50
  ssh_keys       = ["ssh-ed25519 XXXXXXXXXX...XXXX user@example.com"]
}

This is just a simple example for a single server; all available options for more complex setups can be found in our Terraform provider documentation.

Two tips from practice

Automating things saves a lot of time. But what if the automatism does not work as intended? Terraform comes in handy here: with the command terraform plan you can see in detail what changes Terraform would make on the way from the current to the desired configuration. If necessary, you can revise the settings as needed – until the "execution plan" exactly meets your expectations. Only then does a terraform apply actually make changes to your server infrastructure.

Possibly not everyone who has access to your code or configuration repository should also have access to your services at cloudscale.ch. We therefore recommend that you pass your API token as a variable within Terraform and exclude the corresponding source file from versioning. Alternatively, set a shell environment variable called "CLOUDSCALE_TOKEN", which is automatically used by Terraform.


At cloudscale.ch you have always been able to adjust your infrastructure as desired. Terraform now enables you to define your desired configuration and it will make the necessary adjustments for you.

We have the infrastructure for your code!
Your cloudscale.ch team


PS: Terraform is a software written in Go and requires a corresponding Go SDK for each provider. Our Go SDK has been released under the MIT license and allows Go developers (regardless of Terraform) to send HTTPS requests natively from any Go application to our API.

Back to overview