Introduction

Since DevOps practice is designed to improve the quality and speed of the entire SDLC process, there are many tools to work with each stage of it (or if not, we always have bash:)
In this article, we will talk about one of the most popular tools in the direction of Infrastructure as Code (IaC), namely – Terraform.
Why? IaC Basics
Infrastructure as code (IaC) is an approach to automating infrastructure deployment and changes by defining the desired state of resources and their relationships in code.
In other words, suppose we need to prepare a simple (or not) infrastructure for deploying a web application with a network, a database, the appropriate instance type, and other configurations of all the listed elements. Of course, all this can be done through the web console of the cloud provider you decide to work with (by the way, the list of such providers is described here, or you can even write your own provider), but during development, such an environment often needs to be changed, reproduced and duplicated for different environments. Therefore, it is not suitable to do it manually, as it is long, tedious, expensive, and also carries with it a high level of errors.
So, IaC avoids manual configuration and enforces consistency by well-documented code representing the desired environment state. Terraform uses its own “hcl” syntax for this.
Installation
You could use the needed flow depending on your system from the HashiCorp docs
(https://learn.hashicorp.com/tutorials/terraform/install-cli).
The Basic File Structure of Terraform Project
- *.tf – files of your terraform config
- .terraform – folder with downloaded modules and provider files (is created after init)
- terraform.tfstate – local state if you don`t use remote state (is created after init)
- .terraform.lock.hcl – file with provider versions and their hashes (is created after init)
How to use it?
After writing the configuration you want to deploy, use the basic commands below.
Main commands:
- init – to initialize working directory
- validate – validate configuration on syntax error
- plan – show changes required by the current configuration
- apply – create or update infrastructure
- destroy – destroy previously-created infrastructure
Alternative Tools
- AWS CloudFormation
- Ansible
- Chef
- Puppet