Skip to content

Instantly share code, notes, and snippets.

@GeneralTesler
Created May 25, 2020 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeneralTesler/eb4717b4ccc318243313284c8dfa62b5 to your computer and use it in GitHub Desktop.
Save GeneralTesler/eb4717b4ccc318243313284c8dfa62b5 to your computer and use it in GitHub Desktop.
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