Skip to content

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

Open

Description

@prower-turnitin

Description:

Defect where conditional Global environment variables fail to merge properly to Function.
This example demonstrates a defect in AWS SAM where conditional environment variables defined in the Globals.Function.Environment.Variables section using !If statements do not properly merge into individual AWS::Serverless::Function resources that have their own Environment.Variables section.

Steps to reproduce:

Deploy the linked https://github.com/prower-turnitin/sam-conditional-globals-example/tree/main# SAM App.
You'll see function2 does not merge Global Environment as described : https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy-globals.html

Observed result:

When conditional statements (!If) are used in the Globals section, the conditional environment variables are NOT merged into functions that have their own Environment.Variables section.

Expected result:

According to AWS SAM documentation, global environment variables should merge with function-specific environment variables, with function-specific variables taking precedence when there are conflicts.

Function1 (No function-specific environment variables)

✅ WORKS CORRECTLY - Receives all conditional global variables:

{
  "LOG_LEVEL": "INFO",
  "log_level": "INFO", 
  "OTEL_INSTRUMENTATION_COMMON_DEFAULT_ENABLED": "false",
  "OTEL_SERVICE_NAME": "sam-conditional-example-dev"
}

Function2 (Has function-specific environment variables)

❌ DEFECT DEMONSTRATED - Missing conditional global variables:

{
  "LOG_LEVEL": "DEBUG",  // Function-specific override (correct)
  "FUNCTION_SPECIFIC_VAR": "function2-value",  // Function-specific (correct)
  "CUSTOM_CONFIG": "enabled"  // Function-specific (correct)
  // MISSING: OTEL_INSTRUMENTATION_COMMON_DEFAULT_ENABLED
  // MISSING: OTEL_SERVICE_NAME
  // MISSING: log_level
}

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Darwin NOA-M-JW3FNV5R6Y 24.5.0 Darwin Kernel Version 24.5.0: Tue Apr 22 19:53:27 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6041 arm64

  2. sam --version: SAM CLI, version 1.137.1

  3. AWS region: us-east-2

# Paste the output of `sam --info` here
{
  "version": "1.137.1",
  "system": {
    "python": "3.11.10",
    "os": "macOS-15.5-arm64-arm-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "Not available",
    "aws_cdk": "Not available",
    "terraform": "1.5.7"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_PACKAGE_PERFORMANCE",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}

Add --debug flag to command you are running

Activity

  1. added
    stage/needs-triage
    on Jul 10, 2025
  2. changed the title [-]Bug: TITLE[/-] [+]Bug: SAM Conditional Globals Environment Variables maps fail to merge with Function environment variables [/+] on Jul 10, 2025
  3. dcabib commented on Oct 3, 2025

    @dcabib

    @prower-turnitin Thanks for the detailed reproduction case! I can see exactly what's happening here. This looks like a legitimate bug in how SAM's transformation process handles conditional intrinsic functions within the Globals section during the merge operation.

    The root cause is likely in SAM's template transformation logic where it merges global properties into individual function resources. When you use !If conditionals in Globals.Function.Environment.Variables, the transformer appears to be treating the entire conditional structure as a single opaque value rather than evaluating it and then merging the resulting key-value pairs. So when a function defines its own Environment.Variables, SAM's merge logic probably sees a conflict between the conditional structure in Globals and the map in the function, and it's choosing to keep only the function-level variables instead of properly evaluating the conditional first and then merging.

    The issue is that SAM needs to resolve conditionals before performing the merge operation, but it's likely doing the merge first and then resolving conditionals, or it's not handling the conditional case in the merge logic at all. This would be in the SAM transformer code that handles the Globals section expansion, probably somewhere in the samtranslator package where it processes AWS::Serverless::Function resources.

    As a workaround until this is fixed, you could move the conditional logic down to each individual function's environment variables section, or use a nested stack pattern where you conditionally include different templates. Not ideal, but it would unblock you. Have you tried using !If at the entire Environment property level rather than within the Variables map? That might also work around the merge issue.

  4. prower-turnitin commented on Oct 7, 2025

    @prower-turnitin
    Author
  5. removed
    stage/needs-triage
    on Nov 13, 2026
  6. bnusunny commented on Nov 13, 2026

    @bnusunny
    Contributor

    Thanks for the example repo, and sorry for the delay. Merging Globals into functions happens in the SAM Transform (aws/serverless-application-model), not in SAM CLI, so we're transferring this issue there so the right team can investigate the !If merge behaviour.

  7. transferred this issue fromaws/aws-sam-clion Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions