Logo AppDev24 Login / Sign Up
Sign Up
Have Login?
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Login
New Account?
Recovery
Go to Login
By continuing you indicate that you agree to Terms of Service and Privacy Policy of the site.
Terraform

Terraform Resource Listing for Nested Directory

 
Updated on Aug 15, 2024

As a DevOps engineer, managing and maintaining large-scale infrastructure deployments with Terraform can be a daunting task. One of the most critical aspects of this process is keeping track of resources created by Terraform across multiple workspaces and directories. In this article, we'll explore a simple yet powerful script that automates the listing of Terraform resources under nested directory structures.

When working with Terraform, it's common to have multiple workspaces and directories containing various infrastructure configurations. Manually listing resources for each workspace can be time-consuming and error-prone. This is where our script comes in – tf-resource-listing.sh.

Our script, written in Bash, takes advantage of the find command to traverse the directory structure and identify Terraform workspaces. It then uses Terraform's built-in state list command to generate a list of resources for each workspace.

#!/bin/bash
# Name: tf-resource-listing.sh
# Owner: Saurav Mitra
# Description: List Terraform resources By Workspace foreach nested directories
# Usage:
# ./tf-resource-listing.sh

# SET VARIABLES
repo="/Users/saurav/Tech/Codebase/Terraform/tf-aws"
workspaces=(dev stg pro)
root=`basename ${repo}`
dir=$(pwd)


for d in $(find ${repo} -maxdepth 3 -type d)
do
    dname=`basename ${d}`
    pdname=`basename $(dirname ${d})`
    if [[ ( "${dname}" == "${root}" ) || ( "${pdname}" == "terraform.tfstate.d" ) || ( "${dname}" == "terraform.tfstate.d" ) || ( "${dname}" == ".terraform" ) || ( "${dname}" == "providers" ) ]]
    then
        continue
    else
        echo "Directory- ${d}:"
        cd ${d}
        for ws in "${workspaces[@]}"
        do
            terraform workspace select ${ws}
            if [ $? -ne 0 ]; then
                continue
            fi
            # echo "Workspace- ${ws}:"
            output="${dir}/resources_${ws}.txt"
            echo "ResourcePath: ${d}" >> ${output}
            terraform state list >> ${output}
        done
    fi
done

cd ${dir}
  • Initialization: The script sets variables for the repository path, workspaces, and the current working directory.
  • Directory Traversal: The find command is used to traverse the directory structure, searching for directories up to three levels deep (-maxdepth 3 -type d). This ensures we capture all relevant directories without overwhelming the script with unnecessary information.
  • Workspace Selection: For each directory found, the script iterates through the list of workspaces and selects the corresponding workspace using terraform workspace select. If the selection fails (e.g., due to an invalid workspace), the script skips to the next iteration.
  • Resource Listing: Once a workspace is selected, the script generates a text file containing the resource listing for that workspace. The script will create separate files for each workspace, containing the list of resources.

Below is a sample terraform directory layout:

In order to avoid generating large terraform state file, each folder isolates the resources as independent terraform projects.

This script offers several benefits:

  • Efficient: Automates the process of listing Terraform resources, saving you time and reducing errors.
  • Flexible: Supports multiple workspaces and directories, making it suitable for large-scale infrastructure deployments.
  • Customizable: You can modify the script to suit your specific needs by adjusting the workspace list or adding additional logic.

In this article, we've explored a simple yet powerful Bash script that automates the listing of Terraform resources under nested directory structures. By leveraging find and terraform state list, our script provides an efficient way to manage and maintain large-scale infrastructure deployments with Terraform. Give it a try and see how it can streamline your workflow!

PrimeChess

PrimeChess.org

PrimeChess.org makes elite chess training accessible and affordable for everyone. For the past 6 years, we have offered free chess camps for kids in Singapore and India, and during that time, we also observed many average-rated coaches charging far too much for their services.

To change that, we assembled a team of top-rated coaches including International Masters (IM) or coaches with multiple IM or GM norms, to provide online classes starting from $50 per month (8 classes each month + 4 tournaments)

This affordability is only possible if we get more students. This is why it will be very helpful if you could please pass-on this message to others.

Exclucively For Indian Residents: 
Basic - ₹1500
Intermediate- ₹2000
Advanced - ₹2500

Top 10 Articles