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: Using a Ref in StageName is Not Working Properly - Template.yaml #3548

Closed
Jonathynlee opened this issue Feb 22, 2024 · 4 comments
Closed
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

@Jonathynlee
Copy link

Jonathynlee commented Feb 22, 2024

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 Jonathynlee added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Feb 22, 2024
@Jonathynlee Jonathynlee changed the title Bug: Using a Ref in StageName is Not Working Properly Bug: Using a Ref in StageName is Not Working Properly - Template.yaml Feb 22, 2024
@lucashuy
Copy link
Contributor

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:

if isinstance(permitted_stage, str):
if not permitted_stage:
raise InvalidResourceException(rest_api_id, "StageName cannot be empty.")
stage_suffix = permitted_stage
else:
stage_suffix = "Stage"
(aggregate issue: #2533)

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

@lucashuy lucashuy transferred this issue from aws/aws-sam-cli Feb 22, 2024
@lucashuy lucashuy added the area/intrinsics Ref, If, Sub, GetAtt, ... label Feb 22, 2024
@moelasmar
Copy link
Contributor

moelasmar commented Feb 24, 2024

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 moelasmar added blocked/close-if-inactive Blocked for >14 days with no response, will be closed if still inactive after 7 days and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Feb 24, 2024
@Jonathynlee
Copy link
Author

@moelasmar
Amazing! Thank you!

Copy link
Contributor

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