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

AutoPublishAlias not accepting !If and conditions

Open angustohrallegrinski opened this issue 5 years ago • 4 comments

I'm trying to create a SAM template to use in multiple development stages. In some stages, I have to use AutoPublishAlias (production stages); in other stages (like development or test) I don't need and I don't whant AutoPublishAlias.

But, if I use conditions to set the property

Parameters:
  DeployStack:
      Type: String

Conditions: 
  CreateProdResources: !Equals [ !Ref DeployStack, 'prod' ]

AutoPublishAlias: !If [CreateProdResources, !Ref DeployStack, !Ref AWS::NoValue]

, I have this error:

Resource with id [HelloWorldFunction] is invalid. Alias name is required to create an alias

My solution for now is to use two different SAM files, but in this project we plan to have from 40 to 60 lambda functions and can be hard to maitain.

angustohrallegrinski avatar Sep 22 '19 13:09 angustohrallegrinski

Thanks for reporting this! SAM doesn't support Fn::If on the AutoPublishAlias property. Limited support for Fn::If was introduced in our last release in #988, but a little extra work is required to support it for AutoPublishAlias.

Please +1 on the feature to help with prioritization.

keetonian avatar Sep 27 '19 17:09 keetonian

@keetonian I also spotted this on the Enabled property of a nested event in AWS::Serverless::Function, eg.

      Events:
        1MinSchedule:
          Type: Schedule
          Description: run every 1 minute
          Properties:
            Schedule: cron(0/1 * ? * * *)
            Enabled: !Equals [!Ref DeployTypeParameter, prod]

I tried !If and !Equals (with a condition). Should I call that a dupe of this issue or open a new one?

mcintyre94 avatar Oct 04 '19 08:10 mcintyre94

@mcintyre94 Open a new one.

It looks to me like !Equals is not supported in CFN outside of the Conditions section of the template.

Enabled expects a boolean value, if you want to provide that via a parameter, since CFN doesn't support boolean parameter types, I think you can either pass in any string parameter to evaluate as a true value or a blank string to evaluate to false.

keetonian avatar Oct 04 '19 17:10 keetonian

I am still having the same issue 3 years later. I don't know why this issue got closed... It doesn't seem to be fixed.

I think implementing a way to use conditionals in a SAM template should be a basic function that is supported. smh

DanielWsk avatar Jul 27 '22 17:07 DanielWsk

You might be able to get this to work by adding AWS::LanguageExtensions to Transform as such:

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

AWS::LanguageExtensions resolves intrinsic functions if the value is known when Transforms are run.

See https://github.com/aws/serverless-application-model/issues/2533 for more information.

hoffa avatar Oct 17 '22 21:10 hoffa

Closing in favor of https://github.com/aws/serverless-application-model/issues/2533.

hoffa avatar Nov 03 '22 23:11 hoffa