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]
- An AWS account.
- Install and configure the AWS CLI.
- Download and install Terraform (Tested against version 0.12.0).
- A bash environment (e.g. Git for Windows).
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...
terraform init
terraform apply
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"
}
}
- Kyle Kolander
This project is licensed under the MIT License - see the LICENSE file for details