Terraform developer tool to extract variables and generate variables.tf files.
Project description
Terraform module development tool.
- Extract variables from
main.tfand generate avariables.tffile - Find missing variables in
variables.tfandmain.tfbased on each other - Generate a module use stub from a
variables.tffile - Generate a .env file with variables from
main.tf - Delete extra scratchrelaxtv files
install
pip install scratchrelaxtv
tip
Once installed, you can just type "relaxtv."
workflows
Here are two example workflows using scratchrelaxtv.
Original module development:
- Write
main.tfwith whatever variables you need - Run scratchrelaxtv to generate
variables.tf - Fill in descriptions, defaults, etc. in
variables.tf - Run
terraform fmtto prettify everything
Cleanup module:
- Run scratchrelaxtv in folder with
main.tfandvariables.tfto find missing variables - Using
-cfoption, automatically add missing vars tovariables.tf - Fill in descriptions, defaults, etc. in
variables.tffor newly added vars - Run
terraform fmtto prettify everything
examples
example: generate variables.tf
By default, scratchrelaxtv looks for main.tf and will generate a variables.tf file. Variables will be in the same order in variables.tf as they were in main.tf. There are options to sort variables. You can --force to overwrite an existing variables.tf file. Otherwise, scratchrelaxtv will generate new variables.tf files with each run: variables.1.tf, variables.2.tf and so on.
Assume this main.tf:
resource "aws_s3_bucket" "this" {
count = "${var.create_bucket ? 1 : 0}"
bucket = "${var.bucket}"
region = "${var.region}"
}
Run scratchrelaxtv:
$ scratchrelaxtv
2019-04-26 08:02:54,011 - INFO - generating variables file
2019-04-26 08:02:54,011 - INFO - input file: main.tf
2019-04-26 08:02:54,011 - INFO - output file: variables.tf
2019-04-26 08:02:54,011 - INFO - not forcing overwrite of output file
2019-04-26 08:02:54,011 - INFO - not ordering output file
The generated variables.tf:
variable "create_bucket" {
description = ""
type = "string"
default = ""
}
variable "bucket" {
description = ""
type = "string"
default = ""
}
variable "region" {
description = ""
type = "string"
default = ""
}
example: find and fix missing variables
Assume you already have a main.tf and a variables.tf. In this example, the variables.tf is missing the region variable.
main.tf:
resource "aws_s3_bucket" "this" {
bucket = "${var.bucket}"
region = "${var.region}"
}
variables.tf:
variable "bucket" {
description = "The bucket where the stuff will be stored"
type = "string"
default = ""
}
Run scratchrelaxtv to automatically add any missing variables:
$ scratchrelaxtv -cf
2019-04-26 08:21:27,289 - INFO - checking for missing variables
2019-04-26 08:21:27,289 - INFO - input file: main.tf
2019-04-26 08:21:27,289 - INFO - output file: variables.tf
2019-04-26 08:21:27,289 - INFO - forcing overwrite of output file
2019-04-26 08:21:27,289 - INFO - not ordering output file
2019-04-26 08:21:27,290 - WARNING - input file main.tf is missing variables:
region
Now, the variables.tf looks like this:
variable "bucket" {
description = "The bucket where the stuff will be stored"
type = "string"
default = ""
}
variable "region" {
description = ""
type = "string"
default = ""
}
example: generate a stub for using the module
By default, when generating a stub, scratchrelaxtv looks for variables.tf.
Assume this variables.tf:
variable "id" {
description = "The ID of the resource"
type = "string"
default = ""
}
variable "bucket" {
description = "The bucket where the stuff will be stored"
type = "string"
default = ""
}
variable "region" {
description = "The AWS region where the bucket lives"
type = "string"
default = ""
}
Run scratchrelaxtv with the module stub option:
$ scratchrelaxtv -m
2019-04-26 08:09:27,147 - INFO - generating module usage stub
2019-04-26 08:09:27,147 - INFO - input file: variables.tf
2019-04-26 08:09:27,147 - INFO - output file: modstub.tf
2019-04-26 08:09:27,147 - INFO - not forcing overwrite of output file
2019-04-26 08:09:27,147 - INFO - not ordering output file
The generated modstub.tf:
module "tests2" {
source = "../tests2"
providers = {
aws = "aws"
}
id = "${local.id}"
bucket = "${local.bucket}"
region = "${local.region}"
}
example: generate a .env (dotenv) file
By default, when generating a .env file, scratchrelaxtv looks for variables.tf.
Assume this variables.tf:
resource "aws_s3_bucket" "this" {
bucket = "${var.bucket}"
region = "${var.region}"
}
Run scratchrelaxtv with the generate .env and sort-ascending options:
$ scratchrelaxtv -ea
2019-06-21 20:01:35,362 - INFO - generating .env file
2019-06-21 20:01:35,362 - INFO - input file: main.tf
2019-06-21 20:01:35,362 - INFO - output file: .env
2019-06-21 20:01:35,362 - INFO - not forcing overwrite of output file
2019-06-21 20:01:35,362 - INFO - ordering output file ascending
The generated .env:
unset "${!TF_VAR_@}"
TF_VAR_bucket=replace
TF_VAR_region=replace
example: remove files
$ scratchrelaxtv -r
scratchrelaxtv can also tidy up your directories by removing its own extra generated files. Presumably it will only remove files you no longer need but be careful. This chart shows examples of what would be deleted or not.
NOTE: scratchrelaxtv removes files in the current directory and subdirectories.
| Filename | Deleted? |
|---|---|
| variables.tf | no |
| modstub.tf | yes |
| modstub.1.tf | yes |
| variables.1.tf | yes |
| xyz.abc | no |
| variables.a.tf | no |
| variables.43.tf | yes |
| modstub | no |
| modstub..tf | no |
help
scratchrelaxtv includes help:
$ scratchrelaxtv --help
usage: scratchrelaxtv [-h] [-i INPUT] [-o OUTPUT] [-f] [-m] [-n MODNAME] [-r]
[-c] [-e] [-a | -d]
optional arguments:
-h, --help show this help message and exit
-i INPUT, --input INPUT
file to extract vars from
-o OUTPUT, --output OUTPUT
file to write extracted vars to
-f, --force overwrite existing out file
-m, --modstub generate module usage stub
-n MODNAME, --modname MODNAME
name to use in module stub
-r, --remove remove all modstub.tf, variables.#.tf files
-c, --check check that all vars are listed
-e, --env generate .env with Terraform vars
-a, --asc sort output variables in ascending order
-d, --desc sort output variables in descending order
CHANGE LOG
0.4.0 - 2019.06.21
- Add feature to generate dot-env (
.env) file
0.3.0 - 2019.04.25
- Add functionality to check existing
main.tfandvariables.tffor missing variables
0.2.1 - 2019.04.25
- Fix inconsequential error thrown when removing files
0.2.0 - 2019.04.25
- Add capability to delete extra working files
0.1.3 - 2019.04.17
- Add module stub capability
0.1.2 - 2019.04.17
- Add simple docs
0.1.1 - 2019.04.16
- Initial working version
0.1.0 - 2019.04.15
- Initial release!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file scratchrelaxtv-0.4.0.tar.gz.
File metadata
- Download URL: scratchrelaxtv-0.4.0.tar.gz
- Upload date:
- Size: 26.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
16e3fd786273f763d85b8237d0e1e28ad69975447ed2bbd157d8f1f4e4973fce
|
|
| MD5 |
a4f189738fcdf8e9da00fe85b5ff617f
|
|
| BLAKE2b-256 |
fbf867fc4249bbd4e82bc271dd89989f4794e438bcd539777487c74a47b947e5
|
File details
Details for the file scratchrelaxtv-0.4.0-py2.py3-none-any.whl.
File metadata
- Download URL: scratchrelaxtv-0.4.0-py2.py3-none-any.whl
- Upload date:
- Size: 20.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a7d519d1eef6d18384ec2177f20b957c060b52c64f3b36aa06161600c6f894cb
|
|
| MD5 |
e8dbf823efcd7f3682e5ed2e0f1fa2c6
|
|
| BLAKE2b-256 |
1beb59c91394e2906efb1353db98020b2df48e7927b88b90f5ec065f2de8250f
|