Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Log AWS API calls using boto3 event system
# 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