fix: S3 backend config detection with multiple terraform blocks #74
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue
The
generate_s3_backend_config
function had a bug where it would exit the loop after finding the first terraform block with a backend configuration, ignoring any subsequent terraform blocks that might contain the actual backend configuration.This could cause issues in configurations with multiple terraform blocks across different files, where the function might not find the correct backend configuration.
Fix
Introduced a temporary variable
tmp_backend_config
to properly handle multiple terraform blocks. The function now correctly finds the first valid backend configuration and stops searching after that point, ensuring it doesn't miss backend configurations in subsequent terraform blocks.Testing
I've verified this fix works correctly with configurations that have multiple terraform blocks.
A rough test case to reproduce this issue is available in a separate branch for review. While the test code is not very clean, it demonstrates the issue and confirms the fix works as expected.
https://github.com/hazi/terraform-local/commits/reproduce_bug_in_backend_config/
https://github.com/hazi/terraform-local/blob/df3f6d0111b093a4e06a7e15e964ecea78f67a30/tests/test_apply.py#L456-L472