我在CloudTrail事件"CreateBucket“上有一个事件规则,我使用了以下Lambda:
import boto3
import time
import logging
s3client = boto3.client('s3')
s3 = boto3.resource('s3')
ssmclient = boto3.client('ssm')
stsclient = boto3.client('sts')
from botocore.exceptions import ClientError
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
logger.info(event)
account_id = stsclient.get_caller_identity()["Account"]
serveraccessloggingbucket = "acct-"+account_id+"-serveraccesslogging1"
bucketName = event['detail']['requestParameters']['bucketName']
getbucketLogging = s3client.get_bucket_logging(Bucket= bucketName)
try:
applybucketlogging = s3client.put_bucket_logging(Bucket= bucketName, BucketLoggingStatus={'LoggingEnabled': {'TargetBucket': serveraccessloggingbucket,'TargetPrefix': bucketName}},)
logger.info("Applying Logging To S3 Bucket")
except ClientError as e:
statuscode = getbucketLogging['ResponseMetadata']['HTTPStatusCode']
if statuscode == 200:
print("Target Bucket Logging is Not Found")
applybucketlogging = s3client.put_bucket_logging(Bucket= bucketName, BucketLoggingStatus={'LoggingEnabled': {'TargetBucket': serveraccessloggingbucket,'TargetPrefix': bucketName}},)
else:
exit()
except Exception as e:
logger.info(str(e))然而,它并不起作用。这可能是合乎逻辑的,因为在可以调用函数时CreateBucket事件已经发生了……有什么想法吗?其目标是自动确保始终为每个S3存储桶启用服务器访问日志记录,即使用户在创建存储桶期间忘记设置。
发布于 2020-08-30 06:12:19
如果其他任何人有同样的问题,通过自动化实施服务器访问日志记录的最佳方法是结合使用配置规则/补救和AWS SSM文档(自动化)。我最终在CloudFormation模板中实现了这一点的自动化,创建了所需的角色、策略、配置规则等,并引用了亚马逊的SSM自动化来强制对任何未启用服务器访问日志记录的存储桶进行补救。
https://stackoverflow.com/questions/62287703
复制相似问题