您可以通过Amazon CloudWatch和AWS Lambda来优化AWS GuardDuty成本。 CloudWatch可以设置CloudTrail日志规则以过滤掉不必要的事件,而Lambda可以处理CloudTrail日志,将特定事件类型作为守卫任务进行处理,同时忽略其他无关事件类型,从而可以减少GuardDuty成本。
以下是可能用到的Lambda代码示例:
import json
import boto3
def lambda_handler(event, context):
# Ensure this function only processes AWS CloudTrail API Calls
if event['detail-type'] != 'AWS API Call via CloudTrail':
return
# Process only the AWS API calls you want to monitor
omitted_operations = ['CreateBucket']
if event['detail']['eventName'] in omitted_operations:
return
# Trigger AWS GuardDuty with findings
guardduty_client = boto3.client('guardduty')
guardduty_client.create_sample_findings(
DetectorId='YourDetectorId',
FindingTypes=['Policy:IAMUser/S3BucketEncryptionDisabled'])
在此代码示例中,您可以根据自己的需要更改omitted_operations
和FindingTypes
的列表。