Amplify-cli: Add schedule option on function category cli

Created on 8 Jul 2019  路  16Comments  路  Source: aws-amplify/amplify-cli

Describe the solution you'd like

Should be possible to set a schedule/cron on function category CLI.

amplify function add

among the other questions:

  1. It's a Lambda function that should be scheduled?
  2. What's the schedule expression?

Describe alternatives you've considered

Edit the JSON autogenerated or go to console to add a schedule/cron

Where this came from: https://twitter.com/gdecampo/status/1148210103893594114

feature-request functions pending-review

Most helpful comment

@guilhermedecampo I have added this to our back log. Our product team will review this and prioritize this

All 16 comments

@guilhermedecampo I have added this to our back log. Our product team will review this and prioritize this

@yuth any updates on this? I see this is still pending review.

Is it possible to put an event to schedule the lambda in CustomResources.json instead of doing it via console?

@yuth any updates about this? I think this could be done by creating a cloudwatch event

Any update?

Any update on how we can do this? Or any alternate to do this?
I need to make something functional in next two day.

+1 to this feature. Having the ability to run cron processes/scheduled tasks in a backend is very important in many solutions.

+1 as we required to run weekly email outs to users based on data stored in database, any update about this feature request?

All,
I was able to make a workaround for it.
I created a custom stack which creates a cloudwatch Event rule which is invoked every midnight (depending on the cron expression or rule I provided), and that event triggers my lambda function.
The good thing is that when making a custom stack, we have access to the ENV variable so we can name the event depending on the environment.

This was the only way I was able to do it without using the console.

@rehanbabertkxel what about security? For example I need to charge my customer recurring monthly fee. Payment gateway expectes me to make transaction manually every month.

If anyone is still looking for a workaround to this, I managed to get it working by using AWS::Events::Rule .

Create a new function using the amplify cli, then in the newly created function's cloudformation template add the following under the Resources section:

"TagWatcherRule": {
      "Type": "AWS::Events::Rule",
      "Properties": {
         // Add cron expression here
        "ScheduleExpression": "cron(0 1 1 3 *)",
        "Targets": [
          {
            "Id": "TagWatcherScheduler",
            "Arn": {
              "Fn::GetAtt": ["LambdaFunction", "Arn"]
            }
          }
        ]
      }
    },
    "InvokeLambdaPermission": {
      "Type": "AWS::Lambda::Permission",
      "Properties": {
        "FunctionName": {
          "Fn::GetAtt": ["LambdaFunction", "Arn"]
        },
        "Action": "lambda:InvokeFunction",
        "Principal": "events.amazonaws.com",
        "SourceArn": {
          "Fn::GetAtt": ["TagWatcherRule", "Arn"]
        }
      }
    }
  }

This would be a very useful feature. +1

Hey guys, yesterday with Amplify CLI v4.18.0 we officially support schedule functionality in the functions category. Here's a blog post for the release explaining the functionality - https://aws.amazon.com/blogs/mobile/how-to-schedule-recurring-lambda-functions-using-the-amplify-cli/

amazing guys!

Hey @kaustavghosh06 ... I just updated to the latest version of the cli and tried to add a schedule using amplify update function. The push however fails with

UPDATE_FAILED               function   AWS::CloudFormation::Stack Thu Apr 09 2020 20:45:23 GMT+0200 (Central European Summer Time) Parameters: [CloudWatchRule] do not exist in the template                    

I can confirm that in the template there's a reference to the rule "ScheduleExpression": { "Ref": "CloudWatchRule" } but the rule itself is not in the template.
Would you like me track this issue separately?

@amuresia Could you please open a separate GitHub issue regarding this? Thank you.

Was this page helpful?
0 / 5 - 0 ratings