简体   繁体   English

AWS CloudWatch Events 不触发 Lambda 函数

[英]AWS CloudWatch Events Do not Trigger Lambda Function

I'm having trouble triggering my AWS Lambda function.我在触发 AWS Lambda 函数时遇到问题。

The function works perfectly fine when I click Test , but I've created a new scheduled rule which triggers the Lambda function every minute.当我单击Test 时,该函数运行良好,但我创建了一个新的计划规则,每分钟触发一次 Lambda 函数。 It works once, and then never again.它工作一次,然后再也不会。 I've also tried to use Cron, same results.我也尝试使用 Cron,结果相同。

The logs should output a print function, but instead they read this:日志应该输出一个打印函数,但它们读取的是:

02:07:40
START RequestId: |numbers| Version: 8

02:07:40
END RequestId: |numbers|

I've clicked Enable on 'CloudWatch Events will add necessary permissions for target(s) so they can be invoked when this rule is triggered.', so I suspect that my permissions aren't an issue.我在“CloudWatch Events 将为目标添加必要的权限,以便在触发此规则时调用它们。”上单击启用,因此我怀疑我的权限不是问题。

As a side note, I've done everything on the console and am not really sure how to properly use the CLI.作为旁注,我已经在控制台上完成了所有工作,但不确定如何正确使用 CLI。 Any help would be wonderful.任何帮助都会很棒。 Thank you.谢谢你。

The best way is to start simple, then build-up to the ultimate goal.最好的方法是从简单的开始,然后逐步达到最终目标。

Start by creating an AWS Lambda function that simply prints something to the log file.首先创建一个 AWS Lambda 函数,该函数只是将某些内容打印到日志文件中。 Here is an example in Python:这是 Python 中的一个示例:

def lambda_handler(event, context):

    print ('Within function')

Then, ensure that the function has been assigned an IAM Role with the AWSLambdaBasicExecutionRole policy, or another policy that grants access to CloudWatch Logs:然后,确保已为该函数分配了具有AWSLambdaBasicExecutionRole策略或授予对 CloudWatch Logs 访问权限的其他策略的 IAM 角色:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "*"
        }
    ]
}

Then, configure CloudWatch Events to trigger the function once per minute and check the log files in Amazon CloudWatch Logs to confirm that the function is executing.然后,将 CloudWatch Events 配置为每分钟触发一次该函数并检查 Amazon CloudWatch Logs 中的日志文件以确认该函数正在执行。

This will hopefully work correctly.这有望正常工作。 It's then just a matter of comparing the configurations to find out why the existing function is not successfully running each minute.然后只需比较配置即可找出现有功能无法每分钟成功运行的原因。 You can also look at the Monitoring tab to see whether any executions produced errors.您还可以查看“监控”选项卡以查看是否有任何执行产生错误。

OK, here's where I went wrong:好的,这是我出错的地方:

According to this answer: https://forums.aws.amazon.com/thread.jspa?threadID=264583 AWS only runs the entire S3 zip package once.根据这个答案: https : //forums.aws.amazon.com/thread.jspa? threadID = 264583 AWS 只运行整个 S3 zip 包一次。 I needed to put all of my code into the handler to fix this.我需要将所有代码放入处理程序中以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM