DEV Community

loading...
Cover image for Terraform CLI shortcuts

Terraform CLI shortcuts

Serhii Vasylenko
Hi there! 👋 I am an engineer from Ukraine. I like astronomy and everything related to DevOps. I thrive on developing great product offerings, great people, and great teams.
Originally published at serhii.vasylenko.info ・2 min read

I want to share some CLI shortcuts I use day-to-day to simplify and speed-up my Terraform workflow.
This works for any bash-compatible interpreter.

In order to use any of described aliases of functions, you need to place it in your ~/.bashrc or ~/.zshrc file (or any other configuration file you have for your shell).
Then just source this file, for example: source ~/.zshrc

Function: list outputs and variables of given module

You need to provide the path to module directory and this function will list all declared variables and outputs module has. Comes very useful when you don't remember them all and just need to take a quick look.

# TerraForm MOdule Explained
function tfmoe {
  echo -e "\nOutputs:"
  grep -r "output \".*\"" $1 |awk '{print "\t",$2}' |tr -d '"'
  echo -e "\nVariables:"
  grep -r "variable \".*\"" $1 |awk '{print "\t",$2}' |tr -d '"'
}
Enter fullscreen mode Exit fullscreen mode

Example usage:

user@localhost $: tfmoe ./module_alb

Outputs:
     alb_arn

Variables:
     acm_certificate_arn
     lb_name
     alb_sg_list
     subnets_id_list
     tags
Enter fullscreen mode Exit fullscreen mode

Function: pre-fill module directory with configuration files

You need to provide a path to the module directory and this function will create a bunch of empty 'default' .tf files in it.

#TerraForm MOdule Initialize
function tfmoi {
  touch $1/variables.tf
  touch $1/outputs.tf
  touch $1/versions.tf
  touch $1/main.tf
}
Enter fullscreen mode Exit fullscreen mode

Example usage:

user@localhost $: mkdir ./module_foo && tfmoi $_

user@localhost $: ls ./module_foo
main.tf      outputs.tf   variables.tf versions.tf

Enter fullscreen mode Exit fullscreen mode

Aliases

The purpose of these aliases is just to keep you from typing long commands when you want to do a simple action.

alias tf='terraform'
Enter fullscreen mode Exit fullscreen mode
alias tfv='terraform validate'
Enter fullscreen mode Exit fullscreen mode
alias tfi='terraform init'
Enter fullscreen mode Exit fullscreen mode
alias tfp='terraform plan' 
Enter fullscreen mode Exit fullscreen mode

This one is useful because it makes format tool to go in-depth (recursively) through directories.

alias tfm='terraform fmt -recursive'
Enter fullscreen mode Exit fullscreen mode

Example usage:

user@localhost $: tfm 
module_ecs_cluster/ecs.tf
module_alb/alb.tf
Enter fullscreen mode Exit fullscreen mode

Discussion (0)