在哪里可以找到 Lambda@Edge 的日志?

Where to find the logs for Lambda@Edge?

作为我遇到的 previous problem 的步骤之一,我需要查看一些 Lambda@Edge 的日志,但我无法在任何地方找到它们。

根据 Lambda@Edge 上的 documentation

When you review CloudWatch log files or metrics when you're troubleshooting errors, be aware that they are displayed or stored in the Region closest to the location where the function executed. So, if you have a website or web application with users in the United Kingdom, and you have a Lambda function associated with your distribution, for example, you must change the Region to view the CloudWatch metrics or log files for the London AWS Region.

当我在加拿大时,我试图为其查找日志的 lambda 函数位于 us-east-1(由 CloudFront 强制要求,因为它被用作分发的事件处理程序)所以我假设最接近区域将是 ca-central-1。但是由于我没有在 ca-central-1 开发,所以我在该地区没有任何日志组。无论如何,我没有看到我的 Lambda@Edge 的日志。为了完整起见,我检查了所有区域,但找不到 lambda 函数的任何日志痕迹。明确地说,我正在寻找具有 lambda 函数名称的日志组。

我肯定应该有日志,因为我的代码中有 console.log(),而且我可以下载请求的内容(lambda 函数负责选择保存内容的 S3 存储桶)表示 lambda 函数已成功执行。如果不是,我应该无法获取 S3 内容。

在哪里可以找到我的 Lambda@Edge 函数的日志?

对于可能面临相同问题的任何其他人,请使用同一文档页面中提到的脚本来查找您的日志组:

FUNCTION_NAME=function_name_without_qualifiers
for region in $(aws --output text  ec2 describe-regions | cut -f 4) 
do
    for loggroup in $(aws --output text  logs describe-log-groups --log-group-name "/aws/lambda/us-east-1.$FUNCTION_NAME" --region $region --query 'logGroups[].logGroupName')
    do
        echo $region $loggroup
    done
done

创建一个文件,将上面的脚本粘贴到其中,将 function_name_without_qualifiers 替换为您的函数名称,使其可执行并 运行 它。它会为您找到 Lambda@Edge 的区域和日志组。这里吸取的教训是日志组不像普通日志组那样命名。相反,它遵循以下结构:

/aws/lambda/${region}.${function_name}

看来log describe-log-groups的格式也变了。当我尝试该脚本时,它什么也没返回。但是使用“/aws/lambda/$FUNCTION_NAME”而不是“/aws/lambda/us-east-1.$FUNCTION_NAME”脚本 returns 组列表以下结构:

${region} /aws/lambda/${function_name}