Terraform Resource Listing for Nested Directory
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!