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

!If not working on Serverless:Function handler #3554

Closed
des-psl opened this issue Feb 28, 2024 · 5 comments
Closed

!If not working on Serverless:Function handler #3554

des-psl opened this issue Feb 28, 2024 · 5 comments
Labels
area/intrinsics Ref, If, Sub, GetAtt, ... blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days

Comments

@des-psl
Copy link

des-psl commented Feb 28, 2024

Description

I am trying to have a conditional (!If) on my lambda handler property to execute different logics depending of an env var

Steps to reproduce

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31

Parameters:
  NewRelicEnabled:
    Type: String
    Description: Enable New Relic integration.
    Default: true

Conditions:
  IsNewRelicEnabled: !Equals [!Ref NewRelicEnabled, true]

Globals:
  Function:
    Architectures:
      - arm64
    Handler: !If [IsNewRelicEnabled, test1.handler, test2.handler]

Resources:
  ImportFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: import-function
      CodeUri: application/functions/import/
    Metadata:
      BuildMethod: esbuild
      BuildProperties:
        Format: esm
        Minify: true
        OutExtension:
          - .js=.mjs
        Target: "es2020"
        Sourcemap: true
        EntryPoints:
          - index.ts
        Banner:
          - js=import { createRequire } from 'module'; const require = createRequire(import.meta.url);
        External:
          - application/node_modules

 Outputs:
  Condition:
    Description: asdf
    Value: !If [IsNewRelicEnabled, test1.handler, test2.handler]

Observed result

Global handlers variable is resolving to its false value (test2.handler), but at the Outputs, Condition is resolving to true (test1.handler)

Expected result

Global handlers and Output Condition should both resolving to true (test1.handler)

Additional environment details

  1. OS: Mac
  2. If using the SAM CLI, sam --version:
  3. AWS region: eu-east-1
@des-psl des-psl added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Feb 28, 2024
@xazhao
Copy link
Contributor

xazhao commented Mar 1, 2024

Hello @des-psl , thanks for raising the issue.

As mentioned in #2533 SAM Transform does not support intrinsic functions for handler. So to make sure that SAM handles the created resources as you expect, we recommend you to use the AWS::LanguageExtensions Transform, and add it before SAM Transform so SAM receives the template after got processed, and intrinsic function got replaced with the actual values.

You can update your template, and replace

Transform: AWS::Serverless-2016-10-31

with

Transform:
  - AWS::LanguageExtensions
  - AWS::Serverless-2016-10-31

@xazhao xazhao added area/intrinsics Ref, If, Sub, GetAtt, ... and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Mar 1, 2024
@des-psl
Copy link
Author

des-psl commented Mar 1, 2024

@xazhao I tried your solution but still not working:

AWSTemplateFormatVersion: "2010-09-09"
Transform:
  - AWS::LanguageExtensions
  - AWS::Serverless-2016-10-31

@moelasmar
Copy link
Contributor

Hi @des-psl,
Looking to your example, I think the problem is you defined the intrinsic function in the Globals section, and as it is not part of The CFN template anatomy (doc), so AWS::LanguageExtensions will not resolve the intrinsic functions defined in it. And since SAM Transform could not process the intrinsic functions, you are facing this problem.

The recommendation is not to use any intrinsic functions in the Globals section, and if you want to use intrinsic functions, you can use it in the resource itself.

@moelasmar moelasmar added the blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days label Mar 4, 2024
@xazhao
Copy link
Contributor

xazhao commented Mar 8, 2024

Closing the issue. Feel free to reopen it if you have other questions.

@xazhao xazhao closed this as completed Mar 8, 2024
Copy link
Contributor

github-actions bot commented Mar 8, 2024

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/intrinsics Ref, If, Sub, GetAtt, ... blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days
Projects
None yet
Development

No branches or pull requests

3 participants