[PR #167] [MERGED] fix: Fix KeyError in reverse_relations when handling module resources #187

Closed
opened 2026-05-06 12:38:05 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/patrickchugh/terravision/pull/167
Author: @malekmaciej
Created: 2/8/2026
Status: Merged
Merged: 2/9/2026
Merged by: @patrickchugh

Base: mainHead: main


📝 Commits (1)

  • 3ca467b fix: resolve KeyError when processing Terraform configurations that contain resources defined in submodules with cross-references between module resources and root resources

📊 Changes

1 file changed (+4 additions, -1 deletions)

View changed files

📝 modules/graphmaker.py (+4 -1)

📄 Description

Fix KeyError in reverse_relations when handling module resources

Problem

Terravision was crashing with a KeyError when processing Terraform configurations that contain resources defined in submodules with cross-references between module resources and root resources.

Error:

KeyError: 'aws_iam_role.cloudwatch_trigger_ssm_role'

Stack trace:

File "modules/graphmaker.py", line 354, in reverse_relations
    tfdata["graphdict"][node].remove(c)
    ~~~~~~~~~~~~~~~~~~~^^^^^^
KeyError: 'aws_iam_role.cloudwatch_trigger_ssm_role'

Root Cause

In the reverse_relations function (line 354), the code was using the wrong variable name as the dictionary key:

tfdata["graphdict"][node].remove(c)

The issue is that:

  • node = resource name without module prefix (e.g., aws_iam_role.cloudwatch_trigger_ssm_role)
  • n = full resource name with module prefix (e.g., module.build.aws_iam_role.cloudwatch_trigger_ssm_role)

The graphdict keys are stored with their full module paths (n), but the code was attempting to access the dictionary using the shortened name (node), causing a KeyError when the resource was defined in a module.

Solution

Updated line 352-354 in modules/graphmaker.py to:

  1. Use the correct variable: Changed from node to n to match the actual dictionary key
  2. Add safety check: Ensure the graphdict key exists before accessing
  3. Verify list membership: Only attempt removal if the item exists in the list

Before:

if reverse_origin:
    if n not in tfdata["graphdict"][c]:
        tfdata["graphdict"][c].append(n)
    tfdata["graphdict"][node].remove(c)  # ❌ Wrong key: 'node' doesn't exist

After:

if reverse_origin:
    if not tfdata["graphdict"].get(c):
        tfdata["graphdict"][c] = list()
    if n not in tfdata["graphdict"][c]:
        tfdata["graphdict"][c].append(n)
    if c in tfdata["graphdict"].get(n, []):
        tfdata["graphdict"][n].remove(c)  # ✅ Correct key with safety checks

Testing

Tested with a Terraform configuration containing:

  • Root-level resources
  • Submodule resources (in build/ directory)
  • Cross-references between root and module resources (IAM roles, CloudWatch events, etc.)

Result: Successfully generated diagram without errors.

Impact

  • Fixes: KeyError crashes when processing modules with cross-resource references
  • Improves: Robustness by adding defensive checks for dictionary access
  • No breaking changes: Only fixes edge case handling; existing functionality unchanged

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/patrickchugh/terravision/pull/167 **Author:** [@malekmaciej](https://github.com/malekmaciej) **Created:** 2/8/2026 **Status:** ✅ Merged **Merged:** 2/9/2026 **Merged by:** [@patrickchugh](https://github.com/patrickchugh) **Base:** `main` ← **Head:** `main` --- ### 📝 Commits (1) - [`3ca467b`](https://github.com/patrickchugh/terravision/commit/3ca467be508f710d8589c93a07cca24a674f6f9a) fix: resolve KeyError when processing Terraform configurations that contain resources defined in submodules with cross-references between module resources and root resources ### 📊 Changes **1 file changed** (+4 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `modules/graphmaker.py` (+4 -1) </details> ### 📄 Description # Fix KeyError in reverse_relations when handling module resources ## Problem Terravision was crashing with a `KeyError` when processing Terraform configurations that contain resources defined in submodules with cross-references between module resources and root resources. **Error:** ``` KeyError: 'aws_iam_role.cloudwatch_trigger_ssm_role' ``` **Stack trace:** ```python File "modules/graphmaker.py", line 354, in reverse_relations tfdata["graphdict"][node].remove(c) ~~~~~~~~~~~~~~~~~~~^^^^^^ KeyError: 'aws_iam_role.cloudwatch_trigger_ssm_role' ``` ## Root Cause In the `reverse_relations` function (line 354), the code was using the wrong variable name as the dictionary key: ```python tfdata["graphdict"][node].remove(c) ``` The issue is that: - `node` = resource name **without** module prefix (e.g., `aws_iam_role.cloudwatch_trigger_ssm_role`) - `n` = **full** resource name with module prefix (e.g., `module.build.aws_iam_role.cloudwatch_trigger_ssm_role`) The `graphdict` keys are stored with their full module paths (`n`), but the code was attempting to access the dictionary using the shortened name (`node`), causing a KeyError when the resource was defined in a module. ## Solution Updated line 352-354 in `modules/graphmaker.py` to: 1. **Use the correct variable**: Changed from `node` to `n` to match the actual dictionary key 2. **Add safety check**: Ensure the graphdict key exists before accessing 3. **Verify list membership**: Only attempt removal if the item exists in the list **Before:** ```python if reverse_origin: if n not in tfdata["graphdict"][c]: tfdata["graphdict"][c].append(n) tfdata["graphdict"][node].remove(c) # ❌ Wrong key: 'node' doesn't exist ``` **After:** ```python if reverse_origin: if not tfdata["graphdict"].get(c): tfdata["graphdict"][c] = list() if n not in tfdata["graphdict"][c]: tfdata["graphdict"][c].append(n) if c in tfdata["graphdict"].get(n, []): tfdata["graphdict"][n].remove(c) # ✅ Correct key with safety checks ``` ## Testing Tested with a Terraform configuration containing: - Root-level resources - Submodule resources (in `build/` directory) - Cross-references between root and module resources (IAM roles, CloudWatch events, etc.) **Result:** Successfully generated diagram without errors. ## Impact - **Fixes**: KeyError crashes when processing modules with cross-resource references - **Improves**: Robustness by adding defensive checks for dictionary access - **No breaking changes**: Only fixes edge case handling; existing functionality unchanged --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 12:38:05 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/terravision#187
No description provided.