question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SAM Function Global Environment Variables Interact Oddly with `!If` and Conditional Values

See original GitHub issue

cfn-lint version: (cfn-lint --version)

cfn-lint 0.46.0

Description of issue.

When the !If intrinsic function is used with conditional values in SAM’s Global Environment Variables, cfn-lint warns that the value on the true side of the condition may not be available when the condition is not true. For example, given an AWS::AppConfig::Environment in the current template, an importable instance of same, and an environment variable meant to pass the ID of one of them to a Function:

---
AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Parameters:
  DeploymentMode:
    Type: String
    AllowedValues:
    - sandbox
    - live
    Default: sandbox
    ConstraintDescription: Value must be a known deployment mode.
Conditions:
  IsLive: !Equals [ !Ref DeploymentMode, live ]
Globals:
  Function:
    Environment:
      Variables:
        AppConfig__Environment: !If
        - IsLive
        - !Ref ConfigEnvironment
        - !ImportValue ConfigEnvironment-blah
Resources:
  ConfigApplication:
    Type: AWS::AppConfig::Application
    Properties:
      Name: Application-A
  ConfigEnvironment:
    Type: AWS::AppConfig::Environment
    Condition: IsLive
    Properties:
      ApplicationId: !Ref ConfigApplication
      Name: Environment-B
  FunctionC:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: provided.al2
      Handler: provided

…cfn-lint reports this:

[cfn-lint] W1001: Ref to resource “ConfigEnvironment” that may not be available when condition “IsLive” is False at Function/Environment/Variables/AppConfig__Environment/Fn::If/1/Ref

Oddly, the error is reported at a location in Function (under Globals, presumably), which I think isn’t a location which exists in the transformed template. More oddly, this error is reported only there. If the !If is removed and ConfigEnvironment is !Refed directly, the following are reported:

[cfn-lint] W1001: Ref to resource “ConfigEnvironment” that may not be available when condition “IsLive” is False at Function/Environment/Variables/AppConfig__Environment/Ref

[cfn-lint] W1001: Ref to resource “ConfigEnvironment” that may not be available when condition “IsLive” is False at Resources/FunctionC/Properties/Environment/Variables/AppConfig__Environment/Ref

That is, the error is reported both in the Globals section and in the Environment.Variables of FunctionC itself. But when the !If is present (as it should be), the error remains in Globals. If the environment variables are moved from Globals to directly within FunctionC, no error is reported. Unfortunately, this does not scale. Suppressing W1001 globally for deployments is our current workaround.

Nothing in cfn-lint jumps out at me as responsible for this issue, so I don’t have any leads for you to track down. The thing that looked closest was how this section is considered to have “non-strict” values, but that appears to be related to string vs. number, so it didn’t seem right to me.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
kddejongcommented, Apr 22, 2021

Figured out the fix. Working on a resolution.

1reaction
chrisoverzerocommented, Apr 22, 2021

I confirm the fix. Thanks so much.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Globals section of the AWS SAM template
Overridable properties. Resources can override the properties that you declare in the Globals section. For example, you can add new variables to an...
Read more >
Q: How I can setup different number of variables for lambda ...
In my example I was trying to inject environmental variables based on whether a DynamoDb table in the template did or did not...
Read more >
User Manual and Reference v.10.7.5 - newLISP
During startup, newLISP sets the environment variable NEWLISPDIR, if it is not set already. On Linux, BSDs, macOS and other Unixes the variable...
Read more >
vegan: Community Ecology Package
The functions in the vegan package contain tools for diversity analysis, ... If missing, variables are looked in the global environment.
Read more >
Econometric Analysis of Cross Section and Panel Data - IPC-IG
The Role of Conditional Expectations in Econometrics ... taneous equations models, we study estimation of a general linear system by instru-.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found