您可以在动态计时器上触发AWS Lambda吗? [英] Can you trigger an AWS Lambda on a dynamic timer?

查看:86
本文介绍了您可以在动态计时器上触发AWS Lambda吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在动态计时器上触发AWS Lambda?目前,我正在使用计划事件来触发lambda,但这是一个设置的计时器.有没有办法动态设置从Lambda内部触发Lambda的时间?

Is there a way to trigger an AWS Lambda on a dynamic timer? Currently, I am utilizing scheduled-events to trigger the lambda, but this is a set timer. Is there a way to dynamically set a time for the Lambda to be triggered from within the Lambda?

这里的想法是,此Lambda进行特定的检查并执行代码以知道下一步应在何时运行(因为我只希望此Lambda在需要时运行).我想1)确定下次运行的时间,2)从Lambda代码中设置时间.

The idea here is that this Lambda does specific checks and executes code to know when it should run next (because I only want this lambda to run when it needs to). I want to 1) determine the next time it needs to run and 2) set the time from within the Lambda code.

我看到有很多资源可用于触发Lambda函数(SNS,Kinesis等),但我似乎找不到一种动态启动它的好方法.

I see there are a lot of resources that are used for triggering Lambda functions (SNS, Kinesis, etc.), but I cant seem to find a good way to dynamically kick one off.

推荐答案

这可以通过设置CloudWatch事件规则来触发Lambda函数来实现.在每次调用Lambda函数时,该函数都需要确定其下一次运行时间,并适当地修改事件规则.

This can be accomplished by setting a CloudWatch event rule to trigger your Lambda function. On each invocation of your Lambda function, the function will need to determine its next run time and modify the event rule appropriately.

var AWS = require("aws-sdk");

exports.handler = function(event, context) {
    var cloudwatchevents = new AWS.CloudWatchEvents();
    var intervals = Array(3, 5, 7);
    var nextInterval = intervals[Math.floor(Math.random()*intervals.length)];
    var currentTime = new Date().getTime(); // UTC Time
    var nextTime = dateAdd(currentTime, "minute", nextInterval);
    var nextMinutes = nextTime.getMinutes();
    var nextHours = nextTime.getHours();

    //  =================================
    //  DO YOUR WORK HERE
    //  =================================

    var scheduleExpression = "cron(" + nextMinutes + " " + nextHours + " * * ? *)";
    var params = {
        Name: "YOUR CLOUDWATCH EVENT RULE NAME",
        ScheduleExpression: scheduleExpression
    };
    cloudwatchevents.putRule(params, function(err, data) {
        if (err) {
            console.log(err, err.stack);  
        }
        else {
            console.log(data);
        }
    })
};

var dateAdd = function(date, interval, units) {
    var ret = new Date(date); // don't change original date
    switch(interval.toLowerCase()) {
        case 'year'   :  ret.setFullYear(ret.getFullYear() + units);  break;
        case 'quarter':  ret.setMonth(ret.getMonth() + 3*units);  break;
        case 'month'  :  ret.setMonth(ret.getMonth() + units);  break;
        case 'week'   :  ret.setDate(ret.getDate() + 7*units);  break;
        case 'day'    :  ret.setDate(ret.getDate() + units);  break;
        case 'hour'   :  ret.setTime(ret.getTime() + units*3600000);  break;
        case 'minute' :  ret.setTime(ret.getTime() + units*60000);  break;
        case 'second' :  ret.setTime(ret.getTime() + units*1000);  break;
        default       :  ret = undefined;  break;
    }
    return ret;
}

您应该能够将自己的随机决定与自己的计划逻辑交换,并插入您需要做的任何工作来代替我的评论.

You should be able to swap my random determination with your own scheduling logic and insert whatever work you need in place of my comment.

您需要在我的代码段中将事件规则的名称替换为您的CLOUDWATCH事件规则名称".

You will need to substitute your event rule's name for "YOUR CLOUDWATCH EVENT RULE NAME" in my snippet.

博客的大问题:动态调度其下一个运行时的AWS Lambda函数

这篇关于您可以在动态计时器上触发AWS Lambda吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆