Skip to content
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

Bug: Fn::If not supported, unsure of a workaround when working with Lambda layers #7509

Open
EricXDong opened this issue Sep 24, 2024 · 0 comments
Labels
stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at.

Comments

@EricXDong
Copy link

Description:

In one project, I'm outputting a Lambda layer:

Outputs:
  LayerArn:
    Description: ""
    Value: !Ref LayerResource
    Export:
      Name: LayerArn

In a separate project, I want to import this layer:

Resources:
  LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Layers:
        - !ImportValue LayerArn

This works in production, but not locally because Fn::ImportValue doesn't work locally. The standard workaround is to use Conditions on a parameter that is overridden on local runs:

Parameters:
  Environment:
    Type: String
    Default: "prod"

Conditions:
  IsLocalRun: !Equals [!Ref Environment, "local"]

Resources:
  LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Layers:
        - !If
          - IsLocalRun
          - hardcoded ARN value
          - !ImportValue ChatFunctionApiArn-prod

But this doesn't work because Fn::If isn't supported either. Users have run into similar issues where the recommended workaround was to instead define a parameter for the value itself, which is overridden on local runs. Applied to my case, that would be something like:

Parameters:
  LayerArnParam:
    Type: String
    Default: !ImportValue LayerArn

Resources:
  LambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      Layers:
        - !Ref LayerArnParam

However, this doesn't work because Every Default member must be a string, so it seems you can't use intrinsic functions in the default values of parameters.

Observed result:

$ sam validate

E2001 Property Parameters/LayerArnParam/Default should be of type String

Expected result:

Expect to be able to !ImportValue a Lambda layer ARN in production, and override it with a hardcoded ARN in local environment.

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

  1. OS: Mac
  2. sam --version: SAM CLI, version 1.118.0
  3. AWS region: us-west-1
# Paste the output of `sam --info` here
$ sam --info
{
  "version": "1.118.0",
  "system": {
    "python": "3.8.13",
    "os": "macOS-14.5-x86_64-i386-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "27.2.0",
    "aws_cdk": "Not available",
    "terraform": "Not available"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}
@EricXDong EricXDong added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at.
Projects
None yet
Development

No branches or pull requests

1 participant