Skip to content

Terraform

Terraform is an infrastructure as code tool that lets you build, change, and version infrastructure safely and efficiently.

Warning

Caveat emptor: The intro was ripped from the official Terraform page. Makester is by no way endorsing Terraform as a preferred IaC. There are probably better ways to handle infrastructure deployments. Consider this only as a basic Terraform primer. For example, to stand up a managed Kubernetes cluster where you can then leverage GitOps. The less DevOps in your project, the better ...

It is wise to take a pragmatic approach to DevOps as it is an evolving discipline. I have seen projects put too much faith in the DevOps process which ultimately leads to rigid systems that are prone to failure and resistance to change. In a fast-moving technological landscape aim to build flexible systems that evolve with emerging trends, and are not to the detriment of the product. Furthermore, DevOps is not a panacea for bad system design ...

If you are embarking on your first DevOps project with Terraform, get ready for version skew, incompatibilities, conflicts, provider decay, the list goes on. As you start working on more Terraform projects you will notice that the only consistency when it comes to project layouts is that they will all be different. That is where the major consultancy firms will burn you with convoluted pre-baked deployments that they on-sell to unsuspecting clients who are blinded by the quick-win fallacy of the OpEx revolution. As the consultancies are the only ones that can operate and manage the ensuing mess, you're stuck with that turd until you come to your senses.

Getting started

Ensure Terraform is available in your path (we recommend installing tfenv).

If you are operating Makester in minimal mode, then append terraform to MAKESTER__INCLUDES to enable the Makester Terraform subsystem.

Command reference

Create a simple Terraform project directory layout

make tf-project-create

Makester will produce the following directory layout:

terraform
├── data.tf
├── locals.tf
├── main.tf
├── provider.tf
├── terraform.tfvars

provider.tf is pre-populated with the standard terraform block, ready to start adding your providers. All other files within the terraform directory are simple stubs, only.

With new Terraform configuration files in place, you will need to initialise the working directory.

make tf-state-ls

It is possible to filter resources by providing an address to the MAKESTER__TERRAFORM_RESOURCE variable:

make tf-state-ls MAKESTER__TERRAFORM_RESOURCE=<OBJECT_ADDRESS>

Initialise a Terraform working directory

Terraform command: init

This is the first command that should be run after writing a new Terraform configuration and is safe to re-run multiple times.

make tf-init

Validate Terraform configuration files

Terraform command: init

Checks if configuration is syntactically valid and internally consistent. Validation does not access remote state.

make tf-validate

Display current Terraform version

Terraform command: init

make tf-version

Preview the Terraform execution plan

Terraform command: init

Report on state changes before applying.

make tf-plan

Execute the Terraform execution plan

Terraform command: init

Applies the Terraform configuration in the working directory.

make tf-apply

Destroy all remote objects managed by Terraform configuration

Terraform command: init

make tf-destroy

Rewrite Terraform configuration with consistent formatting

Terraform command: init

Formatting is based on a subset of the Terraform language style conventions.

make tf-fmt

Check Terraform configuration formatting

Terraform command: init

Report on Terraform configuration files that are subject to formatting changes.

make tf-fmt-check

Display Terraform configuration formatting diffs

Terraform command: init

Display Terraform configuration file formatting differences.

make tf-fmt-diff

Launch interactive console

Terraform command: init

Console allows you to evaluate Terraform expressions and interact with any values that are currently saved in the configuration state.

make tf-console

Inspecting infrastructure state

Terraform command: state list

List all resources in the state file:

make tf-state-ls 

Workspaces: list all available workspaces

Terraform command: state list

make tf-ws-list MAKESTER__TERRAFORM_WS=<WORKSPACE_NAME>

Workspaces: create a new workspace

Terraform command: state list

Create a new Terraform workspace as per the value defined by MAKESTER__TERRAFORM_WS.

make tf-ws-new MAKESTER__TERRAFORM_WS=<WORKSPACE_NAME>

Workspaces: delete a workspace

Terraform command: state list

Delete Terraform workspace defined by MAKESTER__TERRAFORM_WS.

make tf-ws-delete MAKESTER__TERRAFORM_WS=<WORKSPACE_NAME>

Workspaces: choose a workspace to use

Terraform command: state list

Select Terraform workspace defined by MAKESTER__TERRAFORM_WS.

make tf-ws-select MAKESTER__TERRAFORM_WS=<WORKSPACE_NAME>

Remove a local binding to an existing remote object without first destroying it

Terraform command: state rm

Remove a binding to an existing remote object without first destroying it. This makes Terraform "forget" the object while it continues to exist in the remote system.

make tf-pristine MAKESTER__TERRAFORM_RESOURCE=<ADDRESS>

Note

ADDRESS must be provided. Otherwise an error is generated.

Variables

MAKESTER__TERRAFORM_RESOURCE

Switch to a different working directory before executing the given subcommand. Defaults to $(MAKESTER__PROJECT_DIR)/terraform.

See Switching working directory with -chdir.

MAKESTER__TERRAFORM_WS

Control Terraform workspace context. Default is default.

See Workspaces.


top