gpt4 book ai didi

amazon-web-services - 通过 cloudformation 在 s3 存储桶上启用对象日志记录

转载 作者:行者123 更新时间:2023-12-04 00:14:20 25 4
gpt4 key购买 nike

在 AWS S3 中,您可以访问控制台并向存储桶添加“对象级日志记录”。您创建或选择预先存在的跟踪并选择读取和写入日志类型。

现在我正在通过 Yaml CloudFormation 创建存储桶,并希望向其中添加预先存在的跟踪(或创建新的跟踪)。我怎么做?我找不到任何例子。

最佳答案

使用 CloudTrail 进行 S3 存储桶的对象日志记录是通过在 CloudTrail 中为数据事件定义所谓的事件选择器来完成的。这也可以通过 CloudFormation 获得。以下 CloudFormation 模板显示了这是如何完成的。重要的部分在下半部分(上半部分仅用于设置 CloudTrail 可以登录的 S3 存储桶):

AWSTemplateFormatVersion: "2010-09-09"

Resources:
s3BucketForTrailData:
Type: "AWS::S3::Bucket"
trailBucketPolicy:
Type: "AWS::S3::BucketPolicy"
Properties:
Bucket: !Ref s3BucketForTrailData
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service: "cloudtrail.amazonaws.com"
Action: "s3:GetBucketAcl"
Resource: !Sub "arn:aws:s3:::${s3BucketForTrailData}"
- Effect: Allow
Principal:
Service: "cloudtrail.amazonaws.com"
Action: "s3:PutObject"
Resource: !Sub "arn:aws:s3:::${s3BucketForTrailData}/AWSLogs/${AWS::AccountId}/*"
Condition:
StringEquals:
"s3:x-amz-acl": "bucket-owner-full-control"

s3BucketToBeLogged:
Type: "AWS::S3::Bucket"
cloudTrailTrail:
Type: "AWS::CloudTrail::Trail"
DependsOn:
- trailBucketPolicy
Properties:
IsLogging: true
S3BucketName: !Ref s3BucketForTrailData
EventSelectors:
- DataResources:
- Type: "AWS::S3::Object"
Values:
- "arn:aws:s3:::" # log data events for all S3 buckets
- !Sub "${s3BucketToBeLogged.Arn}/" # log data events for the S3 bucket defined above
IncludeManagementEvents: true
ReadWriteType: All

有关更多详细信息,请查看CloudFormation documentation for CloudTrail event selectors .

关于amazon-web-services - 通过 cloudformation 在 s3 存储桶上启用对象日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58257430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com