【发布时间】: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