Skip to content

Terraform configuration manager

Notifications You must be signed in to change notification settings

wobondar/tfconfig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 

Repository files navigation

tfconfig

Terraform configuration manager

Environment variables:

CI - if true or 1, confirmation before any changes will be skipped

usage: tfconfig [<flags>] <command> [<args> ...]

Terraform configuration manager

Flags:
  -h, --help       Show context-sensitive help (also try --help-long and --help-man).
  -v, --version    Show application version.
  -c, --ci         CI flag, default 'false', if 'true' that you will not be asked before changes
  -p, --path=PATH  Terraform project path
  -V, --verbose    Verbose mode, default 'false'

Commands:
  help [<command>...]
    Show help.


  env <environment>
    Switch Terraform project environment


  dotenv [<flags>] <environment> [<dotEnvFile>]
    Generate .env file or expose configuration into env vars from Parameter Store

    -d, --decrypt  Will attempt to decrypt the parameter, default: true. use --no-decrypt to disable it
    -e, --export   Prints vars prepared for export to env via eval like 'export VAR_NAME=var_value\n'

Commands

env

Switches Terraform environment for you project by generating environment.tf

config.tf must be exists inside your project folder

aws-terraform-modules the folder should be somewhere near the project and have the structure, see example above

env example

$ tfconfig env dev
[INFO]  Path:   /Volumes/Secured/user/git/your-cool-application/terraform
[INFO]  Config: /Volumes/Secured/user/git/your-cool-application/terraform/config.tf
[INFO]  Environment:    /Volumes/Secured/user/git/your-cool-application/terraform/environment.tf
[WARNING]  Environment file 'environment.tf' exists and will be overridden
[INFO]  Looking in '/Volumes/Secured/user/git/your-cool-application/terraform/./'
[INFO]  Looking in '/Volumes/Secured/user/git/your-cool-application/terraform/../'
[INFO]  Looking in '/Volumes/Secured/user/git/your-cool-application/terraform/../../'
[INFO]  Found 'aws-terraform-modules' in '/Volumes/Secured/user/git/your-cool-application/terraform/../../'
[INFO]  Module source will be: '../../aws-terraform-modules/environment/dev/config'

After this operation configuration will be changed
Do you want to continue? [Y/n] y
[INFO]  Environment successfully switched: dev

$ cat environment.tf
######################################
##   DO NOT EDIT THIS FILE          ##
##   Generated by tfconfig          ##
######################################

module "config" {
  source = "../../aws-terraform-modules/environment/dev/config"
}

dotenv

Generate .env file or expose configuration into env vars from AWS Parameter Store via your provided .env.<environment>

Before start to working with dotenv command you should have AWS_REGION environment variable!

Inspiring by ssm-env I've got part of @remind101 code that communicates with AWS.

dotenv examples

$ export AWS_REGION=us-west-1
$ tfconfig dotenv example
DOTENV_SECURE_DB_HOST=db1.example.com DOTENV_PLAIN_DB_NAME=db_name DOTENV_SECURE_DB_PASSWORD=PaSsW0rd
$ tfconfig dotenv example -e
export DOTENV_PLAIN_DB_NAME=db_name
export DOTENV_SECURE_DB_PASSWORD=PaSsW0rd
export DOTENV_SECURE_DB_HOST=db1.example.com
$ tfconfig dotenv example .env
[INFO]  Path:   /Volumes/Secured/user/git/tfconfig/src
[INFO]  Environment:    example
[INFO]  Source dotEnv file:     .env.example
[INFO]  Destination dotEnv file:        .env
[WARNING]  dotEnv file '.env' exists and will be overridden

After this operation configuration will be changed
Do you want to continue? [Y/n] y
[INFO]  Successful.
$ env $(tfconfig dotenv example) node -e "console.log(process.env)" | grep "DOTENV_"
  DOTENV_SECURE_DB_PASSWORD: 'PaSsW0rd',
  DOTENV_SECURE_DB_HOST: 'db1.example.com',
  DOTENV_PLAIN_DB_NAME: 'db_name' }
$ eval "$(tfconfig dotenv example -e)"
$ env | grep "DOTENV_"
DOTENV_SECURE_DB_HOST=db1.example.com
DOTENV_SECURE_DB_PASSWORD=PaSsW0rd
DOTENV_PLAIN_DB_NAME=db_name

Some different use case, might be useful:

  1. Reading .env.example, getting values from AWS SSM and writes into .env.dev
  2. Reading .env.dev and then exposing vars without requesting those from AWS SSM, because a new .env.dev doesnt have values that should be requested
$ tfconfig dotenv example .env.dev -c
[INFO]  Path:   /Volumes/Secured/user/git/tfconfig/src
[INFO]  Environment:    example
[INFO]  Source dotEnv file:     .env.example
[INFO]  Destination dotEnv file:        .env.dev
[WARNING]  Confirmation has been skipped via running environment configuration
[INFO]  Successful.
$ cat .env.dev
DOTENV_PLAIN_DB_NAME="db_name"
DOTENV_SECURE_DB_HOST="db1.example.com"
DOTENV_SECURE_DB_PASSWORD="PaSsW0rd"
$ tfconfig dotenv dev -e
export DOTENV_PLAIN_DB_NAME=db_name
export DOTENV_SECURE_DB_HOST=db1.example.com
export DOTENV_SECURE_DB_PASSWORD=PaSsW0rd