Boto3 cron:参数ScheduleExpression无效

时间:2017-01-31 10:23:37

标签: python amazon-web-services cron boto3

我正在尝试在执行15分钟后安排事件:

client = boto3.client('events')
d = datetime.now() + timedelta(minutes=40)
cronJob = "cron(" + str(d.hour) + " " + str(d.minute) + " * * ? *)"
client.put_rule(Name='extractData', ScheduleExpression=cronJob, State='ENABLED', Description='This is rule extracting flurry data')
try:
    client.put_targets( Rule='extractData', Targets=[ { 'Id': '1', 'Arn': 'arn:aws:lambda:ap-southeast-1:381409677897:function:flurry_extractReportOnDemand' }])
except:
    print("\n###################\n")
    print ("Could not schedule")
    print("\n###################\n") 

这给我一个错误

Parameter ScheduleExpression is not valid.

知道为什么吗?

1 个答案:

答案 0 :(得分:2)

http://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html#CronExpressions

第一个值应为分钟,第二个值应为小时。

所以你的代码应该是:

cronJob = "cron(" + str(d.minute) + " " + str(d.hour) + " * * ? *)"

而不是"cron(" + str(d.hour) + " " + str(d.minute) + " * * ? *)"