Intro to terraform
Tags: terraform, devops
Published: 2023-08-27 09:02:35

Hey Reader- 
Just started learning terraform and this articles serves to explain what I have learnt. If you have any thoughts or feel that I may be wrong, do let me know. My plan for this is to make this a series of articles that can explain the tool from my POV. 

Cheers
A

---

Contents
1. Intro to terraform
2. [Terraform pt-2](https://dev.to/albertjokelin/terraform-pt-2-2f7p)

---

# What is terraform? 
Terraform is HashiCorp's Infrastructure-as-a-Code (IaaC) Service that allows you to automate provision resources on cloud providers like AWS, GCP and Azure using human-readable configuration files. 

IaaCs allow you to automatically provision resources through code in contrast to earlier methods where DevOps teams had to manually provision resources in order to scale up/down systems. 

This is awful and reserves unnecessary resources especially if you want to use them for a certain amount of time. Terrafrom on the other hand, observes the initial or current state and the final state (written in the config file) and figures out how to get to that state i.e. it is **declarative** in nature.  

# What is it used for? 
Terraform can be used: 
1. To manage any infrastructure- From AWS, GCP and Azure to personal servers, Terraform can manage almost any infrastructure you throw at it. 

2. To track your infrastructure- Terraform plans changes to be made to your infrastructure and saves it as state file for you to review it. Terraform uses the state file to determine the changes to be made. 

3. To automate changes- The declarative nature of Terraform's state files enables it to automatically figure out what needs to be provisioned, how and where and does it with minimal involvement from the developer.

4. Standardize configurations- Terraform supports reusable components known as **modules** which allows users to have standardized configuration code across the board. 

5. Collaborate- State file can be deployed to a Version Control System (VCS) and upload it to Terraform Cloud or any cloud (AWS S3 + DynamoDB for instance) where anyone on the team can easily make changes and deploy. 

It is often used in combination with different tools depending on the usecase:
1) Provisioning (Terraform) + Configuration Management (Ansible- AWS)
2) Provisioning (Terraform) + Server templating (Packer- Hashicorp) 
3) Provisioning (Terraform) + Orchestration (Kubernetes) 

# How does it work? 
In a nutshell, Terraform architecture can be divided into three main components:


![Terraform architecture, developer.hashicorp.com](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qynrj96hkb6y6rnuv2yf.png)

Terraform creates and manages resources on cloud platforms and other services through their application programming interfaces (APIs). Providers enable Terraform to work with virtually any platform or service with an accessible API.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20zjwqe92ifbqqxq4eso.png)

We start off by defining the infrastructure we need in the configuration file. And then run `terraform plan` on it. 

Plan returns the changes that will be made to the infrastruture. If satisified, run `terraform apply` to apply the changes.