tfvars-annotations

command module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2019 License: MIT Imports: 20 Imported by: 0

README

Update values in terraform.tfvars using annotations

This is still WIP, full of bugs, missing core features and docs.

Terraform is awesome!

As of today, Terraform 0.11 and 0.12 support only static (known, fixed, already computed) values in tfvars files. There is no way to use Terraform interpolation functions, or data-sources inside tfvars files in Terraform to update values.

While working on modules.tf (a tool which converts visual diagrams created with Cloudcraft.co into Terraform configurations), I had a need to generate code which would chain invocations of Terraform AWS modules and pass arguments between them without requiring any extra Terraform code as a glue. Terragrunt is a great fit for this, it allows to reduce amount of Terraform configurations by reusing Terraform modules and providing arguments as values in tfvars files.

Some languages I know have concepts like annotations and decorators, so at first I made a shell script which replaced values in tfvars based on annotations and was called by Terragrunt hooks. tfvars-annotations shares the same goal and it has no external dependencies (except terraform or terragrunt).

Use cases

  1. modules.tf and Terragrunt (recommended)
  2. Terraform (not implemented yet)
  3. General example - AMI ID, AWS region

Features

  1. Supported annotations:
  • terragrunt_output:
    • @tfvars:terragrunt_output.vpc.vpc_id
    • @tfvars:terragrunt_output.security-group.this_security_group_id
  • terraform_output
  • data-sources generic
  1. Type wrapping:
  • to_list: Wrap original value with [] to make it it as a list

How to use

Run tfvars-annotations before terraform plan, apply, refresh.

It will process tfvars file in the current directory and set updated values.

E.g.:

$ tfvars-annotations examples/project1-terragrunt/eu-west-1/app
$ terraform plan

How to disable processing entirely

Put @tfvars:disable_annotations anywhere in the terraform.tfvars to not process the file.

Examples

See examples for some basics.

To-do

  1. Get values from other sources:
  • data sources generic
  • aws_account_id or aws_region data sources
  1. terragrunt_outputs from stacks:
  • in any folder
  • in current region
  1. cache values unless stack is changed/updated
  2. functions (limit(2), to_list)
  3. rewrite in go (invoke like this => update_dynamic_values_in_tfvars ${get_parent_tfvars_dir()}/${path_relative_to_include()})
  4. make it much faster, less verbose
  5. add dry-run flag
  6. Proposed syntax:
  • @tfvars:terragrunt_output.security-group_5.this_security_group_id.to_list

  • @tfvars:terragrunt_output.["eu-west-1/security-group_5"].this_security_group_id.to_list

  • @tfvars:terragrunt_output.["global/route53-zones"].zone_id

  • @tfvars:terragrunt_data.aws_region.zone_id

  • @tfvars:terragrunt_data.aws_region[{current=true}].zone_id

Bugs

  1. Add support for maps (and lists of maps). Strange bugs with rendering comments in wrong places.

How to install

go get github.com/antonbabenko/tfvars-annotations

Or download release for your platform here

go run . -debug examples/project1-terragrunt/eu-west-1/app
go run . examples/project1-terragrunt/eu-west-1/app

Authors

This project is created and maintained by Anton Babenko with the help from different contributors.

License

This work is licensed under MIT License. See LICENSE for full details.

Copyright (c) 2019 Anton Babenko

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL