Terraform

Let’s talk about Terraform. I’m not a profesionnal and not a system adminstrator, just trying things because it give me lot of fun.

The aim of Terraform is to make a script about how you want to setup your machine (often a server) and then perform it as many as you want without pain. Remember how many time you spend the last time you installed your machine, install basic packages, setup the ssh configuration and rights access, firewall. Now think about doing this only once and perform again with one command. Nice dream ? Let’s make this dream true.

Download Terraform

First of all, we have to do with all awesome projects, download the binary. All binaries are present on this page : https://www.terraform.io/downloads.html

Because i’m a nice boy, i give you the command for MacOS and Linux for having the expected binary without pain :

MAC

wget -O terraform.zip https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_darwin_amd64.zip && unzip terraform.zip && rm -rf terraform.zip

LINUX (amd64)

wget -O terraform.zip https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip && unzip terraform.zip && rm -rf terraform.zip

Testing

Because we have to test Terraform with machines, it is very useful to use virtual machines for trying make a setup. Indeed if you make mistakes (and you will do 😉) you can just detroy your virtual machines and try again. For my test i use virtualbox because the provider driver for it exists and work well with Terraform.

Install Virtual Box provider

Provider driver is a go project available on github at this address : https://github.com/terra-farm/terraform-provider-virtualbox

After several tests, i found theses steps to download, build and use the driver :

Download the go package. Obviously, you should have Go installed on your computer, with your favorite package manager.

go get github.com/terra-farm/terraform-provider-virtualbox

Go package are installed in you go path, so go to the downloaded package spot

cd $GOPATH/src/github.com/terra-farm/terraform-provider-virtualbox

Build the provider

go build

Terraform’s plugins have to be placed at a specific place to be use by scripts. So we create the folder if it did not exists and move the builded provider to it

mkdir -p ~/.terraform.d/plugins/
mv terraform-provider-virtualbox ~/.terraform.d/plugins/.

Congratulations, your virtualbox driver is ready to use !

Definitions

  • provider: provider of resources by API (list).
  • resource: element which can be create, read, update and delete thought the provider.
  • data source: same as resource except that is read only, not CRUD like resource.
  • state: storage of the state of the resource. Stored as a tfstate file.

Descriptions

Provider

provider "kubernetes" {
  # Very important to take the habit to specify
  # the version of the used version. 
  # API updates could bring breaking changes !
  version = "1.10" 
}

Resource

# resource_type => resource of the provider
# resource_name => free field for naming
resource "resource_type" "resource_name" {
  arg = "value"
}

Date source

data "data_type" "data_name" {
  arg = "value"
}

Metadata

  • count: allows to make interations on resource, if count equals two, the resource will be iterated two times.

Variables

âž¡ type

  • string
  • number
  • bool
  • list
  • map

If a variable is declared without a type, terraform will ask you the value when you will apply.

âž¡ usage

  • var.name
  • “${var.name}

âž¡ order

There is five levels of variable in terraform

    1. Environment
export TF_VAR_nameOfVariable="value"
    1. file : terraform.tfvars
nameOfVariable="value" 
    1. file json : terraform.tfvars.json
    1. file *.auto.tfvars ou *.auto.tfvars.json
Same as #2
    1. CLI : -var ou -var-file
terraform apply -var 'str="var"' -var-file myFile

higher is better (variable is level 5 is more important that variable in level 4). Environment is the worst.