GithubHelp home page GithubHelp logo

cloudposse-archives / tfenv Goto Github PK

View Code? Open in Web Editor NEW
125.0 13.0 4.0 28 KB

Transform environment variables for use with Terraform (e.g. `HOSTNAME` ⇨ `TF_VAR_hostname`)

Home Page: https://cloudposse.com/accelerate

License: Apache License 2.0

Makefile 2.73% Go 74.05% Shell 23.22%
terraform environment-variables environments env envvars chamber tfenv tfvars 12factor 12factorapp

tfenv's Introduction

README Header

Cloud Posse

tfenv Build Status Latest Release Slack Community

Command line utility to transform environment variables for use with Terraform. (e.g. HOSTNAMETF_VAR_hostname)

It can also intelligently map environment variables to terraform command line arguments (e.g. TF_CLI_INIT_BACKEND_CONFIG_BUCKET=exampleTF_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.

Introduction

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 or AWS_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:

  1. Lowercase all envs (Terraform convention)
  2. Strip leading or trailing underscores (_)
  3. Replace consecutive underscores with a single underscore (_)
  4. 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.

History

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.

Usage

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 is TF_VAR_, but you could do something like TF_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

Direnv

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)"

Bash

Load the terraform environment into your shell.

Just add the following into your shell script:

source <(tfenv)

Terraform Args

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.

Initializing Modules

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.

Examples

Compiling the Binary

make go/build

Use with Chamber

chamber exec foobar -- tfenv terraform plan

Use with Terragrunt

tfenv terragrunt plan

Print Environment

tfenv printenv

Use with Direnv

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

Share the Love

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. =)

Related Projects

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.

References

For additional context, refer to some of these links.

Help

Got a question?

File a GitHub issue, send us an email or join our Slack Community.

README Commercial Support

Commercial Support

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.

E-Mail

  • 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.

Slack Community

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.

Newsletter

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.

Contributing

Bug Reports & Feature Requests

Please use the issue tracker to report any bugs or file feature requests.

Developing

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.

  1. Fork the repo on GitHub
  2. Clone the project to your own machine
  3. Commit changes to your own branch
  4. Push your work back up to your fork
  5. 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

Copyright © 2017-2019 Cloud Posse, LLC

License

License

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.

Trademarks

All other trademarks referenced herein are the property of their respective owners.

About

This project is maintained and funded by Cloud Posse, LLC. Like it? Please let us know by leaving a testimonial!

Cloud Posse

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.

Contributors

Erik Osterman
Erik Osterman

README Footer Beacon

tfenv's People

Contributors

ahmgithubahm avatar osterman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tfenv's Issues

syntax error near unexpected token

I am not experienced with go; so please excuse me if this is not really an issue.

Running the latest golang (1.12.7) on Windows 10 in Git Bash I get this error:

$ make release/tfenv
C:/Program Files (x86)/GnuWin32/bin/make go/build
/usr/bin/sh: -c: line 0: syntax error near unexpected token `('
/usr/bin/sh: -c: line 0: `C:/Program Files (x86)/GnuWin32/bin/make go/build'
make: *** [release/tfenv] Error 1

The documentation says I should use "make go/build" but that is not a valid make target. This probably has to do with the command "shell" not being installed with Git Bash.

Please advise on how to build on Windows 10 with Git Bash.

Installation Challenges with Ubuntu 22.04

Found a bug? Maybe our Slack Community can help.

Slack Community

Describe the Bug

I don't know that I would call it a bug, but I could not get make to build the code for the life of me.

make go/build
make: *** No rule to make target 'go/build'.  Stop.

Expected Behavior

What did work for me and was more strait forward was just using the go install process that is available in 1.17 and above.

go install github.com/cloudposse/tfenv@latest
go: downloading github.com/cloudposse/tfenv v0.0.0-20200228015332-fb2bbf69b648
go: finding module for package github.com/taskcluster/shell
go: downloading github.com/taskcluster/shell v0.0.0-20191115171910-c688067f12d3
go: found github.com/taskcluster/shell in github.com/taskcluster/shell v0.0.0-20191115171910-c688067f12d3

After doing the above I needed to make sure the gopath was added to my environment and was then able to use tfenv.

Which I pulled from here:
https://stackoverflow.com/questions/30295146/how-can-i-install-a-package-with-go-get

Environment (please complete the following information):

Anything that will help us triage the bug will help. Here are some ideas:

  • OS: [e.g. Linux, OSX, WSL, etc] Ubuntu 22.04
  • Version [e.g. 10.15]

Images broken

On masters README.md

This project is part of our comprehensive "SweetOps" approach towards DevOps. Share via Email Share on Google+ Share on Facebook Share on Reddit Share on LinkedIn Share on Twitter

It's 100% Open Source and 

I think the issue here is the markdown syntax

Backend config requires key=value even when value is "true"

When setting up command line arguments for -backend-config, this code strips out =true. So

TF_CLI_INIT_BACKEND_CONFIG_ENCRYPT=true

becomes

-backend-config=encrypt

While this is correct behavior for binary options, -backend-config requires key=value even when value is true. In this example it requires

-backend-config=encrypt=true

Easy fix is to take out the special case for true.

Add release for Apple M1 (darvin_arm64)

Please add a release for Apple M1 (darvin_arm64) so it is possible to use tfenv via asdf also on new Apple machines. Currently M1 processors are not supported or a local build is needed.

Why is it called `tfenv`?

what

  • The "env" suffix is used by some projects to indicate a "version manager", but our tfenv is not a version manager.
  • Why did we call it tfenv?

why

  • The tfenv piggybacks on env, direnv, and autoenv as a way to export the environment
  • The env command has been in Linux since the early 90s. This is at least, a decade before the first release rbenv Et al. =)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.