Untitled

mail@pastecode.io avatar
unknown
python
a year ago
2.2 kB
6
Indexable
import boto3
import json

def test_event_pattern(event_pattern, event):
    # Create an EventBridge client
    client = boto3.client('events')

    # Call the test-event-pattern API
    response = client.test_event_pattern(
        Event=json.dumps(event),
        EventPattern=json.dumps(event_pattern)
    )

    return response['Result']

if __name__ == "__main__":
    # Example event pattern and event
    event_pattern = {
        "detail": {
            "eventName": [{ "equals-ignore-case": "PutObject" }],
        }
    }

    event = {
      "version": "0",
      "id": "6a7e8f6e-0ad7-4c3d-b0ac-00d6c0a5042d",
      "detail-type": "AWS API Call via CloudTrail",
      "source": "aws.s3",
      "account": "123456789012",
      "time": "2023-09-07T08:07:59Z",
      "region": "us-west-2",
      "resources": ["arn:aws:s3:::mybucket/mykey"],
      "detail": {
        "eventVersion": "1.07",
        "userIdentity": {
          "type": "IAMUser",
          "principalId": "EXAMPLEID",
          "arn": "arn:aws:iam::123456789012:user/Alice",
          "accountId": "123456789012",
          "accessKeyId": "EXAMPLEKEY",
          "userName": "Alice"
        },
        "eventTime": "2023-09-07T08:07:59Z",
        "eventSource": "s3.amazonaws.com",
        "eventName": "PutObject",
        "awsRegion": "us-west-2",
        "sourceIPAddress": "127.0.0.1",
        "userAgent": "aws-sdk-python/1.17.12 Python/3.7.3 Linux/4.14.77-70.82.amzn1.x86_64 botocore/1.12.123",
        "requestParameters": {
          "bucketName": "mybucket",
          "key": "mykey"
        },
        "s3": {
          "s3SchemaVersion": "1.0",
          "configurationId": "ID",
          "bucket": {
            "name": "mybucket",
            "ownerIdentity": {
              "principalId": "EXAMPLEID"
            },
            "arn": "arn:aws:s3:::mybucket"
          },
          "object": {
            "key": "mykey",
            "size": 1024,
            "eTag": "0123456789abcdef0123456789abcdef",
            "versionId": "1",
            "sequencer": "0A1B2C3D4E5F678901"
          }
        }
      }
    }


    result = test_event_pattern(event_pattern, event)
    print("Event matches pattern:", result)