【问题标题】:Python3.7 script to export CloudWatch logs to S3用于将 CloudWatch 日志导出到 S3 的 Python3.7 脚本
【发布时间】:2019-12-26 04:00:02
【问题描述】:

我正在使用以下代码将 CloudWatch 日志复制到 S3:-

import boto3
import collections
from datetime import datetime, date, time, timedelta

    region = 'eu-west-1'

    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_CloudwatchLogs',
            logGroupName='/aws/lambda/stop-instances',
            fromTime=int((yesterday-unix_start).total_seconds() * 1000),
            to=int((today -unix_start).total_seconds() * 1000),
            destination='bucket',
            destinationPrefix='bucket-{}'.format(yesterday.strftime("%Y-%m-%d"))
        )
        return 'Response from export task at {} :\n{}'.format(datetime.now().isoformat(),response)

我为角色提供了以下策略:-

policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents",
        "logs:DescribeLogStreams",
        "logs:CreateExportTask",
        "logs:DescribeExportTasks",
        "logs:DescribeLogGroups"
    ],
      "Resource": [
        "arn:aws:logs:*:*:*"
    ]
  }
 ]
}
EOF

第二条政策:-

policy = <<EOF
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Action": [
            "s3:PutObject",
            "s3:PutObjectAcl",
            "s3:GetBucketAcl"
          ],
          "Effect": "Allow",
          "Resource": ["arn:aws:s3:::${var.source_market}-${var.environment}-${var.bucket}/*"],
          "Condition": { "StringEquals": { "s3:x-amz-acl": "bucket-owner-full-control" } }
        }
      ]
    }
    EOF

如果我在 AWS 控制台中执行此操作,我会遇到以下错误:-

{ "errorMessage": "调用 CreateExportTask 操作时发生错误 (InvalidParameterException):对给定存储桶的 GetBucketAcl 调用失败。请检查 CloudWatch Logs 是否已被授予执行此操作的权限。", "errorType": "InvalidParameterException"

在使用适当的策略附加角色后,我已经引用了许多块。

【问题讨论】:

    标签: python-3.x aws-lambda amazon-iam


    【解决方案1】:

    检查存储桶上的加密设置。我遇到了同样的问题,这是因为我将它设置为 AWS-KMS。我使用与您相同的权限收到此错误,然后在我将加密切换到 AES-256 后它立即开始工作

    【讨论】:

    • 这是我的问题
    【解决方案2】:

    这似乎是 s3 存储桶权限的问题。您需要将此策略附加到您的 s3 存储桶。请通过更改 cloudwatch 的存储桶名称和 aws 区域来修改策略。

    {
    "Version": "2012-10-17",
    "Statement": [
      {
          "Action": "s3:GetBucketAcl",
          "Effect": "Allow",
          "Resource": "arn:aws:s3:::my-exported-logs",
          "Principal": { "Service": "logs.us-west-2.amazonaws.com" }
      },
      {
          "Action": "s3:PutObject" ,
          "Effect": "Allow",
          "Resource": "arn:aws:s3:::my-exported-logs/random-string/*",
          "Condition": { "StringEquals": { "s3:x-amz-acl": "bucket-owner-full-control" } },
          "Principal": { "Service": "logs.us-west-2.amazonaws.com" }
      }
    ]}
    

    https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/S3ExportTasksConsole.html

    【讨论】:

    • 在创建策略时,使用存储桶的名称以及 Principal 中的区域名称更新策略很重要。这就是我从他们的文档中复制 AWS 的示例时遇到的问题 - 他们将其标记为更新,但我没有注意到。
    【解决方案3】:

    我遇到了同样的错误,问题是我在策略上设置了“目标”参数,例如存储桶/某物,而我只有存储桶,因此删除参数上的某物前缀解决了问题,因此请检查策略和参数匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-07
      • 2019-12-25
      • 2019-11-01
      • 1970-01-01
      • 2022-10-06
      • 1970-01-01
      相关资源
      最近更新 更多