HashiCorp Terraform

Terraform is an #open source automation tool used for #Infrastructure as Code (IaC) which is available in multiple platforms such as Windows, Apple and Linux. It can manage on-premise server, Cloud Computing# (AWS, Azure, Google Could Platform), #Virtual Machine (VM) (VMWare), Version Control System (VCS) and protocol (HTTP, TLS, Domain Name Server (DNS)) using Terraform Providers# available in Terraform Registry#.

Terraform uses HashiCorp Configuration Language (HCL)# as its configuration language and utilises its declarative nature to set up infrastructure accordingly.

A usual Terraform workflow should start with initialising the project and identify the Terraform Providers# to be used for the target environment using the command terraform init (initialise phase). Then, dry-run or test blueprint for each target environment by laying out the action that will be carried out with the command terraform plan (planning phase). Finally, apply the blueprint and make necessary changes for the target environment accordingly by running terraform apply (apply phase). Both terraform plan and terraform apply change the state of Terraform. The state of the resources is recorded in the file terraform.tfstate. If there is a disparity between current environment and the blueprint such as missing plugins#, Terraform will fix it in order to adhere to the specified requirements. Otherwise, one could simply run terraform refresh to synchronise the state of Terraform and the underlying infrastructure. We can import resources that is created manually or providers that is outside Terraform Registry# (from other #Infrastructure as Code (IaC) tools).

Note: You could modify the state using terrafrom state. However, it is for advanced usage so use it with care. You can inspect the state of a resource using terraform state {provider}.{resource} syntax.

One don’t need to run the mentioned two commands in order to find out configuration errors within the project, terraform validate should be enough to do the job. Be aware that the command can’t access any remote service such as remote state and provider APIs, so the values might not be checked. The command terraform show can be used to show the resource’s details defined in the file. Pass the option -json if you want them to be in JSON format. We can inspect all the output variables# or only the specific one, if there’s any, by calling the command terraform output. To show the current required providers for the project, run terraform providers, and we can export these providers into another directory using the subcommand mirrors.

We can destroy a Terraform infrastructure with the command terraform destroy.

All configuration and execution plans, and dependencies could be visualised using the command terraform graph. Since its output is in Graphviz format, you can pipe it to the software dot as follows.

terraform graph | dot -Tsvg terraform.svg

terraform fmt could be run in order to format all HCL files in the project directory.

Terraform Cloud and Terraform Enterprise provide a Web UI that centralised all Terraform components which ease the management.

Links to this page
#devops #iac #automation