Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky RichardBronosky/README.md
Last active Sep 5, 2019

Embed
What would you like to do?
Aliases for Terragrunt/Terraform to simplify using Terraform modules separately from terraform.tfvars

Installation

  1. Add aliases file to ~/.terraform.d
mkdir -p ~/.terraform.d

## Either: (a) Use a snapshot/copy of the current aliases
curl -sLo ~/.terraform.d/aliases https://gist.github.com/RichardBronosky/ae0e564f37f97f12586d6c828b8fa8ed/raw/48cfc248e74118a1c155fc321967d81c5fe70772/aliases

## Or: (b) Clone the aliases and symlink to it so you can git-pull updates
# cd ~/src # or where ever you keep your code
git clone https://gist.github.com/ae0e564f37f97f12586d6c828b8fa8ed terraform_aliases
ln -s $PWD/terraform_aliases/aliases ~/.terraform.d/
  1. Source aliases from ~/.bash_profile
cat <<EOF >>~/.bash_profile

# From: https://gist.githubusercontent.com/RichardBronosky/ae0e564f37f97f12586d6c828b8fa8ed
[[ -f ~/.terraform.d/aliases ]] && source ~/.terraform.d/aliases
EOF
  1. Relaunch your terminal to reload your profile (or however you prefer to do it).
#!/bin/bash
: << 'Usage'
# From ~/src/infrastructure-live/prod/us-east-1/prod/services/static_site
cd $(tgs)
# New location is ~/src/infrastructure-modules/services/static_site
# From ~/src/infrastructure-live/prod/us-east-1/prod/services/static_site
tgps
# Same as terragrunt plan --terragrunt-source ~/src/infrastructure-modules/services//static_site
tgas
# Same as terragrunt apply --terragrunt-source ~/src/infrastructure-modules/services//static_site
Usage
# TerraGrunt Clear cache
tgc(){ [[ -d .terragrunt-cache ]] && ( read -n1 -p "Clear cache? [y/N] "; [[ $REPLY =~ [yY] ]] && rm -rf .terragrunt-cache; [[ -n $REPLY ]] && echo; ); }
# TerraGrunt Apply
tg(){ ( [[ -n $DEBUG ]] && set -x; tgc; terragrunt "$@"; ) }
# TerraGrunt Apply
tga(){ tg apply "$@"; }
# TerraGrunt Plan
tgp(){ tg plan "$@"; }
# TerraGrunt get Source
tgs(){
local modules_dir="$(pwd | sed 's!/infrastructure-live/.*!/infrastructure-modules!')"
local source_repo_path="$(<terraform.tfvars sed -E '/source.?=.*github.com/!d; s!.*//!!; s!(\?.*)?".*!!; s!(/[^/]*)$!/\1!')"
echo "$modules_dir/$source_repo_path"
}
# TerraGrunt terragrunt-Source Source
tgss(){ echo --terragrunt-source "$(tgs)"; }
# TerraGrunt Apply Source
tgas(){ tga $(tgss) "$@"; }
# TerraGrunt Plan Source
tgps(){ tgp $(tgss) "$@"; }
# TerraGrunt Root - piped a path, returns the root of either infrastructure-live or infrastructure-modules
tgr(){ _tgpopwd | sed -E 's?(/infrastructure-(live|modules))/.*?\1?'; }
# TerraGrunt Remove Root - piped a path, returns the path relative to the infrastructure-live or infrastructure-modules repo
tgrr(){ _tgpopwd | read; echo ${REPLY#$(tgr<<<"$REPLY")}; }
# TerraGrunt Other Root - piped a path, returns the root of the other repo
tgor(){ tgr | gsed 's/-live/-modules/; t; s/-modules/-live/'; }
# TerraGrunt Last Modified File - run from as subdirectory of either infrastructure-live or infrastructure-modules, returns the "Last Modified File"
tglmf(){
[[ -n $DEBUG ]] && set -x;
find $(pwd | tgr) \
-type f \
-not -path '*/.terragrunt-cache/*' \
-not -path '*/.git/*' \
-exec \
stat -f '%m %N' {} \; | \
sort -nr | \
awk 'NR==1 {print $2}'
}
# TerraGrunt Last modified Change Directory - run from as subdirectory of either infrastructure-live or infrastructure-modules, changes to the directory of the LMF
tglcd(){
cd $(dirname $(tglmf))
}
# TerraGrunt Other Change Directory - run from as subdirectory of either infrastructure-live or infrastructure-modules, changes to the directory of the "other" (live or modules)
tgocd(){
[[ -n $DEBUG ]] && set -x;
if [[ $(basename $(pwd|tgr)) == 'infrastructure-modules' ]]; then
echo 'This feature (i-mod to i-live) is in Beta!'
local rel=${PWD#$(pwd|tgr)}
local env
case $ASSUMED_ROLE in
shared)
env='shared-services'
;;
*)
env=$ASSUMED_ROLE
;;
esac
echo "ASSUMED_ROLE is '$ASSUMED_ROLE'. 'Assuming' you meant i-live/$env ;-)"
cd $(dirname $(ls -t $(grep -rHIl "$(pwd|tgrr)" "$(pwd|tgor)/$env") | grep -v ".terragrunt-cache" | head -n1))
else
cd $(tgs)
fi
}
_tgpopwd(){
[[ -p /dev/stdin ]] && cat || pwd
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.