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

sam incorrectly resolve parameters

Open mgorski-mg opened this issue 5 years ago • 9 comments

I have a problem to pass parameters to enable or disable Lambda Scheduler

Template:

Parameters:
  SchedulerCronParameter:
    Type: String
    Description: Enter scheduler cron expresion. Default is cron(0 1 1 * ? *).
    Default: cron(0 1 1 * ? *)
  SchedulerEnabledParameter:
    Type: String
    Description: Enable or disable scheduler. Default is true.
    Default: true
    AllowedValues: [true, false]

Resources:
  ScheduledLambda:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: code/
      Handler: handler.lambda_handler
      Runtime: python3.7
      Timeout: 900
      Tracing: Active
      Events:
        Schedule:
          Properties:
            Name: LambdaScheduler
            Description: Monthly schedule
            **Enabled: !Ref SchedulerEnabledParameter**
            Schedule: !Ref SchedulerCronParameter
            Input: '{"view_only": false}'
          Type: Schedule

I think it's because the template is translated before resolving CF Parameters. Code from SAM translator:

if self.Enabled is not None:
            events_rule.State = "ENABLED" if self.Enabled else "DISABLED"

If the Parameter is not resolved before translation, it's always set State as 'ENABLED'.

Do you have any workaround for it?

SAM CLI version: 0.39.0

mgorski-mg avatar Jan 03 '20 16:01 mgorski-mg

@mgorski-mg Thanks for the issue. I think this has to do with how the spec works and not specifically the deploy command. I am going to forward this over to the SAM Translator (Spec) repo for further help.

jfuss avatar Jan 03 '20 16:01 jfuss

@jfuss true that :sweat_smile: Thx!

mgorski-mg avatar Jan 03 '20 21:01 mgorski-mg

Any updates or workarounds for this? We have a Lambda that we'd like to schedule in our Production account, but not in our Development account. We're trying to use TemplateConfiguration files in our CodePipeline in order to set this value. This bug is preventing us from disabling the schedule in our Development account.

I suppose we could define two Lambdas in our template (one with a schedule and one without) that get conditionally created based on the environment. But that's brittle, error prone, and ugly.

soccerboymalloy-patriot avatar Jun 19 '20 17:06 soccerboymalloy-patriot

Any updates or workarounds for this? We have a Lambda that we'd like to schedule in our Production account, but not in our Development account. We're trying to use TemplateConfiguration files in our CodePipeline in order to set this value. This bug is preventing us from disabling the schedule in our Development account.

I suppose we could define two Lambdas in our template (one with a schedule and one without) that get conditionally created based on the environment. But that's brittle, error prone, and ugly.

You can define the scheduler using classic CloudFormation instead of the SAM. You need to define AWS::Events::Rule and AWS::Lambda::Permission. Then you can use parameters properly.

mgorski-mg avatar Jun 19 '20 21:06 mgorski-mg

You can define the scheduler using classic CloudFormation instead of the SAM. You need to define AWS::Events::Rule and AWS::Lambda::Permission. Then you can use parameters properly.

Thanks for the pointer! Sometimes I forget that I can mix and match CloudFormation and SAM. For reference, this documentation is exactly what I needed.

soccerboymalloy-patriot avatar Jun 22 '20 15:06 soccerboymalloy-patriot

I just came across this issue. Any update or progress? or the solution at the moment is still the one suggested by @mgorski-mg?

diegogurpegui avatar Sep 26 '20 02:09 diegogurpegui

I just came across this issue. Any update or progress? or the solution at the moment is still the one suggested by @mgorski-mg?

There's been no fix for this after 1 year, but there's a workaround discussed here

RichieRunner avatar Jan 19 '21 19:01 RichieRunner

I just tried this today and it is still broken. The workaround by @mgorski-mg continues to work.

ckabalan avatar Sep 21 '21 20:09 ckabalan

The same on my side. The issue is still here.

Yop-La avatar Apr 26 '22 15:04 Yop-La

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