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

When using !Sub or !Ref in the 'Enabled' property in when 'Schedule' of AWS::Serverless::Function. #1943

Closed
stanfan1992 opened this issue Feb 26, 2021 · 6 comments
Assignees
Labels
area/event-source area/intrinsics Ref, If, Sub, GetAtt, ... stage/accepted Accepted and will be fixed type/bug

Comments

@stanfan1992
Copy link

Description:

!Sub or !Ref in the 'Enabled' property in when 'Schedule' of AWS::Serverless::Function, it cannot be transformed correctly. The AWS::CloudWatch::Rule will always use 'ENABLED' for State property

Steps to reproduce:

Launch the template below:

Transform: AWS::Serverless-2016-10-31
Parameters:
  TriggerState:
    Type: String
    Default: false
    AllowedValues:
      - true
      - false
Resources:
  ExampleLambda:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: example
      Handler: example
      Runtime: python3.8
      CodeUri:
        Bucket: myBucket
        Key: Lambda.py.zip
      Events:
        LambdaSchedule:
          Type: Schedule
          Properties:
          Name: lambda-trigger
          Schedule: cron(0 4 * * ? *)
          Description: description
          Enabled: !Sub ${TriggerState}

Observed result:

After the template is transformed/launched with 'false' to 'TriggerState', the property State of AWS::CloudWatch::Rule will be 'ENABLED'

Expected result:

As I passed 'false' for Enabled, the AWS::CloudWatch::Rule property State should be disabled.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: Mac
  2. If using SAM CLI, sam --version: SAM CLI, version 1.15.0
  3. AWS region: us-east-1

Add --debug flag to any SAM CLI commands you are running

@stanfan1992
Copy link
Author

@hawflau hawflau added area/event-source area/intrinsics Ref, If, Sub, GetAtt, ... stage/bug-repro The issue/bug needs to be reproduced type/bug and removed stage/bug-repro The issue/bug needs to be reproduced labels Feb 26, 2021
@hawflau hawflau self-assigned this Mar 1, 2021
@stanfan1992
Copy link
Author

stanfan1992 commented Mar 4, 2021

Add a workaround:

Since the issue is not successfully transforming the template with the intrinsic function we have workaround:

Using Fn::Transform to call a AWS::Include transform. Use the AWS::Include transform, which is a macro hosted by AWS CloudFormation, to insert boilerplate content into your templates. The AWS::Include transform lets you create a reference to a template snippet in an Amazon S3 bucket.

Please check the below SAM template samples:
--- template.yml ---

Transform: AWS::Serverless-2016-10-31
Parameters:
  S3Bucket: 
    Type: String 
  TriggerState:
    Type: String
    Default: false
    AllowedValues:
      - true
      - false
Resources:
  ExampleLambda:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: example
      Handler: example
      Runtime: python3.8
      CodeUri:
        Bucket: <MyBucket>
        Key: Lambda.py.zip
      Events:
        LambdaSchedule:
          Fn::Transform :
            Name : AWS::Include
            Parameters :
              Location : 
                Fn::Sub: "s3://${S3Bucket}/${TriggerState}.yml"

--- true.yml ---

Type: Schedule
Properties:
  Name: lambda-trigger
  Schedule: cron(0 4 * * ? *)
  Description: description
  Enabled: true

--- false.yml ---

Type: Schedule
Properties:
  Name: lambda-trigger
  Schedule: cron(0 4 * * ? *)
  Description: description
  Enabled: false

@MarcGuiselin
Copy link

MarcGuiselin commented Mar 15, 2021

I can reproduce this issue on v1.19.1 (via github actions)

Another workaround is to conditionally set the cron job to run on a past date like so:

Type: Schedule
Properties:
  Name: lambda-trigger
  # Workaround for disabling schedule
  Schedule: !If
    - TriggerState # Condition
    - cron(0 1 * * ? *) # 1:00 AM UTC or a custom schedule here
    - cron(0 1 1 1 ? 1970) # Never run
  Description: description
  Enabled: True

@wchengru wchengru added the stage/accepted Accepted and will be fixed label Jul 4, 2021
@pappnase99
Copy link

Just ran into this issue, more than a year later. Still happening in version 1.46.0.

@hoffa
Copy link
Contributor

hoffa commented Oct 17, 2022

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 #2533 for more information.

@hoffa
Copy link
Contributor

hoffa commented Nov 3, 2022

Closing in favor of #2533.

@hoffa hoffa closed this as completed Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/event-source area/intrinsics Ref, If, Sub, GetAtt, ... stage/accepted Accepted and will be fixed type/bug
Projects
None yet
Development

No branches or pull requests

7 participants