Terraform#
Terraform is an Infrastructure as Code (IaC) tool used by the Platform Engineering team as its primary means to create and control cloud based infrastructure.
Terraform is a vendor agnostic tool developed by Hashicorp that interprest terraform (.tf) files written in the Hashicorp Configuration Language (HCL).
The tool itself is an executable that has built in commands to setup, inspect and execute a given instruction.
Getting Started#
To use Terraform in a development worklofw the Platform Engineering team have developed a set of components that work with our instance of Terraform Cloud (TFC).
We can provision the project and workload along with authentication to a target cloud in order to setup pipelines that can consume the components.
Commands#
The following section goes through the common commands used in a terraform workflow.
Init#
terraform init is used to initialise a folder ready to run further instructions, typically this is a "src" folder for pure infrastructure projects, but could easily be any folder with terraform files within it.
Plan#
terraform plan is used to determine what actions terraform will take against a target system. This is used to check what will be created, changed or destroyed
Apply#
terraform apply is used to execute the files or plan. Apply will attempt to carry out the actions it details in it's initial output and may ask you to confirm the changes before it attempts the change.
How Terraform Tracks Changes#
Terraform tracks changes through a concept called state state is often stored in the terraform.tfstate file which is a sort of JSON based document database of all the knowledge terraform has of a given development.
Changes are often written to state once they've been applied.
!NOTE: Terraform can handle things like database passwords and these often end up in the statefile in plain text. Treat the statefile as you would a password or secret and keep it safe (for help in this place reach out to the platform engineering team)
Declaring Resources#
Azure Resource Group Example#
The code below shows how to declare a resource group in Azure
resource "azurerm_resource_group" "group" {
name = "my-first-resource-group"
location = "uksouth"
}
Tutorials#
Hashicorp provide a suite of tutorials that can be used as a starting point for getting hands on.