A Terraform wrapper that automatically selects the correct .tfvars files based on your active
AWS profile and region.
When deploying Terraform across multiple AWS accounts, each repo typically contains tfvars files
named after AWS profiles (e.g. prod.tfvars, staging-eu-west-2.tfvars). Remembering to pass
the right -var-file flags every time is tedious and error-prone. tf does it for you.
- Reads your active AWS profile from the
AWS_VAULTenv var (local) orenvironment(CI) - Reads the region from
AWS_DEFAULT_REGIONorAWS_REGION - Finds matching tfvars files in the current directory:
<profile>.tfvars(base)<profile>-<region>.tfvars(overlay, loaded on top if it exists)
- Passes everything through to
terraformwith the correct-var-fileflags injected
TfRunner— orchestrates the tool with four execution paths:- Simple subcommands (fmt, validate, etc.) pass through directly, no AWS session needed
- init injects
-backend-configflags for bucket, key, region, lock table, and encrypt - show, output, state set
TF_DATA_DIRso terraform finds the correct providers, then pass through - plan, apply, etc. auto-run
terraform initwith backend config, then inject-var-fileflags
tf planautomatically saves a binary plan to/tmp/<profile>.tfplan(view withtf show /tmp/<profile>.tfplan). Skipped if the user passes their own-outflag.tf applyautomatically appends-auto-approve, since locally the plan has already been reviewed and in CI interactive approval is not available.
Clone this repo and add bin/ to your PATH:
git clone git@github.com:kosli-dev/tf.git ~/tools/tf
# Add to ~/.zshrc or ~/.bashrc:
export PATH="$HOME/tools/tf/bin:$PATH"Use tf wherever you would use terraform:
aws-vault exec staging -- tf plan
aws-vault exec prod -- tf applyThis repo provides reusable workflows for Terraform plan and apply in CI. They handle checkout, AWS OIDC authentication, terraform installation, formatting checks, and plan artifact uploads.
Call the plan workflow (designed to be used from a matrix job):
plan:
needs: [all-environments]
permissions:
id-token: write
contents: write
uses: kosli-dev/tf/.github/workflows/plan.yml@main
strategy:
fail-fast: false
matrix:
include: ${{ fromJSON(needs.all-environments.outputs.json) }}
name: ${{ matrix.name }}
with:
aws_region: ${{ matrix.aws_region }}
aws_role_arn: "arn:aws:iam::${{ matrix.aws_account_id }}:role/my-role"
environment: ${{ matrix.environment }}
tf_version: v1.14.6To apply instead of plan, use apply.yml:
uses: kosli-dev/tf/.github/workflows/apply.yml@mainBoth plan.yml and apply.yml accept the same inputs:
| Input | Required | Default | Description |
|---|---|---|---|
environment |
yes | AWS profile name (e.g. staging, production) |
|
aws_region |
yes | AWS region (also used as AWS_DEFAULT_REGION) |
|
aws_role_arn |
yes | IAM role ARN for OIDC authentication | |
aws_role_duration |
no | 1200 |
Role session duration in seconds |
working_directory |
no | ./ |
Directory containing Terraform config |
tf_version |
no | 1.14.6 |
Terraform version to install |
Plan (plan.yml):
- Checks out the calling repo
- Installs terraform and
tf - Runs
terraform fmt --recursive -check(fails if files need reformatting) - Configures AWS credentials via OIDC
- Runs
tf plan(auto-init, auto-selects tfvars, saves binary plan) - Runs
tf showto produce a human-readable plan - Uploads the plan as a
tfplan-<environment>artifact
Apply (apply.yml):
- Steps 1–4 as above
- Runs
tf apply(auto-init, auto-selects tfvars, auto-approves)
You can place a tf.env file in the root of your Terraform repo to set default environment
variables. The file uses KEY=value format, one per line. Comments (#) and blank lines are
ignored. Values in tf.env do not override environment variables that are already set.
Example tf.env:
AWS_DEFAULT_REGION=eu-west-1
- Python 3.11+
- make
make pipmake test