Terraform for Beginners How and Why?

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

Read also

Basics of Containerization Docker Engine

Docker Engine is open-source software, the most popular container management platform, and considering that containerization in the field of software development is now an indispensable thing, this tool is really important and useful for every engineer who implements elements of DevOps methodology. Why? Let's consider this further.

GitOps Approach and Best Practices

Моє знайомство з GitOps почалося не так давно. До цього, працюючи з Kubernetes кластером та Kubernetes в цілому, ми використовували CI для доставки коду. Користувалися різними інструментами, на кшталт helm чи kubectl, і постійно стикалися з певними проблемами, які виникали через невідповідність стейту на самому кластері. З часом ми перейшли на GitOps і вже успішно використали цей підхід на кількох проєктах. У цій статті розглянемо суть та принципи GitOps, поговоримо про безпеку Pipelines та їхню реалізацію.