Skip to content

Instantly share code, notes, and snippets.

@jkmart
Created August 31, 2018 21:08
Show Gist options
  • Save jkmart/95e5b068c15a6d7f27385b1e2f5b7555 to your computer and use it in GitHub Desktop.
Save jkmart/95e5b068c15a6d7f27385b1e2f5b7555 to your computer and use it in GitHub Desktop.
Terraform module repository validation check
#!/usr/bin/env bash
command -v terraform >/dev/null 2>&1 || { echo >&2 "Requires terraform bin to be on path; Aborting."; exit 1; }
# for each of the provider directories, validate sub-directory modules
for Module in ./**/*/; do
# Check if *.tf file exists in directory, to handle cases of multiple and empty submodule directories
tf_file=$(find ${Module} -maxdepth 1 -type f -name "*.tf" | head -n1)
if [ -d "${Module}" ] && [ -a "${tf_file}" ] ; then
# Initialize any module plugins for validating
terraform init ${Module} >/dev/null
# Run validate to make sure syntax is correct
if ! terraform validate -check-variables=false ${Module} ; then
# exit if validation fails with module path as hint
echo "${Module} fails validation"
exit 1;
fi
fi
done
echo "All modules pass validation"
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment