2016-12-05 111 views
0

我在CloudWatch中有一些日志,并且每天都会收到一些日志。现在,我想将今日和昨天的日志存储在云端监视中,但是旧日志不得不转移到S3。使用Lambda将Cloudwatch Logs导出到S3

我曾尝试使用下面的代码,CloudWatch的日志导出到S3的尝试:

import boto3 
import collections 

region = 'us-east-1' 

def lambda_handler(event, context): 

    s3 = boto3.client('s3') 

    response = s3.create_export_task(
     taskName='export_task', 
     logGroupName='/aws/lambda/test2', 
     logStreamNamePrefix='2016/11/29/', 
     fromTime=1437584472382, 
     to=1437584472402, 
     destination='prudhvi1234', 
     destinationPrefix='AWS' 
    ) 

    print response  

当我运行它,我得到了以下错误:

'S3' object has no attribute 'create_export_task': AttributeError 
Traceback (most recent call last): 
    File "/var/task/lambda_function.py", line 10, in lambda_handler 
    response = s3.create_export_task(
AttributeError: 'S3' object has no attribute 'create_export_task' 

什么可能的错误是什么?

+1

你为什么不使用'create_export_task'? http://boto3.readthedocs.io/en/latest/reference/services/logs.html#CloudWatchLogs.Client.create_export_task –

+1

这是因为您正试图在s3客户端上调用'create_export_task()'方法。这不是S3客户端方法,它是'logs'客户端方法。用'logs_client = boto3.client('logs')替换's3 = boto3.client('s3')',然后在下面的行中用'response = logs.client.create_export_task('''替换'response = s3.create_export_task 。 –

+0

在你的方法调用中你有'fromTime'和'to',这些是UNIX时间戳,告诉你的函数要导出哪些日志条目,根据当前的时间戳,在每次运行时在你的Lambda函数中计算它们 –

回答

1
client = boto3.client('logs') 

您正在从CloudWatch访问日志,而不是S3。因此错误。 随后

response = client.create_export_task(
     taskName='export_task', 
     logGroupName='/aws/lambda/test2', 
     logStreamNamePrefix='2016/11/29/', 
     fromTime=1437584472382, 
     to=1437584472402, 
     destination='prudhvi1234', 
     destinationPrefix='AWS' 
    ) 

http://boto3.readthedocs.io/en/latest/reference/services/logs.html#CloudWatchLogs.Client.create_export_task

结账此链接以获取更多信息

+0

这个答案的第一行是答案,在这个问题上这个问题是错误的 –

相关问题