Sub topic 4
Publishing custom events with PutEvents
This chapter assumes you know the AWS CLI, JSON, and the basic event envelope (source, detail-type, detail). It also assumes you know what an event bus is. You will publish a custom event to a custom bus and learn how to detect partial failures.
What PutEvents does
PutEvents is the API call a producer uses to send events to an event bus. One call can carry up to 10 entries, and the total size of all entries must stay under 256 KB. Each entry becomes one event on the bus. EventBridge then evaluates the event against the rules on that bus.
Each entry takes these fields:
| Field | Required | Meaning |
|---|---|---|
| `Source` | Yes | Who produced the event, for example `com.example.orders` |
| `DetailType` | Yes | What kind of event it is, for example `OrderPlaced` |
| `Detail` | Yes | A JSON document, passed as a string |
| `EventBusName` | No | Target bus name or ARN. If you omit it, the event goes to the default bus |
| `Resources` | No | ARNs of resources the event concerns |
| `Time` | No | Event timestamp. If omitted, EventBridge uses the time it received the event |
The most common mistake is leaving out EventBusName. The call succeeds, but the event lands on the default bus, where your custom-bus rules never see it.
Publish with the CLI
Create a custom bus, then send one event to it.
aws events create-event-bus --name orders-bus
aws events put-events --entries '[{"Source":"com.example.orders","DetailType":"OrderPlaced","Detail":"{\"orderId\":\"1001\",\"amount\":250,\"status\":\"new\"}","EventBusName":"orders-bus"}]'Notice that Detail is a string containing JSON, so its inner quotes are escaped. If Detail is not valid JSON, the entry fails.
A successful response looks like this:
{
"FailedEntryCount": 0,
"Entries": [
{ "EventId": "11710aed-b79e-4468-a20b-bb3c0c3b4860" }
]
}Publish with boto3
In application code, build Detail with json.dumps so you do not have to escape anything by hand.
import json
import boto3
events = boto3.client("events")
response = events.put_events(
Entries=[
{
"Source": "com.example.orders",
"DetailType": "OrderPlaced",
"Detail": json.dumps({"orderId": "1001", "amount": 250, "status": "new"}),
"EventBusName": "orders-bus",
}
]
)
if response["FailedEntryCount"] > 0:
for entry in response["Entries"]:
if "ErrorCode" in entry:
print(entry["ErrorCode"], entry["ErrorMessage"])Partial failure
PutEvents can return HTTP 200 while some entries failed. The call as a whole succeeds, and each entry is accepted or rejected on its own. A failed entry has ErrorCode and ErrorMessage instead of EventId. Entries in the response appear in the same order as the request, so you can match failures to inputs by index.
Common causes of entry failure:
InvalidArgument: a required field is missing orDetailis not valid JSON.ThrottlingException: you exceeded the account's PutEvents quota in that Region. Retry with backoff.InternalFailure: a transient service error. Retry.
Permissions
The caller needs events:PutEvents on the target bus. For a custom bus, scope the resource to its ARN, such as arn:aws:events:us-east-1:123456789012:event-bus/orders-bus.
Exercise
A producer sends a batch of 10 events with PutEvents. The call returns HTTP 200 with FailedEntryCount of 2. What should the producer do, and why should it not resend the whole batch?
Show answer
It should find the entries that contain an ErrorCode, matching by position in the response, and retry only those two, with backoff for throttling. Resending all 10 would publish the 8 accepted events a second time and create duplicates.