2016-12-05 216 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

這個答案的第一行是答案,在這個問題上這個問題是錯誤的 –

相關問題