作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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)
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),
{
"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"))
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/
我在 cloudwatch 上有日志,我想每天将其存储在 S3 上。我正在使用 AWS Lambda 来实现这一点。 我在 AWS Lambda 上创建了一个函数,并使用 Cloudwatch 事件作
我是一名优秀的程序员,十分优秀!