Skip to content

hanks/terraform-variables-generator

 
 

Repository files navigation

Go Report Build Status Coverage Status

terraform-variables-generator

Simple Tool to Generate Variables file from Terraform Configuration. It will find all *.tf files in current directory, and generate variables.tf file. If you already have this file, it will ask to override it.

I want to create a tool to generate variables.tf from *.tf, and found this awesome project terraform-variables-generator. So based on it, I add some enhancements that seems can not be merged back 😂

Prerequisite

  • Docker, for development
  • Linux/macOS, not test in Windows now

Enhancements

  1. Refactor dramatically for basic maintenance friendly
  2. Use dep to do dependencies management
  3. Add customized var settings, that can help to create more flexible variables.tf file
  4. Full support variable block element, like 'type', 'description', 'default'
  5. Use 'terraform fmt' api to do the final format
  6. Add Makefile to do task management
  7. Introduce docker environment for the development
  8. Add more tests to do the better coverage
  9. Update .travis.yml with docker setup

Installation

make install

and uninstall by:

make uninstall

Usage

tfvargen

It will find all *.tf files in current directory, and generate variables.tf file. If you already have this file, it will ask to override it.

and you can find help info by:

tfvargen -h

Also you can customized variable block with vars.yml to be put into the *.tf directory, like

vars:
  - public_subnets:
      type: list
      description: subnets for public
      default: |
        ["sub1", "sub2"]
  - tags:
      type: map
      default: |
        {
          Name = "Terraform"
        }
  - cidrs:
      type: list
      default: |
        ["10.0.0.0/16", "10.1.0.0/16"]
  - t1-var2:
      description: var for t1
      type: string

Demo

demo.gif

Example

resource "aws_eip" "nat" {
  vpc   = true
  count = "${length(var.public_subnets)}"
}

resource "aws_nat_gateway" "nat" {
  allocation_id = "${element(aws_eip.nat.*.id, count.index)}"
  subnet_id     = "${element(aws_subnet.public.*.id, count.index)}"
  count         = "${length(var.public_subnets)}"
  tags          = "${var.tags}"
}

data "template_file" "template1" {
  template = "${file("${path.module}/template1.tpl")}"

  vars {
    t1_var1 = "${var.cidrs}"
    t1-var2 = "${var.t1-var2}"
    t1-var3 = "${var.t1-Var3}-${var.t1-inline}"
  }
}

Will generate:

variable "cidrs" {}

variable "public_subnets" {}

variable "t1-Var3" {}

variable "t1-inline" {}

variable "t1-var2" {}

variable "tags" {}

And, if you add customized conf vars.yml:

vars:
  - public_subnets:
      type: list
      description: subnets for public
      default: |
        ["sub1", "sub2"]
  - tags:
      type: map
      default: |
        {
          Name = "Terraform"
        }
  - cidrs:
      type: list
      default: |
        ["10.0.0.0/16", "10.1.0.0/16"]
  - t1-var2:
      description: var for t1
      type: string

then run generator again, the result will be:

variable "cidrs" {
  type = "list"

  default = ["10.0.0.0/16", "10.1.0.0/16"]
}

variable "public_subnets" {
  description = "subnets for public"
  type        = "list"

  default = ["sub1", "sub2"]
}

variable "t1-Var3" {}

variable "t1-inline" {}

variable "t1-var2" {
  description = "var for t1"
  type        = "string"
}

variable "tags" {
  type = "map"

  default = {
    Name = "Terraform"
  }
}

Development

  • make test, run unit test, coverage test, static analytics
  • make run, run program to generate variables.tf in tests directory
  • make build, cross compile binaries, and put into dist/bin directory
  • make debug, use dlv to do the gdb-style debug
  • make dev, build docker image used in dev

About

Simple Tool for Generate Variables file from Terraform Configuration

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 86.2%
  • Makefile 9.0%
  • HCL 3.0%
  • Dockerfile 1.8%