Created
March 24, 2016 14:37
-
-
Save jordiclariana/95b17b018cd44c7e6afd to your computer and use it in GitHub Desktop.
Sync terraform state from/to s3 bucket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
TERRAFORM_DIR="TERRAFORM_DIRECTORY" | |
find ${TERRAFORM_DIR} -name "*.tfstate" | while read lfile; do | |
rfile="$(jq '.remote.config | select (.key != null) | "s3://" + .bucket + "/" + .key' ${lfile} -r)" | |
if [ -n "${rfile}" ]; then | |
if ! diff -q ${lfile} <(s3cmd --no-progress get ${rfile} -) > /dev/null; then | |
echo "${lfile} differs, syncing ..." | |
cd "$(dirname "${lfile}")/.." | |
terraform remote pull && \ | |
terraform remote push | |
else | |
echo "${lfile} is synced" | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment