首先,必须创建用于存储CloudTrail日志的S3桶。然后,可以使用以下AWS CLI命令配置CloudTrail以将日志文件自动转储到该桶中。
aws cloudtrail create-trail --name --s3-bucket-name
同样,也需要创建用于存储VPC流日志的S3桶。使用以下AWS CLI命令配置VPC流日志以将其自动转储到该桶中。
aws ec2 create-flow-logs --resource-id --traffic-type ALL --s3-bucket
AWS Lambda函数可用于监视S3桶中的新对象,并在检测到新对象时执行特定操作。下面是一个示例Lambda函数,可在将VPC流日志文件传输到S3桶时解密加密的文件并执行其他操作。
from __future__ import print_function
import json
import urllib
import boto3
import gzip
import StringIO
print('Loading function')
s3 = boto3.client('s3')
def lambda_handler(event, context):
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8'))
try:
response = s3.get_object(Bucket=bucket, Key=key)
# Read the object, uncompress and decrypt the content
file_content = StringIO.StringIO(response['Body'].read())
with gzip.GzipFile(fileobj=file_content) as gzip_reader:
file_content_decrypted = boto3.client('kms').decrypt(CiphertextBlob=gzip_reader.read())['Plaintext']
# Do something with the file_content_decrypted