gpt4 book ai didi

amazon-web-services - create_export_task 返回成功,但不会将数据从 cloudwatch 导出到 s3

转载 作者:行者123 更新时间:2023-12-04 08:17:54 27 4
gpt4 key购买 nike

我在 cloudwatch 上有日志,我想每天将其存储在 S3 上。我正在使用 AWS Lambda 来实现这一点。

我在 AWS Lambda 上创建了一个函数,并使用 Cloudwatch 事件作为触发器。这在 Cloudwatch 上创建了一个事件规则。现在,当我执行此 lambda 函数时,它会成功执行并在存储桶内的 S3 上创建名为“aws-log-write-test”的文件,但存储桶中没有其他数据或文件。该文件包含文本“权限检查成功”。

这是我的 lambda 函数:

import boto3
import collections
from datetime import datetime, date, time, timedelta

region = 'us-west-2'

def lambda_handler(event, context):
yesterday = datetime.combine(date.today()-timedelta(1),time())
today = datetime.combine(date.today(),time())
unix_start = datetime(1970,1,1)
client = boto3.client('logs')
response = client.create_export_task(
taskName='export_cw_to_s3',
logGroupName='ABC',
logStreamNamePrefix='ABCStream',
fromTime=int((yesterday-unix_start).total_seconds()),
to=int((today-unix_start).total_seconds()),
destination='abc-logs',
destinationPrefix='abc-logs-{}'.format(yesterday.strftime("%Y-%m-%d"))
)
return 'Response from export task at {} :\n{}'.format(datetime.now().isoformat(),response)

这是我执行 lambda 函数时的响应:
Response from export task at 2018-01-05T10:57:42.441844 :\n{'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx', 'HTTPHeaders': {'x-amzn-requestid': 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx', 'date': 'Fri, 05 Jan 2018 10:57:41 GMT', 'content-length': '49', 'content-type': 'application/x-amz-json-1.1'}}, u'taskId': u'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx'}

START RequestId: xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx Version: $LATEST
END RequestId: xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx
REPORT RequestId: xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx Duration: 1418.13 ms Billed Duration: 1500 ms Memory Size: 128 MB Max Memory Used: 36 MB

最佳答案

其实按照方法create_export_task您应该以毫秒为单位转换时间戳,将结果数乘以 1000 :

fromTime = int((yesterday-unix_start).total_seconds() * 1000),
to = int((today-unix_start).total_seconds() * 1000),

另外,请确保您已经创建了合适的 bucket policy使您的 lambda 能够导出并将对象放在您的存储桶中:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "logs.us-west-2.amazonaws.com"
},
"Action": "s3:GetBucketAcl",
"Resource": "arn:aws:s3:::abc-logs"
},
{
"Effect": "Allow",
"Principal": {
"Service": "logs.us-west-2.amazonaws.com"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::abc-logs/*",
"Condition": {
"StringEquals": {
"s3:x-amz-acl": "bucket-owner-full-control"
}
}
}
]
}

您尝试在您的存储桶上创建不同的文件夹以使日常导出彼此分开,这是一个绝妙的主意:
destinationPrefix='abc-logs-{}'.format(yesterday.strftime("%Y-%m-%d"))

但是 但不可能在策略 json 上使用时间戳,所以你必须:

Change Resource Arn to this, to allow putObject on all newly created destination folders:


"Resource":"arn:aws:s3:::abc-logs/*"

关于amazon-web-services - create_export_task 返回成功,但不会将数据从 cloudwatch 导出到 s3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48112289/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com