No commit activity in last 3 years
No release in over 3 years
Will render an arbitrary erb template (passed as the first positional argument), filling variables from a passed in JSON blob, the keys will be used as variable names and the values their respective values. The purpose of this GEM is to make it easy to render complex templates in terraform in a modular and reusable way.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.14.3
~> 1.16
~> 12.3
~> 3.7
 Project Readme

terraform-template-renderer

Provides a ruby executable which will take a path to an ERB template as it's only positional argument and render that with a json blob passed in to STDIN. The keys in the json blob will be used as instance variable names to use in the template.

The result will be returned inside a json blob with a single key, the name of which is rendered.

For example if your template is

Hello <%= @my_key %>!

And you execute the command as:

echo '{ "my_key": "dear reader" }' | template_renderer path_to_my_template

You will produce

{ "rendered": "Hello dear reader!" }

The purpose for this strange behaviour is to be a terraform external provider to render arbitrarily complex templates, terraform passes in the variables to render as a json blob to stdin as described above and expects a json blob back.

Rendering Partials

Inside any template you can render a partial template by calling the render method.

For example if you have a partial template in filepath partials/partial.erb:

This is a partial, Hello <%= name %>

And your template is

My favourite colour is <%= colour %>
<%= render 'partials/partial.erb' %>

And you execute the command as

echo '{ "colour": "purple", "name": "Jonathan" }' | template_renderer path_to_my_template

You will produce

{ "rendered": "My favourite colour is purple\nThis is a partial, Hello Jonathan\n" }

ERB Template notes

Trim mode is enabled and the trim character is a hyphen -.

Examples

There are a number of examples in the examples folder. To run these examples you will need terraform 0.11, you can run the example as follows:

cd examples/simple_strings
terraform init
terraform apply

In each example there is an erb template and a terraform file, applying the terraform code will produce a file called example_output.

WARNING: Some of the examples incur AWS costs, ALWAYS remember to terraform destroy after you have finished running the example. You are solely responsible for any AWS costs incurred running these examples.