1

This is my (shortened) template.yml file:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: CF template for foobar

Parameters:
  EnvType:
    Type: String
    Description: The environment to deploy to.
    AllowedValues:
      - dev1
      - qc1
      - uat1
    Default: dev1

  VeryImportantFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src/very_important/
      Handler: app.very_important
      Runtime: python3.8
      {unrelated policies and environment variables}
      Events:
        VeryImportantSchedule:
          Type: Schedule
          Properties:
            Schedule: 'rate(2 hours)'
            Enabled: True

What I am trying to do is only enable that schedule when the EnvType parameter is set to qc1. The problem is, I can't get either the Equals condition or the If condition to work. I have tried all of the following:

Enabled: !Equals [!Ref EnvType, qc1]
Enabled: !Equals [!Ref EnvType, "qc1"]

and

Conditions:
  IsQcEnv: !Equals [!Ref EnvType, qc1]
  {and}
  IsQcEnv: !Equals [!Ref EnvType, "qc1"]

....
....
....
      Events:
        VeryImportantSchedule:
          Type: Schedule
          Properties:
            Schedule: 'rate(2 hours)'
            Enabled: !If [IsQcEnv, True, False]

In between testing all four of those cases, I deleted my entire stack rather than trying to update them, as a comment in this post suggested. I also triple checked that the parameter being passed in is not qc1, and also just set the Enabled flag to False to make sure that it would actually disable the schedule, which it did. At this point I'm stumped, does anyone have any idea of what I'm doing wrong or what I could try?

2
  • 1
    Why it does not work? Any error messages? Parring erros? Syntax errors? Stack rollbacks? Commented Apr 15, 2021 at 23:57
  • 1
    @Marcin: I had the same problem. AWS does not complain, but the schedule is always enabled. Commented Jun 12, 2021 at 10:46

3 Answers 3

5

I had the same problem. Apparently the Enabled property does not work well with !If or !Equals. I fixed it with a hack in the Schedule property:

 Events:
   InvocationLevel:
     Type: Schedule
     Properties:
        Schedule: !If [IsQcEnv, "rate(2 hours)", "cron(0 0 31 2 ? *)"]

If the envType is not qc1, the cron expression will be used. This expression will execute your function on February 31st. Which means it will never execute.

I found this solution with help from those answers:

https://stackoverflow.com/a/65214717/517173

https://stackoverflow.com/a/13938155/517173

Sign up to request clarification or add additional context in comments.

1 Comment

My team and I just got around to trying this and it works! Thanks so much :)
0

This looks like another instance of an infamous SAM bug where intrinsic functions do not work for some properties, but only in AWS:Serverless resources. A known workaround is to include the AWS::LanguageExtensions transform before Serverless like this:

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

Comments

0

There is option to use state: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-schedule.html#sam-function-schedule-state

It disables the rule.

New contributor
Алексей Ковальчук is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.