How to Enable Terraform Zsh Completion: Easy Guide

Terraform Zsh completion improves your development and devops workflow by providing intelligent suggestions and auto-completion for the Terraform commands.

Kashyap Merai / / terraform  · 3 min read

Terraform Zsh completion improves your development and devops workflow by providing intelligent suggestions and auto-completion for the Terraform commands.

Terraform Zsh Completion: Introduction

What is Terraform Zsh Completion?

Terraform is the most popular infrastructure as a code to define and provision the infrastructure using the high-level configuration language. Zsh, on the other hand, is a powerful shell with many features built-in such as Terraform Zsh completion.

Terraform Zsh completion improves your development and devops workflow by providing intelligent suggestions and auto-completion for your terminal’s Terraform commands, variables, and resources.

Why Use Zsh Completion with Terraform?

Using Zsh completion with Terraform can significantly improve your productivity workflow by:

  • Reducing Errors: Autocompletion helps prevent typos and syntax errors.

  • Speeding Up Workflow: Quickly navigate through Terraform commands and options.

  • Improve Usability: Get instant checks on valid commands and options, enhancing your command-line experience.

DevOps-Notion-Training-Board

Kickstart Your DevOps Career

Get the free Notion training board that shows you exactly what to learn, in what order, with zero guesswork.

Prerequisites for Enabling Terraform Zsh Completion

Installing Zsh

Let’s get started by installing the Zsh, Here’s how to install Zsh on different operating systems:

On Ubuntu/Debian

sudo apt update && sudo apt install zsh

On CentOS/RHEL

sudo yum install zsh

On macOS (using Homebrew)

brew install zsh

After installation, you can verify it by running:

zsh --version

Installing Oh-My-Zsh

Oh-My-Zsh is an excellent open-source framework for managing your Zsh configuration. To install Oh-My-Zsh, execute the following commands:

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

This command downloads and runs the installation script. Once installed, Oh-My-Zsh will be set to the default shell.

Setting Up Terraform Zsh Completion

Installing Terraform

First, you need to install Terraform. Follow these steps based on your operating system:

On macOS (using Homebrew)

brew tap hashicorp/tap
brew install hashicorp/tap/terraform

On Ubuntu/Debian

wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list

sudo apt update && sudo apt install terraform

On Windows

Download the Terraform binary from the official Terraform website and add it to your PATH.

Downloading Terraform Zsh Completion

Terraform provides Zsh completion:

terraform -install-autocomplete

Once the autocomplete support is installed, you will need to restart your shell or apply the changes by sourcing the .zshrc file:

touch ~/.zshrc

Free DevOps Learning Board for Absolute Beginners

11 core modules, curated content, and clear action steps - all in one interactive Notion board.

DevOps-Notion-Training-Board

Verifying Terraform Zsh Completion

Testing the Completion Setup

To verify that Zsh completion is working, start a new terminal session and type the following command:

terraform <TAB>

You should see a list of available Terraform commands and options.

If you encounter any issues, ensure the completion is correctly sourced in your .zshrc.

Tips for Optimizing Zsh Completion

Some tips to enhance your Zsh completion with Terraform:

Custom Aliases and Functions: Create custom aliases or functions for frequently used Terraform commands. Add them to your .zshrc file:

alias tf="terraform"
function tf-v() {
    terraform validate $1
}

Additional Plugins: Consider installing other Zsh plugins to enhance productivity. Popular choices include zsh-syntax-highlighting and zsh-autosuggestions.

Conclusion

By enabling Terraform Zsh completion, you streamline your development process, reduce errors, and enhance productivity. The setup is straightforward, and the benefits are immediate.

CTA image for DevOps-Notion-Training-Board

Additional Reading

Terraform Module vs Resource: The Ultimate Difference

Terraform Merge Lists: Effective Use

Terraform Where to Store Secrets: Best Practices and Solutions

Kashyap Merai

Kashyap Merai

Kashyap Merai, a Certified Solution Architect and Public Cloud Specialist with over 8 years in IT. He helped startups in Real Estate, Media Streaming, and On-Demand industries launch successful public cloud projects.

Passionate about Space, Science, and Computers. He also mentors aspiring cloud engineers, shaping the industry's future.

Connect with him on LinkedIn to stay updated on cloud innovations.

Related Posts