Repository navigation
fix(globals): merge a local dict or list into each branch of a global Fn::If - #4044
Open
Shaurya55555 wants to merge 1 commit into
Open
Shaurya55555 wants to merge 1 commit into
Shaurya55555 wants to merge 1 commit into
Conversation
… Fn::If A global property wrapped in Fn::If was dropped from every resource that declared the same property, because the merge treats an intrinsic as a primitive and prefers the local value. Merge the local dict or list into each branch of the Fn::If instead, so the result stays conditional. Fixes aws#4035
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 #, if available
Fixes #4035
Description of changes
When a
Globalsproperty is wrapped inFn::If, it was dropped from every resource that declares the same property itself. The merge inGlobalProperties._do_mergetreats an intrinsic function as a primitive, so a globalFn::Ifmet a local dict as "different types" and the local value won alone. This is the case from #4035:F2ended up withVariables: {X: "1"}and lost the conditional global variables, although the Globals documentation says global and local maps are merged.Now, when the global value is a well-formed
Fn::If(a list of three) and the local value is a dict or a list, the local value is merged into each branch and the result stays conditional:A branch that cannot be merged with the local value (for example
Ref: AWS::NoValue) ends up as the local value alone, so local keys always win and nothing is added when the condition is false. NestedFn::Ifinside a branch is handled by the same recursion. The same rule applies to lists (for exampleLayers), which are concatenated within the chosen branch.Behavior that does not change: a local intrinsic (including a local
Fn::If) still wins over a global dict;Fn::Ifon both sides still prefers local; a malformedFn::Ifis not treated as a conditional.One behavior change to be aware of: any global
Fn::Ifthat holds a dict or list is now merged with a local dict or list for the same property, where before the global was silently dropped. For example a globalVpcConfig: !If [UseVpc, {...}, !Ref AWS::NoValue]and a function with its ownVpcConfignow combine in the true branch, as they already do for a globalVpcConfigthat is not conditional. I think this is the documented intent, but it is a change for templates that relied on the global being ignored, so please say if you would rather limit it to some properties.Description of how you validated changes
tests/plugins/globals/test_globals.py(merge into both branches of a dict,NoValuebranch, list, nestedFn::If,Fn::Ifunder a key as inEnvironment.Variables, and the unchanged-behavior cases listed above). 5 of the 8 fail on the unchanged code; the other 3 pin behavior that must not change.tests/plugins/globals: 76 passed.globals_function_with_conditional_environment, with outputs foraws,aws-cnandaws-us-gov(globalEnvironment.VariablesandLayersasFn::If, one function that inherits them and one that declares its own). The 3 cases fail on the unchanged code and pass now. I generated the outputs withbin/add_transform_test.pyand read them to check the merged branches.Globals:section (the only ones the change can reach): 469 passed, 9 failed. Those 9 (function_with_events_and_propagate_tagsandimplicit_http_api_with_many_conditions, plus the openapi3 variants) fail the same way on the unchangeddevelop, so they are not caused by this change. I ran this on Windows with Python 3.14.4, which may be why.ruff checkpasses on the two changed Python files.ruff format --checkandmypyreport the same findings on the unchanged files in my environment (blackis not installed here), so I did not reformat anything.make pr.Checklist
Examples?
Not needed.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.