Skip to content

fix(terraform): create graph edges for resources using count/for_each#7508

Open
pszypowicz wants to merge 4 commits intobridgecrewio:mainfrom
pszypowicz:fix/graph-edges-foreach-count
Open

fix(terraform): create graph edges for resources using count/for_each#7508
pszypowicz wants to merge 4 commits intobridgecrewio:mainfrom
pszypowicz:fix/graph-edges-foreach-count

Conversation

@pszypowicz
Copy link
Copy Markdown

Summary

Fixes #7358

  • Graph edge builder failed to connect resources that use count or for_each because vertex names include an index suffix (e.g. bucket[0]) after expansion, but remove_index_pattern_from_str strips numeric indices from references during lookup -- causing a name mismatch in _get_possible_vertices()
  • Added a sanitized-name fallback to _get_possible_vertices() using the existing get_sanitized_terraform_resource_id() utility, which matches indexed vertex names against the stripped reference name
  • Set for_each_index on resource/data blocks in ForeachEntityHandler._create_new_resource(), mirroring the existing behavior in ForeachModuleHandler._create_new_module() -- this enables _find_best_match_based_on_foreach_key() to disambiguate between multiple indexed vertices (e.g. bucket[0] vs bucket[1])

Test plan

  • Added count = 1 pass/fail cases to S3BucketLogging graph check test
  • Added count = 2 case where both bucket and logging resource use count -- exercises multi-candidate disambiguation via for_each_index
  • Verified test fails on unfixed main (expected 3 passes, got 2)
  • All 196 graph check tests pass
  • All 47 foreach renderer tests pass

The graph edge builder failed to connect resources that use count or
for_each because vertex names include an index suffix (e.g. bucket[0])
after expansion, but remove_index_pattern_from_str strips numeric
indices from references during lookup -- causing a name mismatch.

Add a sanitized-name fallback in _get_possible_vertices and set
for_each_index on resource/data blocks to match the module handler.
@flesko-profinit
Copy link
Copy Markdown

Hey @maxamel @Saarett @AdamDev, sorry for the ping. Could somebody have a look at this?

@Saarett
Copy link
Copy Markdown
Contributor

Saarett commented Apr 15, 2026

Hi @pszypowicz, thanks for the contribution!
I’ve asked the relevant maintainers to review the PR. It may take some time due to higher-priority tasks. Meanwhile, I'm running the PR tests.

@Saarett
Copy link
Copy Markdown
Contributor

Saarett commented Apr 15, 2026

Re-running the performance-tests that failed. If it won't pass again, please work to improve the performance of the solution. Please let us know if any assistance is needed. Thanks!

The fallback in _get_possible_vertices iterated all vertices in a
(module, block_type) bucket and sanitized each name on every miss,
making edge-building quadratic on graphs with many expanded resources.

Maintain a parallel dict keyed by sanitized (un-indexed) name, populated
in _add_block_data_to_graph and reset alongside the primary map in
_arrange_graph_data. The fallback becomes an O(1) lookup.
@pszypowicz
Copy link
Copy Markdown
Author

@Saarett Thanks for feedback, fixes pushed, please let me know if I can do something else to help

@pszypowicz
Copy link
Copy Markdown
Author

@Saarett it looks like there was some other issue?

Error: An action could not be found at the URI 'https://api.github.com/repos/imranismail/setup-kustomize/zipball/a76db1c6419124d51470b1e388c4b29476f495f1' (9425:1C79:5D7422:7F6883:69E63CFB)
Error: Failed to download archive 'https://api.github.com/repos/imranismail/setup-kustomize/zipball/a76db1c6419124d51470b1e388c4b29476f495f1' after 1 attempts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CKV_AWS_18 fails when count/for_each are used

3 participants