Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Don't Repeat Yourself: Building Reusable Terraf...

Don't Repeat Yourself: Building Reusable Terraform Modules

Ananda Dwi Ae

April 23, 2024
Tweet

More Decks by Ananda Dwi Ae

Other Decks in Technology

Transcript

  1. Don't Repeat Yourself: Building Reusable Terraform Modules Ananda Dwi Rahmawati

    She/Her Cloud Engineer Activate Interactive Pte Ltd @misskecupbung
  2. Key Takeaways Terraform Modules It enables you to define, package,

    and deploy standardized components across projects, promoting both code maintainability and environmental flexibility.
  3. What is Module? Modules are the main way to package

    and reuse resource configurations with Terraform. It contains collection of .tf and/or .tf.json files in a directory.
  4. Module Structure Output variables To give results back to the

    calling module so it can use them to fill in arguments elsewhere. Input variables To receive values from the calling module. Resources To specify and handle one or more infrastructure objects within the module.
  5. CODE EDITOR Module Structure (Example) $ tree complete-module/ . ├──

    README.md ├── main.tf ├── variables.tf ├── outputs.tf ├── LICENSE ├── ... ├── modules/ │ ├── nestedA/ │ │ ├── README.md │ │ ├── variables.tf │ │ ├── main.tf │ │ ├── outputs.tf │ ├── .../
  6. CODE EDITOR Root Modules Only required element. This should be

    the main entry point for the module and contain specific configurations. $ tree complete-module/ . ├── README.md ├── main.tf ├── variables.tf ├── outputs.tf ├── LICENSE ├── ... ├── modules/ │ ├── nestedA/ │ │ ├── README.md │ │ ├── variables.tf │ │ ├── main.tf │ │ ├── outputs.tf │ ├── .../
  7. CODE EDITOR $ tree complete-module/ . ├── README.md ├── main.tf

    ├── variables.tf ├── outputs.tf ├── LICENSE ├── ... ├── modules/ │ ├── nestedA/ │ │ ├── README.md │ │ ├── variables.tf │ │ ├── main.tf │ │ ├── outputs.tf │ ├── .../ README It contains description of the module and what it should be used for.
  8. CODE EDITOR LICENSE It contains the terms and conditions for

    using, modifying, and distributing the code (e.g Modules) in the repository. $ tree complete-module/ . ├── README.md ├── main.tf ├── variables.tf ├── outputs.tf ├── LICENSE ├── ... ├── modules/ │ ├── nestedA/ │ │ ├── README.md │ │ ├── variables.tf │ │ ├── main.tf │ │ ├── outputs.tf │ ├── .../
  9. CODE EDITOR $ tree complete-module/ . ├── README.md ├── main.tf

    ├── variables.tf ├── outputs.tf ├── LICENSE ├── ... ├── modules/ │ ├── nestedA/ │ │ ├── README.md │ │ ├── variables.tf │ │ ├── main.tf │ │ ├── outputs.tf │ ├── .../ main.tf, variables.tf, outputs.tf The recommended filenames for a minimal module, even if they're empty. main.tf should be the primary entrypoint.
  10. CODE EDITOR variable "image_id" { type = string description =

    "The id of the machine image (AMI) to use for the server." } output "db_password" { value = aws_db_instance.db.password description = "The password for logging in to the database." sensitive = true } variables.tf, outputs.tf All variables and outputs should have a short description explaining what it's for. This helps people know what each part of the code does.