Introduction to Terraform

Terraform is an awesome Open Source tool for building, changing and versioning infrastructure. It is a good start to anyone that want to try Cloud providers, because it has a lot of service providers for beginners and can be extended via in-house solutions for all use-cases.

Traditionally, developers have little or nothing to do when it comes to the servers or application hosting. However, with the current approach, it is more easy than ever start playing with infrastructure for personal projects, or more robust infrastructures for work stuff, because you can add Continuous Integration in your projects.

Infrastructure as Code

Terraform is built in Golang, and uses configuration files to describe all the components of the infrastructure from small to entire application stacks. Terraform can be managed from high level components as DNS entries, security groups, firewalls to low components as compute instances (VPS), storage, and networking.

The main industry shift with Terraform is the term “Infrastructure as a Code” that is use to describe using high level syntax, making the configurations files a direct blueprint of the current, future and past state of the infrastructure.

Less Scripting More Testing

By design, Terraform is built to relied in minimal interaction to perform basic to complex changes. It will know exactly what should be changed and placed in a logical order, avoiding many possible human errors.

There are binaries for all platforms (Linux, Windows, MacOS) making it easy to use, but some people will argue that this sounds too complicated for beginners.

If you have the second opinion maybe you will like to know that you can use scripting inside Terraform in the form of template.

But what is better is the illustrated beauty of Terraform. The most common server that  developers use is a LAMP stack. In this case, Vultr is appropriate for this example.

Manual Deploy

This approach can work for small amount of servers, but with time becomes a nightmare of dependencies or infrastructure drift.

  1. Create droplet (VPS)
  2. Login to machine (via password or ssh key)
  3. Install packages (LAMP stack)
  4. Create DNS record in provider (Vultr)
  5. Manage changes

Note: Even if you have a install.sh script to handle the VPS itself, this will take somewhere from a couple of minutes to a few hours depending your experience with the shell to configure one server only, imagine this at a larger scale is technically impossible.

Terraform Deploy

The advantage of this approach is that you do not make any manual changes to your servers in the manual base, but instead make new servers with the new changes in a git-based workflow, knowing all the aspect of how is built.

  1. Configure your access to the provider
  2. Make the main.tf file
  3. Deploy your first server (Terraform apply)

Note: This means that running the contrary command will erase all infrastructures without leaving anything behind. This means that you can test servers for your personal labs.

Example

I use this file to do the demo. I have more examples in this repository for this provider.

provider "vultr" {
    api_key = "${var.vultr_api_key}"
}

data "vultr_region" "region" {
    filter {
        name   = "name"
        values = ["${var.vultr_region}"]
    }
}

data "vultr_os" "os" {
    filter {
        name   = "name"
        values = ["${var.os_version}"]
    }
}

data "vultr_plan" "plan" {
    filter {
        name   = "price_per_month"
        values = ["${var.plan[0]}"]
}

filter {
    name   = "ram"
    values = ["${var.plan[1]}"]
}


#creating ssh_key
resource "vultr_ssh_key" "terraform_infra" {
    name       = "terraform_infra"
    public_key = "${var.pub_key}"
}

resource "vultr_instance" "instance" {
    name              = "${var.instance_name}"
    region_id         = "${data.vultr_region.region.id}"
    plan_id           = "${data.vultr_plan.plan.id}"
    os_id             = "${data.vultr_os.os.id}"
    hostname          = "${var.instance_name}"
    ssh_key_ids       = ["${vultr_ssh_key.terraform_infra.id}"]

    connection {
        user        = "root"
        type        = "ssh"
        private_key = "${file("~/.ssh/dokey")}"
        timeout     = "2m"
    }
}
  
provisioner "remote-exec" {
    inline = [
        "sudo apt-get update",
        "sudo apt install tasksel",
        "sudo tasksel install lamp-server > /dev/null"
    ]
}
Demo Using vultr Provider

You can build and destroy your infrastructure in minutes, or play with developer mode on your own projects fast and quickly.

Are you more interested in this tool? Great! A tutorial series for beginners about Terraform is coming soon.

0
0

Related Posts