【问题标题】:Why does Cloudwatch trigger lambda when no events occur?为什么 Cloudwatch 在没有事件发生时触发 lambda?
【发布时间】:2020-04-15 04:32:22
【问题描述】:

我正在关注 CloudTrail 和 CloudWatch 上的 AWS 教程,并使用它来设置一个规则,该规则将在 S3 存储桶上发生 PUT 操作时触发 Lambda。 lambda 将打印存储桶的名称和其他详细信息。

import json
import urllib.parse
import boto3

# instantiate an S3 object
s3 = boto3.client('s3')

def lambda_handler(event, context):
    # Start processing by looking at Records key-value
    try:
        records = event['Records']
        if len(records) == 0:
            raise Exception("Records was zero!")
        else:
            packet = []
            for row in records:
                msg = "Received event type - {}".format(row["eventName"]) + \
                " caused by user - {}".format(
                    row["userIdentity"]["principalId"]) + \
                    " on bucket - {}".format(row["s3"]["bucket"]["name"]) + \
                    " whose owner is - {}".format(
                        row["s3"]["bucket"]["ownerIdentity"]["principalId"]) + \
                        " The specific object that got put was {}".format(
                            row["s3"]["object"]["key"]
                            )
                print(msg)
                packet.append(
                    {
                        "eventName":row["eventName"],
                        "userIdentity":row["userIdentity"]["principalId"],
                        "bucket":row["s3"]["bucket"]["name"],
                        "bucketOwner":row["s3"]\
                        ["bucket"]["ownerIdentity"]["principalId"],
                        "objectKey":row["s3"]["object"]["key"]
                        }                   )
            return {"statusCode":200, "body":packet}
    except:
        raise Exception("Could not find Records from Events")

我注意到,在 Cloudwatch 日志中(在日志组下),每当我将文件放在 S3 存储桶下时,我都会看到 S3 存储桶的所有活动。 但也有例外情况 -> 无法从事件中找到记录 - 存在。

为什么在这种情况下会触发 Lambda?

【问题讨论】:

    标签: amazon-web-services amazon-s3 aws-lambda amazon-cloudwatch


    【解决方案1】:

    您能否打印异常并查看引发的异常是什么。

    【讨论】:

    • 引发的异常是“无法从事件中找到记录”。当事件键中不存在 Records 键时,会发生这种情况。记录键保存数据点。问题是如果没有要处理的记录,为什么会触发 lambda
    • 无法从事件中找到记录异常是由您打印或引发的。你能检查一下异常是keyerror还是别的什么。只需将代码更改为例外 Exception as e 和 print(e)。
    • 这是一个 Keyerror - 关键是 Records。感谢您的意见
    【解决方案2】:

    替代解决方案 - 如果 cloudwatch 出现错误。您可以在 s3 中使用通知配置。这将在 s3 中创建对象时调用 lambda。

    Resources: bucket1: Type: AWS::S3::Bucket Properties: BucketName: !Ref SourceBucket NotificationConfiguration: LambdaConfigurations: - Event: 's3:ObjectCreated:*' Function: !GetAtt LambdaFunction.Arn BucketPermission: Type: AWS::Lambda::Permission Properties: Action: 'lambda:InvokeFunction' FunctionName: !Ref LambdaFunction Principal: s3.amazonaws.com SourceArn: !Sub arn:aws:s3:::${SourceBucket}

    【讨论】:

    • 知道了 - 这将在 S3 中创建对象时做出响应。那么我们什么时候应该使用它而不是 Cloud trail 和 Cloud watch 呢?
    猜你喜欢
    • 1970-01-01
    • 2019-10-24
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    相关资源
    最近更新 更多