mbigras / terraform-turtles

Illustrate abstracting details with Terraform modules

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

terraform-turtles

Illustrate abstracting details with Terraform modules.

Summary

In this repo, I illustrate the following:

  1. Higher-level Terraform modules alleviate the burden of gritty details by applying reasonable defaults and calling lower-level modules.
  2. As the level of abstraction increases (indicated by incrementing the module number), the quantity of gritty details decrease because the lower-level modules handle those lower-lever details—alleviates burden.
  3. This workflow also illustrates the inverse—that is, as you decrease abstration, you increase the quantity of details—more arduous.

For example consider the higher-level mod3 module compared to the lower-level mod2 module:

# mod3/main.tf
variable "config" {
  type = object({
    detail3 = optional(string, "reasonable mod3 default")
  })
  default = {} # defaults are good enough--yay!
}
# [...]
module "mod2" {
  source = "../mod2"
  config = var.config
}
# [...]
# mod2/main.tf
variable "config" {
  type = object({
    detail2 = optional(string, "reasonable mod2 default")
    detail3 = string
  })
}
# [...]

Observation: Notice that you don't need to understand detail2 detail to call mod2 module because mod2 sets detail2 to reasonable mod2 default default.

Getting started

To get started, run the ordered sequence of steps.

  1. Install Terraform—see Install Terraform page.

  2. Get this code.

    git clone git@github.com:mbigras/terraform-turtles.git
    cd terraform-turtles
  3. Initialize Terraform.

    terraform init

    Your output should look like the following:

    $ terraform init
    
    Initializing the backend...
    Initializing modules...
    - mod3 in mod3
    - mod3.mod2 in mod2
    - mod3.mod2.mod1 in mod1
    
    Initializing provider plugins...
    
    Terraform has been successfully initialized!
    [...]
    

    Observation: Notice that mod3 calls mod2 which calls mod1.

  4. Automatically, plan and apply Terraform.

    terraform apply -auto-approve

    Your output should look like the following:

    $ terraform apply -auto-approve
    [...]
    Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    mod3 = {
      "config" = {
        "detail3" = "reasonable mod3 default"
      }
      "mod2" = {
        "config" = {
          "detail2" = "reasonable mod2 default"
          "detail3" = "reasonable mod3 default"
        }
        "mod1" = {
          "config" = {
            "detail1" = "reasonable mod1 default"
            "detail2" = "reasonable mod2 default"
            "detail3" = "reasonable mod3 default"
          }
        }
      }
    }
    

    Consider the following:

    1. Observation: Notice that you didn't need to understand detail1, detail2, or detail3 details to run the root Terraform module.
    2. Insight: The root module is a more abstract and less arduous root module because you don't need to set many variables because mod3, mod2, and mod1 lower-level Terraform modules each applied reasonable defaults and called further lower-level module.

About

Illustrate abstracting details with Terraform modules


Languages

Language:HCL 100.0%