Terraform notes

Terraform home page
Terraform Documentation
Terraform best pratices

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently.

Infrastructure as Code

Infrastructure is described using a high-level configuration syntax. This allows a blueprint of your datacenter to be versioned and treated as you would any other code. Additionally, infrastructure can be shared and re-used.

Terraform allows to write code that is specific to each provider, taking advantage of that provicer's unique functionality, but to use the same language and toolset.

Initial release: 2014.

Other provisioning tools:

Commands

Commands:

AWS ENV:

Syntax

Input varaibles

variable "<name>" {
    [description = "<description>"]
    [default = <default value>]
    [type = "<type>"]
}

Providing a variable value:

Type options (terrafomr can gues the type):

Outputs variables

output "<name>" {
    value = <value>
}

Data sources

A data source represents a piece of read-only information that is fetched from the provider.

Lifecycle

create_before_destroy: create a replacement resource before destroying the original.

Tips

Starting service on image setup:

resource "aws_instance" "app" {
    ...

    user_data = <<-EOF
    #!bin/bash
    sudo service myservice start
    EOF
}

user_data - a script that executes when the server is booting.

Don't put terraform state under version control

Problems:

Use remote state storage. Available options:

S3 remote storage:

provider "aws" {
    region = "us-east-1"
}

resource "aws_s3_bucket" "terraform_state" {
    bucket = "my-bucket-name"
    versioning {
        enabled = true
    }
    lifecycle {
        prevent_destroy = true
    }
}
terraform apply
terraform remote config \
    -backend=s3 \
    -backend-config="bucket=my-bucket-name" \
    -backend-config="key=global/s3/terraform.tfstate" \
    -backend-config="region=us-east-1" \
    -backend-config="encrypt=true"

Project structure and isolation

Use a folder per environment.

Resource folder:

Templet files

Use templates files instead of interpolation, for large scripts.

Module versioning

Use versioning for modules.

Count

resource "aws_iam_user" "example" {
    count = 10
    name = "myuser.${count.index}"
}

Or

resource "aws_iam_user" "example" {
    count = "${length(var.names)}"
    name = "${element(var.names,count.index)}"
}

If statement

Use count.

Vocabulary

Infrastructure as Code

Infrastructure as Code (IAC) - write and execute code to define, deploy, and update your infrastructure.

Terraform At Scale by Calvin French-Owen on YouTube
Terraform: Up and Running by Yevgeniy Brikman

Licensed under CC BY-SA 3.0