Skip to main content

Hashicorp Terraform - Setup

Victoria Harbour, Hong Kong

Terraform allows infrastructure to be expressed as code. The desired state is expressed in a simple human readable language. Terraform uses this language to provide an execution plan of changes, which can be reviewed for safety and then applied to make changes. Extensible providers allow Terraform to manage a broad range of resources, including hardware, IaaS, PaaS, and SaaS services.

Infrastructure as Code

Use infrastructure as code to provision infrastructure. Codification enables version control and automation, reducing human error and increasing productivity. Codification allows infrastructure changes to be automated, while keeping the definition human readable

Toolsets

Configuration ManagementServer TemplatingServer Provisioning
Nomad, Ansible, SaltStackDocker, Vagrant, PackerTerraform, Docker Compose or Docker Swarm
  • Configuration Management tools install and configure software. Maintain configuration structures (Version Control) and are idempotent (every run results in the same server state - no matter what state the server was in before).
  • Server Templating tools create images that contain all desired software and their dependencies. Running these images provide an immutable software infrastructure.
  • Server Provisioning tools deploy immutable infrastructure to cloud provider with declarative code.

So what is the difference between Nomad and Terraform? Only the provider plugins that directly connect you to Cloud providers like AWS or GCE ?

Installation

You can download the latest version of Terraform as a binary from the Hashicorp Releases:

wget https://releases.hashicorp.com/terraform/1.0.7/terraform_1.0.7_linux_amd64.zip
unzip terraform_1.0.7_linux_amd64.zip
rm terraform_1.0.7_linux_amd64.zip
sudo mv terraform /usr/bin/terraform

Verify that it is working:

terraform -v

Terraform v1.0.7
on linux_amd64

Configuration

Start by creating a directory where you want to store your configuration:

sudo mkdir /etc/terraform.d

As provider I want to start out with local resources which would be represented by a configuration file like:

sudo nano /etc/terraform.d/main.tf

Create a File

resource "local_file" "resource_name" {
filename = "/opt/resource.txt"
content = "some content"
directory_permission = "0777"
file_permission = "0700"
}

Create a File from a Template

If the file is created by another tool like Nomad and you want Terraform to use it you can use the Data_Block instead of an Ressource_Block:

resource "local_file" "resource_name" {
filename = "/opt/resource.txt"
content = data.local_file.dog.content
directory_permission = "0777"
file_permission = "0700"
}

data "local_file" "data" {
filename = "/opt/data.txt"
}

Create Multiple Instances of a File

Count

main.tf

resource "local_file" "resource_name" {
filename = var.filename[count.index]
content = "some content"
directory_permission = "0777"
file_permission = "0700"

count = length(var.filename)

}

variables.tf

variable "filename" {
default = [
"/opt/resource1.txt",
"/opt/resource2.txt",
"/opt/resource3.txt"
]
}

For-Each

main.tf

resource "local_file" "resource_name" {
filename = each.value
content = "some content"
directory_permission = "0777"
file_permission = "0700"

for_each = var.filename

}

variables.tf

variable "filename" {
type=set(string)
default = [
"/opt/resource1.txt",
"/opt/resource2.txt",
"/opt/resource3.txt"
]
}

for-each can only be used with variable sets (sets = lists without duplicated members).

Terraform Life Cycle

Run the following commands from the directory you created the configuration file in:

File Format (Optional)

Making sure that the file is in a canonical format:

sudo terraform fmt
main.tf

Validtation

terraform validate
Success! The configuration is valid.

Initialization

sudo terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/local...
- Installing hashicorp/local v2.1.0...
- Installed hashicorp/local v2.1.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Planning

sudo terraform plan

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

# local_file.resource_name will be created
+ resource "local_file" "resource_name" {
+ content = "some content"
+ directory_permission = "0777"
+ file_permission = "0700"
+ filename = "/opt/resource.txt"
+ id = (known after apply)
}

Plan: 1 to add, 0 to change, 0 to destroy.

────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform
apply" now.

Run

sudo terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

# local_file.resource_name will be created
+ resource "local_file" "resource_name" {
+ content = "some content"
+ directory_permission = "0777"
+ file_permission = "0700"
+ filename = "/opt/resource.txt"
+ id = (known after apply)
}

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.

Enter a value: yes

local_file.resource_name: Creating...
local_file.resource_name: Creation complete after 0s [id=94e66df8cd09d410c62d9e0dc59d3a884e458e05]

Verify

sudo terraform show

# local_file.resource_name:
resource "local_file" "resource_name" {
content = "some content"
directory_permission = "0777"
file_permission = "0700"
filename = "/opt/resource.txt"
id = "94e66df8cd09d410c62d9e0dc59d3a884e458e05"
}
sudo cat /opt/resource.txt
some content

Destroy

sudo terraform destroy

local_file.resource_name: Refreshing state... [id=94e66df8cd09d410c62d9e0dc59d3a884e458e05]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- destroy

Terraform will perform the following actions:

# local_file.resource_name will be destroyed
- resource "local_file" "resource_name" {
- content = "some content" -> null
- directory_permission = "0777" -> null
- file_permission = "0700" -> null
- filename = "/opt/resource.txt" -> null
- id = "94e66df8cd09d410c62d9e0dc59d3a884e458e05" -> null
}

Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.

Enter a value: yes

local_file.resource_name: Destroying... [id=94e66df8cd09d410c62d9e0dc59d3a884e458e05]
local_file.resource_name: Destruction complete after 0s

Life Cycle Rules

Create Before Destroy

When changes are made to your infrastructure Terraform first deletes the old state, then it re-creates the infrastructure in the new state. To revert use:

resource "local_file" "resource_name" {
filename = "/opt/resource.txt"
content = "some content"
directory_permission = "0777"
file_permission = "0700"

lifecycle {
create_before_destroy = true
}
}

Prevent Destroy

If you don't want old version to be destroyed by changes - terraform destroy will still remove everything:

resource "local_file" "resource_name" {
filename = "/opt/resource.txt"
content = "some content"
directory_permission = "0777"
file_permission = "0700"

lifecycle {
prevent_destroy = true
}
}

Ignore Changes

A re-run of the Terraform task should not change back something if it has been changed by an external software - or use ignore_changes = all to ignore everything:

resource "local_file" "resource_name" {
filename = "/opt/resource.txt"
content = "some content"
directory_permission = "0777"
file_permission = "0700"

lifecycle {
ignore_changes = [
file_permission
]
}
}