Skip to content

Instantly share code, notes, and snippets.

@jordiclariana
Created March 24, 2016 14:37
Show Gist options
  • Save jordiclariana/95b17b018cd44c7e6afd to your computer and use it in GitHub Desktop.
Save jordiclariana/95b17b018cd44c7e6afd to your computer and use it in GitHub Desktop.
Sync terraform state from/to s3 bucket
#!/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