Skip to content

Instantly share code, notes, and snippets.

@jayers99
Last active March 21, 2019 16:25
Show Gist options
  • Save jayers99/13e21d67b510bcdebaca4f3099c983b5 to your computer and use it in GitHub Desktop.
Save jayers99/13e21d67b510bcdebaca4f3099c983b5 to your computer and use it in GitHub Desktop.
good format for terraform plan
#!/usr/bin/env bash
planfile=$HOME/plans/${PWD##*/}_$(cat /dev/urandom | tr -cd 'a-f0-9' | head -c 8)
planfilepr=$planfile'PR.md'
repodir=$(git rev-parse --show-prefix)
# set the proxy if not
if [[ -z "${http_proxy}" ]]; then setproxy; fi
if [[ ! -d .terraform ]]; then terraform init; fi
terraform plan "$@" | tee $planfile
# take the color out
sed -i "s,\x1B\[[0-9;]*[a-zA-Z],,g" $planfile
# build a PR
echo '### Terraform Plan -' $repodir > $planfilepr
# change summary
echo '#### Plan Summary' >> $planfilepr
grep --color=no -P '^Plan: ' $planfile >> $planfilepr
echo '```' >> $planfilepr
grep --color=no -P '^\s*[\+\-\~\/]{1,3} (?!(create|update|destroy))' $planfile >> $planfilepr
echo '```' >> $planfilepr
# everthing but security group changes
#echo '### Everything But Security Groups' >> $planfilepr
#echo '```' >> $planfilepr
#awk '/Terraform will perform the following actions:/{flag=1}/------------------------------------------------------------------------/{flag=0}flag' $planfile | grep -vP 'aws_security_group|^\s*(egress|ingress)|Terraform\ will\ perform|Plan:' >> $planfilepr
#echo '```' >> $planfilepr
# just security group changes
#gawk -F"\"" '{if (($0 ~ /^\s*(egress|ingress)/ && $4 != $2) || $0 ~ /^\s*[\+\-\~\/]{1,3} module.vpc.aws_security_group/) print $0 }' $planfile > $planfile'SGTemp'
#if [ -s $planfile'SGTemp' ]; then
# echo '### Security Group Changes' >> $planfilepr
# echo '```' >> $planfilepr
# cat $planfile'SGTemp' >> $planfilepr
# echo '```' >> $planfilepr
# #rm $planfile'SGTemp'
#fi
# full change details
echo '#### Plan Details' >> $planfilepr
echo '```' >> $planfilepr
awk '/Terraform will perform the following actions:/{flag=1}/------------------------------------------------------------------------/{flag=0}flag' $planfile >> $planfilepr
echo '------------------------------------------------------------------------' >> $planfilepr
echo '```' >> $planfilepr
echo "plan has been copied to:"
echo $planfile
echo $planfilepr
echo ///////////////////////////////////////////////////////////////////////////////
echo ///////////////////////////////////////////////////////////////////////////////
echo ///////////////////////////////////////////////////////////////////////////////
#sed -i 'N;/^\n$/d;P;D' $planfilepr
#sed -i 'N;s/^\n```/```/;P;D' $planfilepr
cat $planfilepr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment