Terraform

Terraform and Terragrunt 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:

  • 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

Last updated