Skip to content

Instantly share code, notes, and snippets.

@carbonrobot
Last active August 9, 2019 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carbonrobot/7b20c45ee57b8e72627817a5f334efa7 to your computer and use it in GitHub Desktop.
Save carbonrobot/7b20c45ee57b8e72627817a5f334efa7 to your computer and use it in GitHub Desktop.
Exports Terraform outputs into an environment file that can be sourced into shell
# Exports Terraform outputs into an environment file that can be sourced into shell
# Supports Terraform 0.12+ (also works with Terragrunt 19+)
#
# It can be useful to export tf outputs into a .env file for use with serverless, jenkins, and node
# to use with tools that directly support dotenv, remove the "export " from the prefix
#
# Steps
# 1. Gets the terraform output
# 2. Add quotes to the right hand side of equals
# 3. Remove spaces around equal signs
# 4. Add a prefix for the env variables
# 5. Output to a dotenv file that can be loaded with source command or dotenv tools
terraform output | sed 's/\(=[[:blank:]]*\)\(.*\)/\1"\2"/' | sed -r 's/\s+//g' | sed 's/^/export TF_OUTPUT_/' >> .env
./export.sh
source .env
# All TF_OUTPUTS are now available
@carbonrobot
Copy link
Author

Uppercase version

terragrunt output \
            | sed 's/^[^ ]*/\U&\E/' \
            | sed 's/\(=[[:blank:]]*\)\(.*\)/\1"\2"/' \
            | sed -r 's/\s+//g' \
            | sed 's/^/export TF_OUTPUT_/' \
            >> ../.env

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment