Skip to content

Latest commit

 

History

History
80 lines (66 loc) · 2.45 KB

README.md

File metadata and controls

80 lines (66 loc) · 2.45 KB

tfgen

Terraform preprocessor that allows for easily switching between AWS regions and profiles, and provides a work-around for the lack of interpolation in backend configuration.

Out-of-the-box, tfgen will replace the following placeholders, but one could easily add others as needed:

- [region]
- [profile]
- [app]
- [accountID]

Prerequisites

Run tfgen for preprocessing

A corresponding .tf file will be generated for each .tfgen file in the module. Any placeholder text will be replaced accordingly. Do not modify the .tf files, as they will be overwritten the next time you run tfgen.

./tfgen.sh us-east-2

Running tfgen without any arguments will print out the usage and clean up your module directory. Note that the app argument defaults the name of the module directory.

$ ./tfgen.sh

Usage: ./tfgen.sh region [profile] [app]

  region      AWS Region.
  profile     AWS Profile in ~/.aws/credentials.    Default:  default
  app         Application name.                     Default:  tfgen

Cleaning up...

Run Terraform

terraform init
terraform apply

Example of Terraform backend configuration for S3

This isn't possible with Terraform alone. Using the same module across different regions and accounts requires copy/paste or manual and error-prone editing.

./tfgen.sh us-east-2 kyle

.tfgen

terraform {
  backend "s3" {
    bucket         = "terraform-state-[accountID]-[region]"
    key            = "[app].tfstate"
    region         = "[region]"
    profile        = "[profile]"
    dynamodb_table = "terraform-state-lock-[accountID]-[region]"
  }
}

.tf

terraform {
  backend "s3" {
    bucket         = "terraform-state-redacted-us-east-2"
    key            = "tfgen.tfstate"
    region         = "us-east-2"
    profile        = "kyle"
    dynamodb_table = "terraform-state-lock-redacted-us-east-2"
  }
}

Authors

  • Kyle Kolander

License

This project is licensed under the MIT License - see the LICENSE file for details