Terraform

Project structure

File types

main.tf

Starting point of the IaC logic, mainly containing resources.

variables.tf

  • Defines variables used in config
  • Be referenced in main.tf as ${var.variable_name}

provider.tf

Declares the provider to be used for the configuration.

output.tf

Defines output values which show information about the resources created.

Modules

  • Start writing your configuration with a plan for modules.
  • Use local modules to organize and encapsulate your code.
  • Use the public Terraform Registry to find useful modules.

States

  • Backend

    • Instead of using version control, the best way to manage shared storage for state files is to use Terraform’s built-in support for remote backends.

    • A Terraform backend determines how Terraform loads and stores state.

    • The default backend is the local backend, which stores the state file on your local disk.

    • Caveats

      • In the case of AWS, because S3 bucket and DynamoDB table must be created beforehand, so it poses a chicken-and-egg problem. Separate bootstrap steps are required.
  • Workspace

Open Questions

  1. How to avoid provisioning duplicate resources?

Troubleshooting