> For the complete documentation index, see [llms.txt](https://xmlking.gitbook.io/macos-setup/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xmlking.gitbook.io/macos-setup/devops/terraform.md).

# Terraform

[Terraform](https://www.terraform.io) and [Terragrunt](https://terragrunt.gruntwork.io) are Infrastructure as Code (IaC) tools.

## Install

## Usage

## Style Guide

Coding style Guide

### Overview

A typical infrastructure of a product can be categorized into the following three categories :

* *Global infra:* This infrastructure contains the components which acts as the foundation of the overall infra across the product. The infra at this layer should mostly consist of setting up the networking — vpc ,subnets ,acl etc , dns and so on.
* *Common Infra:* This layer consists of the infra which is needed by more than one service. Examples would be Redis Cluster , GKE Cluster etc.
* *Service Infra:* This layer consists of infra which is service specific and should reside alongside the code repository of the service itself. Example: rds for a service would reside alongside the code of the service.

### Project Layout

All the terraform code written should follow a consistent code structure. Below can be an example:

![terraform-code-structure](https://345815687-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9XSdc4ZLxsryuoSbTpFL%2Fuploads%2Fgit-blob-48b4dad40c0fcb965ea6595b67317784e409a917%2Fterraform-code-structure.png?alt=media)

* *modules :* This folder should contain terraform code for resource creation . Examples : vpc,rds,subnets etc. Creating modules promotes reusability, hence reducing code duplication. Also each of the sub-folders/resources in modules should contain a structure like : main.tf , variables.tf , output.tf
* *policy :* This folder should contain policy documents such as IAM role policies as json files .So this folder should act as a collection of policies which would be used by the modules folder. Examples : rds\_iam\_role\_polcy.json , etc.
* *scripts :* The folder should contain any scripts such as shells scripts or python scripts used for any resource handling or creation . Hence it acts a common place of any kind of script we write for our infra.
* *templates :* In terraform we can use “.tpl” files for various purpose , in such a case we can keep those files in this folder for clarity.
* *main.tf :* This file acts the entry point when we call terraform commands like init, validate , plan ,deploy and destroy.
* *output.tf :* The outputs which need to be written to the state should be present here.
* *variables.tf :* The variables used should be present in this file.
* *{env}-{region}.tfvars :* Examples of such files are :
  * For dev and region — eu-central-1 , the expected file name should be : dev-eu-central-1.tfvars .
  * For int and region — eu-central-1 , the expected file name should be : int-eu-central-1.tfvars and so on.\
    The files contain initialized values for the variables declared in variables.tf file.

## References

* [Infrastructure as a code best practices : Terraform](https://medium.com/@ranjana-jha/infrastructure-as-a-code-best-practices-terraform-d7ae4291d621)
