Skip to content

Instantly share code, notes, and snippets.

@chagui
Created June 12, 2023 14:17
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 chagui/ae17d23f9a622e1cb1d25187f7ffcf63 to your computer and use it in GitHub Desktop.
Save chagui/ae17d23f9a622e1cb1d25187f7ffcf63 to your computer and use it in GitHub Desktop.
For all direct sub-directory find if there is a the lock on the terraform state and remove it
#!/usr/bin/env bash
function unlock_module {
pushd "$1"
local -r lock_id="$(terraform refresh 2>&1 | rg ID | awk -F ' ' '{print $4}')"
if [[ -n $lock_id ]]; then
echo "lock: $lock_id"
terraform force-unlock -force "$lock_id"
else
echo "no lock"
fi
popd
}
export -f unlock_module
find . -mindepth 1 -maxdepth 1 -type d -print | parallel -j 8 unlock_module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment