Skip to content

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
aws:developfrom
Shaurya55555:fix/globals-if-env-merge
Open

Shaurya55555 wants to merge 1 commit into
aws:developfrom
Shaurya55555:fix/globals-if-env-merge

Conversation

@Shaurya55555

Copy link
Copy Markdown

Issue #, if available

Fixes #4035

Description of changes

When a Globals property is wrapped in Fn::If, it was dropped from every resource that declares the same property itself. The merge in GlobalProperties._do_merge treats an intrinsic function as a primitive, so a global Fn::If met a local dict as "different types" and the local value won alone. This is the case from #4035:

Globals:
  Function:
    Environment:
      Variables:
        Fn::If:
        - IsProd
        - OTEL_SERVICE_NAME: service-prod
        - Ref: AWS::NoValue
Resources:
  F2:
    Type: AWS::Serverless::Function
    Properties:
      Environment:
        Variables:
          X: "1"

F2 ended up with Variables: {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:

F2: Variables:
  Fn::If:
  - IsProd
  - {OTEL_SERVICE_NAME: service-prod, X: "1"}
  - {X: "1"}

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. Nested Fn::If inside a branch is handled by the same recursion. The same rule applies to lists (for example Layers), 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::If on both sides still prefers local; a malformed Fn::If is not treated as a conditional.

One behavior change to be aware of: any global Fn::If that 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 global VpcConfig: !If [UseVpc, {...}, !Ref AWS::NoValue] and a function with its own VpcConfig now combine in the true branch, as they already do for a global VpcConfig that 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

  • Added 8 cases to tests/plugins/globals/test_globals.py (merge into both branches of a dict, NoValue branch, list, nested Fn::If, Fn::If under a key as in Environment.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.
  • Added a transform test, globals_function_with_conditional_environment, with outputs for aws, aws-cn and aws-us-gov (global Environment.Variables and Layers as Fn::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 with bin/add_transform_test.py and read them to check the merged branches.
  • Ran the 134 transform test inputs that contain a Globals: section (the only ones the change can reach): 469 passed, 9 failed. Those 9 (function_with_events_and_propagate_tags and implicit_http_api_with_many_conditions, plus the openapi3 variants) fail the same way on the unchanged develop, so they are not caused by this change. I ran this on Windows with Python 3.14.4, which may be why.
  • ruff check passes on the two changed Python files. ruff format --check and mypy report the same findings on the unchanged files in my environment (black is not installed here), so I did not reformat anything.
  • Not run: the full transform suite in one go, integration tests, and a deploy against real AWS. I did not run 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.

… 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
@Shaurya55555
Shaurya55555 requested a review from a team as a code owner November 18, 2026 23:37

This branch has not been deployed

No deployments
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.

Bug: SAM Conditional Globals Environment Variables maps fail to merge with Function environment variables

1 participant