claranet / jinjaform

Terraform wrapper with Jinja2 templates
MIT License
35 stars 8 forks source link

Reading ENV vars during jinja rendering #27

Closed Dyllaann closed 3 years ago

Dyllaann commented 5 years ago

Hi!

I'm using a remote backend, and I use Jinja templating to make use of variables. The remote backend is an Azure storage account, which uses a key to authenticate against.

However, as I do not want this key in version control, my thought was to set it as an environment variable using TF_VAR_xx. Unfortunately, Jinjaform does not pick up these environment variables to render the file.

Dyllaann commented 5 years ago

For the time being, I resolved this by using a custom contextfunction retrieving a given environment variable.

.jinja/context/env.py:

from jinja2 import contextfunction

@contextfunction
def env(ctx, name):
    """
    Returns the value of a given environment variable name.

    Usage: "{{ env(name) }}"
    Output: the value of the environment variable

    """

    return os.environ[name]

__all__ = ['env']

and inside my resource access_key = "{{ env('TF_VAR_remoteStateAccessKey') }}"

But it would be nice if Jinjaform would use the environment variables automaticly

raymondbutcher commented 5 years ago

Hi. This is already tracked in #5 but you've provided more detail here (plus a nice workaround).

By the way, I've made another Terraform wrapper with some inspiration and lessons learned from Jinjaform. The most obvious difference is that instead of Jinja2 templates you use plain Python files. It has full support for Terraform variables including TF_VAR_* environment variables. It requires Terraform 0.12.x though. https://github.com/raymondbutcher/pretf