Terraform CLI Workflow Improvements
As builders we spend a major chunk of our time working on infrastructure code. This is often a repetitive process, which involves jumping back and forth between files, double-checking references, verifying differences between environments and so forth.
In this scenario it is quite useful to have all involved tools optimized and sharp. A process which might take only a couple of seconds, if repeated 100s of times a day, accumulates over time and can yield a sizable portion of the workday. If optimized, even those small repetitive tasks can turn out to be decent time savers, when summarized over weeks, months, or years.
And let's be honest, improving ones own workflow is also just a lot of fun! 😁
Tip 1: Switching Environments
What I found to be doing again and again is to switch between different environments. I'm in state X in sub folder Y in the dev environment, and I need to go for the same state in prod.
This is when the dance starts of typing ../ the exactly right amount of times, and switching back to the correct sub folder in a different environment.
After doing this over and over and over again, I finally came up with a script to do it for me (gist):
A great time saver which I enjoy using. For many traditional editors there are speed-challenges to do a certain edit in the minimal amount of key-strokes, using the best combination of shortcuts. Definitely a niche-thing, but a fun concept to apply to other areas.
Tip 2: Automating terraform actions
You might also encounter use-cases to automate terraform actions across multiple states.
At a client who uses states intensively (70+ states for a small sized project) it quickly became a drag to check if all the individual states where already applied after a ticket was done. Each of the states ideally should have no pending changes and exit the terraform plan command gracefully.
For this particular customer we ended up writing a small tool to execute terraform init and plan across all terraform states. In its early stage without parallelization it still took a long time to execute (~10 minutes), but it was still a great convenience for us and our client.
Tip 3: Aliases for basic commands
Last but not least are of course aliases for the basic terraform commands:
alias tfi="terraform init" alias tfp="terraform plan" alias tfa="terraform apply" alias tff="tf fmt -diff -recursive"
Personally I hadn't set them up for a long time, but also found them to be a major time saver and fun to use. A more complete list of handy aliases can be found over here.
And that's it! We hope this blog could inspire you to take a closer look at some of the things you do on a daily basis, and how they could be made easier using CLI tricks.
Encryption of SSM session data using KMS
Encrypting session data to handle confidential data interactions
Run your code highly favorable and next to your Customer
CloudFormation Best Practices
Recommendations that can help you to use CloudFormation more effectively and securely throughout its entire workflow.