Command line utility to transform environment variables for use with Terraform.
(e.g. HOSTNAME
→ TF_VAR_hostname
)
It can also intelligently map environment variables to terraform command line arguments (e.g. TF_CLI_INIT_BACKEND_CONFIG_BUCKET=example
→ TF_CLI_ARGS_init=-backend-config=bucket=example
).
NOTE: tfenv
is not a terraform version manager. It strictly manages environment variables much like env
or direnv
.
This project is part of our comprehensive "SweetOps" approach towards DevOps.
It's 100% Open Source and licensed under the APACHE2.
If you answer "yes" to any of these questions, then look no further!
- Have you ever wished you could easily pass environment variables to terraform without adding the
TF_VAR_
prefix? - Do you use
chamber
and get annoyed when it transforms environment variables to uppercase? - Would you like to use common environment variables names with terraform? (e.g.
USER
orAWS_REGION
) - Is there some argument to
terraform init
you want to specify with an environment variable? (e.g. a-backend-config
property)
Yes? Great! Then this utility is for you.
The tfenv
utility will perform the following transformations:
- Lowercase all envs (Terraform convention)
- Strip leading or trailing underscores (
_
) - Replace consecutive underscores with a single underscore (
_
) - Prepend prefix (
TF_VAR_
)
NOTE: tfenv
will preserve the existing environment and add the new environment variables with TF_VAR_
. This is because some terraform providers expect non-TF_VAR_*
prefixed environment variables. Additionally, when using the local-exec
provisioner, it's convenient to use regular environment variables. See our terraform-null-smtp-mail
module for an example of using this pattern.
But wait, there's more!
With tfenv
we can surgically assign a value to any terraform argument using per-argument environment variables.
Why is this project called tfenv
?
This tfenv
project borrows its naming convention from popular tools like env
, direnv
, and autoenv
. These tools provide various ways to export variables in the environment.
The env
command has been around since the early 90s, while environment variables were first conceived of in 1979. On the other hand there are tools like rbenv
Et al., which are "version managers" that were conceived of sometime around 2010.
NOTE: The utility supports a number of configuration settings which can be passed via environment variables.
TFENV_PREFIX
- Prefix used for all normalized environment variables (default isTF_VAR_
, but you could do something likeTF_VAR_app_
)TFENV_WHITELIST
- Whitelist of allowed environment variables. Processed after blacklist. Regular expression should match wanted environment variables (by default.*
)TFENV_BLACKLIST
- Blacklist of excluded environment variables. Processed before whitelist. Regular expression should exclude unwanted/dangerous environment variables (e.g. AWS credentials)
The basic usage looks like this. We're going to run some command
and pass it arg1
... argN
:
tfenv command arg1 arg2 arg3 ... argN
So for example, we can pass our current environment to terraform by simply running:
tfenv terraform plan
You can use tfenv
with direnv
very easily. Running tfenv
without any arguments will emit export
statements.
Example .envrc
:
# Export terraform environment
source <(tfenv)
or...
# Export terraform environment
eval "$(tfenv)"
Load the terraform environment into your shell.
Just add the following into your shell script:
source <(tfenv)
With tfenv
we can populate the TF_CLI_ARGS
and TF_CLI_ARGS_*
environment variables automatically. This makes it easy to toggle settings.
For example, if you want to pass -backend-config=bucket=terraform-state-bucket
to terraform init
, then you would do the following:
export TF_CLI_INIT_BACKEND_CONFIG_BUCKET=terraform-state-bucket
Running tfenv
will populate the TF_CLI_ARGS_init=-backend-config=bucket=terraform-state-bucket
Multiple arguments can be specified and they will be properly concatenated.
Terraform has the built-in capability to initialize "root modules" from a remote sources by passing the -from-module
argument to terraform init
.
We can turn this into a 12-factor style invocation using tfenv
.
export TF_CLI_INIT_FROM_MODULE=git::https://github.com/cloudposse/terraform-root-modules.git//aws/tfstate-backend?ref=tags/x.y.z
source <(tfenv)
terraform init
Learn more about TF_CLI_ARGS
and TF_CLI_ARGS_*
in the official documentation.
make go/build
chamber exec foobar -- tfenv terraform plan
tfenv terragrunt plan
tfenv printenv
You can easily integrate tfenv
with direnv
so that your environment is automatically setup for Terraform.
Add the following to your .envrc
:
source <(tfenv)
Example Output
direnv: loading .envrc
direnv: export +TF_VAR_aws_profile +TF_VAR_aws_vault_backend +TF_VAR_colorterm +TF_VAR_dbus_session_bus_address +TF_VAR_desktop_session +TF_VAR_direnv_diff +TF_VAR_direnv_in_envrc +TF_VAR_direnv_watches +TF_VAR_display +TF_VAR_fzf_orig_completion_git +TF_VAR_github_token +TF_VAR_gtk_overlay_scrolling +TF_VAR_histcontrol +TF_VAR_histsize +TF_VAR_home +TF_VAR_hostname +TF_VAR_lang +TF_VAR_lessopen +TF_VAR_logname +TF_VAR_ls_colors +TF_VAR_mail +TF_VAR_mate_desktop_session_id +TF_VAR_okta_user +TF_VAR_oldpwd +TF_VAR_path +TF_VAR_pwd +TF_VAR_qt_auto_screen_scale_factor +TF_VAR_qt_scale_factor +TF_VAR_session_manager +TF_VAR_sessiontype +TF_VAR_shell +TF_VAR_shlvl +TF_VAR_ssh_auth_sock +TF_VAR_term +TF_VAR_user +TF_VAR_vte_version +TF_VAR_windowid +TF_VAR_xauthority +TF_VAR_xdg_current_desktop +TF_VAR_xdg_runtime_dir +TF_VAR_xdg_seat +TF_VAR_xdg_session_id +TF_VAR_xdg_vtnr
Like this project? Please give it a ★ on our GitHub! (it helps us a lot)
Are you using this project or any of our other projects? Consider leaving a testimonial. =)
Check out these related projects.
- Packages - Cloud Posse installer and distribution of native apps
- build-harness - Collection of Makefiles to facilitate building Golang projects, Dockerfiles, Helm charts, and more
- geodesic - Geodesic is the fastest way to get up and running with a rock solid, production grade cloud platform built on strictly Open Source tools.
- direnv - Unclutter your .profile with an environment switcher for the shell
- env - Used to either print a list of environment variables or run another utility in an altered environment without having to modify the currently existing environment.
For additional context, refer to some of these links.
- Terraform Without Wrappers is AWESOME! - Reddit discussion regarding this project.
Got a question?
File a GitHub issue, send us an email or join our Slack Community.
Work directly with our team of DevOps experts via email, slack, and video conferencing.
We provide commercial support for all of our Open Source projects. As a Dedicated Support customer, you have access to our team of subject matter experts at a fraction of the cost of a full-time engineer.
- Questions. We'll use a Shared Slack channel between your team and ours.
- Troubleshooting. We'll help you triage why things aren't working.
- Code Reviews. We'll review your Pull Requests and provide constructive feedback.
- Bug Fixes. We'll rapidly work to fix any bugs in our projects.
- Build New Terraform Modules. We'll develop original modules to provision infrastructure.
- Cloud Architecture. We'll assist with your cloud strategy and design.
- Implementation. We'll provide hands-on support to implement our reference architectures.
Join our Open Source Community on Slack. It's FREE for everyone! Our "SweetOps" community is where you get to talk with others who share a similar vision for how to rollout and manage infrastructure. This is the best place to talk shop, ask questions, solicit feedback, and work together as a community to build totally sweet infrastructure.
Signup for our newsletter that covers everything on our technology radar. Receive updates on what we're up to on GitHub as well as awesome new projects we discover.
Please use the issue tracker to report any bugs or file feature requests.
If you are interested in being a contributor and want to get involved in developing this project or help out with our other projects, we would love to hear from you! Shoot us an email.
In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow.
- Fork the repo on GitHub
- Clone the project to your own machine
- Commit changes to your own branch
- Push your work back up to your fork
- Submit a Pull Request so that we can review your changes
NOTE: Be sure to merge the latest changes from "upstream" before making a pull request!
Copyright © 2017-2019 Cloud Posse, LLC
See LICENSE for full details.
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
All other trademarks referenced herein are the property of their respective owners.
This project is maintained and funded by Cloud Posse, LLC. Like it? Please let us know by leaving a testimonial!
We're a DevOps Professional Services company based in Los Angeles, CA. We ❤️ Open Source Software.
We offer paid support on all of our projects.
Check out our other projects, follow us on twitter, apply for a job, or hire us to help with your cloud strategy and implementation.
Erik Osterman |
---|