我在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'
什麼可能的錯誤是什麼?
你爲什麼不使用'create_export_task'? http://boto3.readthedocs.io/en/latest/reference/services/logs.html#CloudWatchLogs.Client.create_export_task –
這是因爲您正試圖在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 。 –
在你的方法調用中你有'fromTime'和'to',這些是UNIX時間戳,告訴你的函數要導出哪些日誌條目,根據當前的時間戳,在每次運行時在你的Lambda函數中計算它們 –