Created
May 25, 2020 21:59
-
-
Save GeneralTesler/eb4717b4ccc318243313284c8dfa62b5 to your computer and use it in GitHub Desktop.
Log AWS API calls using boto3 event system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/events.html#provide-client-params | |
import boto3 | |
from botocore.client import Config | |
from botocore import UNSIGNED | |
import json | |
def intercept_params(params, **kwargs): | |
print( | |
json.dumps( | |
{ | |
"service": kwargs.get("event_name").split(".")[-2], | |
"operation": kwargs.get("event_name").split(".")[-1], | |
"params": params, | |
"region": kwargs.get("context")["client_region"], | |
}, | |
indent=4, | |
) | |
) | |
if __name__ == "__main__": | |
client = boto3.client("s3", config=Config(signature_version=UNSIGNED)) | |
client.meta.events.register("provide-client-params.*.*", intercept_params) | |
client.head_bucket(Bucket="flaws.cloud") | |
""" | |
expected output from intercept_params: | |
{ | |
"service": "s3", | |
"operation": "HeadBucket", | |
"params": { | |
"Bucket": "flaws.cloud" | |
}, | |
"region": "us-east-1" | |
} | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment