serverless-application-model icon indicating copy to clipboard operation
serverless-application-model copied to clipboard

Bug: Using a Ref in StageName is Not Working Properly - Template.yaml

Open Jonathynlee opened this issue 1 year ago • 1 comments

Description:

In template.yaml, I am using a parameter reference for StageName. When I go to build and deploy my sam application, it is replaced with "Stage". If I pass the string in directly there are no issues.

Steps to reproduce:

create parameter

Parameters:
  Stage:   
    Type: String   
    Description: Stage Name
    Default: develop

use parameter in API Gateway creation

  ProfileAPIGateway:   
    Type: AWS::Serverless::Api   
    Properties:   
      GatewayResponses:   
        DEFAULT_4XX:   
          ResponseParameters:   
            Headers:   
              Access-Control-Allow-Origin: "'*'"   
      Name: !Ref ApisApi   
      StageName: !Ref Stage
...

Please note the "StageName: !Ref Stage"

Observed result:

The incorrect StageName is being used image image

Expected result:

Logical resource Ids should all have "develop" at the end, not "Stage"

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

{ "version": "1.105.0", "system": { "python": "3.11.4", "os": "Windows-10-10.0.19045-SP0" }, "additional_dependencies": { "docker_engine": "Not available", "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" ] }

Jonathynlee avatar Feb 22 '24 06:02 Jonathynlee

Thanks for reporting this issue. I can reproduce the behaviour you see with the stage name reference. This is due to how we treat and resolve intrinsics, and in this case SAM does not resolve the Ref instrinsic to the parameter for stage names: https://github.com/aws/serverless-application-model/blob/e302b200fa3a27fa7dc1417b1534e5f546d0a5b3/samtranslator/model/eventsources/push.py#L734-L740 (aggregate issue: https://github.com/aws/serverless-application-model/issues/2533)

Let me transfer this to over to the SAM repo in case the folks there have anymore insight.

lucashuy avatar Feb 22 '24 23:02 lucashuy

Thanks @Jonathynlee for raising this issue. As mentioned in this issue SAM Transform does not support intrinsic functions. 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

moelasmar avatar Feb 24 '24 02:02 moelasmar

@moelasmar Amazing! Thank you!

Jonathynlee avatar Feb 25 '24 02:02 Jonathynlee

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.

github-actions[bot] avatar Feb 25 '24 02:02 github-actions[bot]