Skip to content

Fix: CFn: mappings with references #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

lizard-boy
Copy link

Motivation

A customer presented an error which involved a FindInMap in a Condition. The FindInMap uses Refs for its map and top level keys, which raised an error:

    return mappings[map_name][top_level_key][second_level_key]
           ~~~~~~~~^^^^^^^^^^
TypeError: unhashable type: 'dict'

Changes

  • Implement the fix by resolving the ref from the template parameters
  • Improve error handling on invalid input

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This PR addresses an issue with the FindInMap function in CloudFormation templates when using Ref for map and top-level keys, improving error handling and implementing a fix for resolving references from template parameters.

  • Updated resolve_condition function in template_utils.py to handle Ref cases within Fn::FindInMap
  • Added new test case test_mapping_ref_map_key in test_mappings.py to validate the fix
  • Introduced TemplateError class in errors.py for more specific error handling in CloudFormation templates
  • Created mapping-ref-map-key.yaml template to demonstrate mappings with references in conditions
  • Added tests for deletion of failed nested stacks in test_nested_stacks.py

12 file(s) reviewed, 5 comment(s)
Edit PR Review Bot Settings

Comment on lines +185 to +192
if isinstance(map_name, dict) and "Ref" in map_name:
ref_name = map_name["Ref"]
param = parameters.get(ref_name)
if not param:
raise TemplateError(
f"Invalid reference: '{ref_name}' does not exist in parameters: '{parameters}'"
)
map_name = param.get("ResolvedValue") or param.get("ParameterValue")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider extracting this Ref resolution logic into a separate function to reduce code duplication

Comment on lines +224 to +228
value = top_level_map.get(second_level_key)
if not value:
raise TemplateError(
f"Invalid reference: '{second_level_key}' could not be found in the '{top_level_key}' mapping: '{top_level_map}'"
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using .get() with a default value instead of checking for None

Comment on lines 477 to +480
context = {**payload["callbackContext"], **event.custom_context}
payload["callbackContext"] = context
payload["requestData"]["resourceProperties"] = event.resource_model
resource["Properties"] = event.resource_model
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using a more descriptive variable name instead of 'context'

LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
RoleName: LambdaExecutionRole
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Using a hardcoded RoleName may cause issues with multiple deployments or updates. Consider using a dynamic naming strategy

Comment on lines +13 to +14
BucketStackId:
Value: !Ref ChildStack
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider adding a description for this output to improve template readability.

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.

3 participants