serverless-application-model: AutoPublishAlias does not accept a !Ref

Description: Using a !Ref in AutoPublishAlias throws an error when running sam validate.

This is a duplicate of awslabs/cfn-python-lint#582 but it looks like it’s a SAM error and not a cfn-lint error, so I’m opening a ticket here.

It is not a duplicate of #220 as using!Ref in AutoPublishAlias should be allowed. Even the error message say so.

Steps to reproduce the issue:

  1. Use this template:
---

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

Parameters:
  Stage:
    Description: Environment stage (deployment phase)
    Type: String
    AllowedValues:
      - beta
      - prod

Resources:

  SkillFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: '.'
      Handler: main.handler
      Runtime: python3.7
      Timeout: 30
      MemorySize: 128
      AutoPublishAlias: !Ref Stage

  1. Run sam validate on that template

Observed result: The command results in an error 💥

Error: [InvalidResourceException('SkillFunction', "'AutoPublishAlias' must be a string or a Ref to a template parameter")] ('SkillFunction', "'AutoPublishAlias' must be a string or a Ref to a template parameter")

Expected result: The command finishes successfully without any error message.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 6
  • Comments: 15 (7 by maintainers)

Commits related to this issue

Most upvoted comments

SAM currently supports only Ref for AutopublishAlias and doesn’t support other intrinsic functions. Thanks for the feature request! We have passed this along to our product team for possible prioritization for FindInMap support. Please +1 on the feature to help with prioritization.

@jbernalvallejo That was definitely the issue w/ !Ref, thanks! The remaining issue would be around !FindInMap:

Parameters:
  Stage:
    Type: String
    Description: Which stage the code is in
    Default: test
    AllowedValues:
      - preprod
      - prod
Mappings:
  Alias:
    preprod:
      alias: test
    prod:
      alias: live
Resources:
  HelloWorldSQSFunction:
    Type: AWS::Serverless::Function 
    Properties:
      AutoPublishAlias: !FindInMap [Alias, !Ref Stage, alias]

Error: Failed to create changeset for the stack: sams-app, ex: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HelloWorldSQSFunction] is invalid. 'AutoPublishAlias' must be a string or a Ref to a template parameter I concede this is likely intended behavior due to the error message, but it would still be nice to have.

Just in case, check the issue is not the colon not needed after !Ref. I had the same scenario and it worked with no issues when deploying.

Parameters:
  Environment:
    Type: String
    Description: Environment to deploy resources to
    AllowedValues:
      - staging
      - production

Resources:
  PutFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub 'put-${Environment}'
      CodeUri: ./src/put-book/
      Handler: index.handler
      AutoPublishAlias: !Ref Environment

Talked with @jfuss who brought up that template validation should depend on whether or not a template is valid, not on the existence of a parameter. Customers should not have to pass in parameters for validation.

That is a valid SAM template and deploys via CloudFormation. It appears that SAM is trying to resolve the parameter passed into AutoPublishAlias and can’t, thus throwing an error.

Allowing parameter overrides in sam validate would fix this issue, as this expects a valid parameter to be available. Since no default is set and no parameter is passed in to the validate command, SAM cannot resolve this parameter.