Installation & Setup

Installing terraform differs from platform to platform, but here is a small overview. It can be installed via the terminal using:

Windows

Requires either winget or choco installation

winget install -e -id Hashicorp.Terraform
choco install terraform -pre

Mac

Requires homebrew Installation

brew install terraform

Linux (Ubuntu)

Use the built in package manager, here with Ubuntu

sudo apt install terraform -y

For the latest way of installing to your OS, see the latest documentation from HashiCorp.


Usecases

Terraform allows you to define your infrastructure as code (IaC). This not only make it human readable, but also versioned and easily shared/documented.

With multi cloud support, you can manage resources consistently across numerous cloud providers, but also on premise environments, such as Proxmox.

It reduces manual work and therefore also manual errors, so deployments can be sped up.

Version control integration ensures that you can track changes and roll back when needed - but also collaborate with team members, drastically reducing knowledge silos, since it is all there, in the code.

Using the modules and templates ensures configuration reliability and reusability across projects and environments.


What is IaC?

Infrastructure as Code (IaC) is a practice most commonly used in DevOps (but can be practiced regardless of agile development framework) and cloud computing that involves managing and provisioning computing infrastructure through machine-readable config files, rather than through physical hardware configuration or interactive tools.

Having the configuration in code, means we can save it, version control it, share it, scale it, easily replicate and keep it in a state file.


HCL

HCL (HashiCorp Configuration Language) is the language built by HashiCorp which is used for configuring your resources.

Being very human readable, it strikes a resemblance to that of YAML or JSON and is the primary language used to writing Terraform configuration files.

Terraform uses HCL in .tf files to describe infrastructure through blocks, arguments, variables and expressions.

Terraform is declarative; it describes the desired infrastructure state rather than a sequence of commands to create it.


Basic Syntax

The HCL syntax consists of three parts:

Structural Language

This is the part where we define blocks, attributes and expressions. The blocks are the fundamental unit, such as:

provider {
  ...
}

resource {
  ...
}

module {
  ...
}

Expression Language

This part is used to express the attribute values, either as literals or derivations of other values.

variable "some_variable" {
  type    = string
  default = "some_string"
}

Template Language

Is used to compose values together as strings.

message = "%{ if var.enabled }Service is on%{ else }Service is off%{ endif }"