Friends don't let friends commit Terraform without fmt, linting and validation
So it starts out easy, you write a bit of terraform
and all is going well then as more and more people start committing and the code is churning things start to get messy. Breaking commits block release, formatting isn’t consistent and and errors get repeated.
Seems a bit odd right, in the middle of your devops pipe which dutifully checks code passes tests and validation you just give terraform
a free pass.
The good new is terraform
has tools to help you out here and make life better!
Here is my rough script for running during build to detect and fail early on a host of terraform
errors. It’s also pinning terraform
to a set release (hopefully the same one you use when releasing to prod) and doing a terraform init
each time to make sure you have providers pinned (if not the script fail when a provider ships breaking changes and give you an early heads up).
It’s rough and ready so make sure your happy with what it does before you give it a run. For an added bonus the docker
command below the script runs it inside a Azure Devops
container to emulate locally what should happen when you push.
https://gist.github.com/lawrencegripper/d5f126279a1991eee5ed2a200234029e
Optionally you can add args
like -var java_functions_zip_file=something
to the terraform validate
call.
Hope this helps as a quick rough guide!