Adam Paterson
Adam Paterson
Author

Programmer, fiancé to one girl. Father to two others.

Share


Tags


Twitter


Terraform: Infrastructure as "code"

The purpose of Terraform is to build, change and version infrastructure safely and efficiently using human readable configuration files.

Adam PatersonAdam Paterson

Introduction

Terraform is the latest edition to the Hashicorp catalog. These guys have already brought us everyday tools like Vagrant, Packer and Serf. The purpose of Terraform is to build, change and version infrastructure safely and efficiently using human readable configuration files.

Infrastructure can include physical machines, VMs, network switches, containers, etc. Almost any infrastructure component can be represented as a resource in Terraform.

Terraform refer to their selling points as:

  • Infrastructure as code: Like it's predecessor Vagrant, Terraform tries to ease the creation, modification and destruction of cloud infrastructure using a high-level configuration syntax. Allowing your application server/datacenter to be versioned as you work with any other code.
  • Execution Plans: Terraform generates an execution plan which describes what the software will do when you call terraform apply. This means you have a overview of what Terraform plans to action, letting you avoid any nasty surprises.
  • Resource Graph: terraform graph will generate a graph of all your resources and parallelizes the creation/modification of all non-dependant resources.
    Syntax

Hashicorp have developed the Terraform Configuration Syntax. The reason for this is to balance human readable and editable configurations while at the same time being machine friendly.

Terraform Syntax

resource "digitalocean_droplet" "web" {  
    name = "ap-web"
    size = "512mb"
    image = "centos-5-8-x32"
    region = "usa2"
}

Don't worry though, if you wish to create your Terraform configuration using an application it also supports JSON configuration files.

JSON Syntax

"resource": {  
    "digitalocean_droplet": {
        "web": {
            "name": "ap-web",
            "size": "512mb",
            "image": "centos-5-8-x32",
            "region": "usa2"
        }
    }
}

The above examples declare a resource type of digitalocean_droplet and is the first step of creating a new DigitalOcean VPS instance.

Use Cases

Being a PHP developer and having a limited understanding on system architecture I don't feel equipped to talk about some of the other use cases such as; Software Defined Networking (SDN), Resource Sechdulers or Self-Service Clusters.

So instead will present the two main use cases I will be using Terraform for in the future.

Software Demos

Although Vagrant allows us to provision and build virtualized environments for demos, it's a real challenge to demo software on real infrastructure which more closely matches production environments.

Running on a MacBook Pro I obviously only have limited resource available for a virtual machine. Terraform solves this issue by allowing us to create a disposable environment, with the only restraint being the provider capabilities.

Example:

We have a bug report that a client's web server is falling over when visitor traffic peaks. Our server administrator has advised to upgrade their VPS to increase the memory and trasfer limits.

Using Terraform we can "spin up" a DigitalOcean instance which matches the prodution environment to prove or debunk the clients claims. More importantly, if the client is correct. We can simply modify the Terraform configuration to make the recommend increases to re-test.

Using DigitalOceans highest volume solution to test the new infrastructure changes for 2 hours would cost us £1.10. It would also save us hours/days of work setting up an aditional server to test the cliams.

Conclusion

Although Terraform refers to their goal as "Infrastructure as code" I don't think this is correct. Terraform lacks the common logical constructs found in programming languages. You could argue this isn't the purpose of Terraform, however in my opinion it would be a welcomed addition to the tool.

Having said that, I'm looking forward to seeing how Terraform progresses and really looking forward to using it on freelance and Juno projects in the near future. When I get change to test it properly I'll follow up with my findings and examples.

Adam Paterson
Author

Adam Paterson

Programmer, fiancé to one girl. Father to two others.

Comments