小编D.R*_*Roy的帖子

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

我在 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)
Run Code Online (Sandbox Code Playgroud)

这是我执行 lambda 函数时的响应:

Response from export task at 2018-01-05T10:57:42.441844 …
Run Code Online (Sandbox Code Playgroud)

amazon-s3 amazon-web-services amazon-cloudwatch aws-lambda

5
推荐指数
1
解决办法
1902
查看次数