-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 intemplate_utils.py
to handle Ref cases within Fn::FindInMap - Added new test case
test_mapping_ref_map_key
intest_mappings.py
to validate the fix - Introduced
TemplateError
class inerrors.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
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") |
There was a problem hiding this comment.
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
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}'" | ||
) |
There was a problem hiding this comment.
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
context = {**payload["callbackContext"], **event.custom_context} | ||
payload["callbackContext"] = context | ||
payload["requestData"]["resourceProperties"] = event.resource_model | ||
resource["Properties"] = event.resource_model |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
BucketStackId: | ||
Value: !Ref ChildStack |
There was a problem hiding this comment.
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.
Motivation
A customer presented an error which involved a
FindInMap
in aCondition
. TheFindInMap
usesRefs
for its map and top level keys, which raised an error:Changes