Sub topic 14
Schema registry and discovery
Earlier chapters showed how to publish events, match them with patterns, and reshape them with input transformers. All of that depends on knowing what the events look like. In a small system the producer's author can tell you. Across many teams, accounts, and AWS services, someone has to write the shape down. The EventBridge schema registry does that. It stores descriptions of event structure and can learn them from live traffic.
This chapter assumes you have worked with custom buses, PutEvents, and event patterns from earlier chapters, and that you can read JSON Schema or OpenAPI.
What a schema is in EventBridge
A schema is a document that describes the full event envelope, including source, detail-type, and the structure of detail. Schemas live in registries, which are named containers. Each schema has numbered versions, so a change to the event shape creates a new version instead of overwriting the old one.
EventBridge supports two schema formats:
- OpenAPI 3.0, which is what discovery produces.
- JSON Schema Draft 4, which you can use for schemas you write yourself.
There are three kinds of registry:
| Registry | Contents | Who writes to it |
|---|---|---|
| `aws.events` | Schemas for events emitted by AWS services, such as EC2 state changes | AWS |
| `discovered-schemas` | Schemas inferred from events on buses where discovery is on | The discoverer |
| Custom registry | Schemas you author or upload | You |
The aws.events registry is useful before you have written any code. If you want a rule for an AWS service event, read its schema to find the exact detail field names.
How discovery works
Schema discovery is a feature you switch on for one event bus. A resource called a discoverer is attached to that bus. It samples the events that arrive and infers a schema from each combination of source and detail-type. The inferred schema is saved in the discovered-schemas registry.
Discovered schemas are named <source>@<detail-type>. For example, events with source com.example.orders and detail-type OrderPlaced become com.example.orders@OrderPlaced. When later events show a different structure, such as a new field, the discoverer records a new version of the schema.
The dotted line matters. The discoverer observes the bus and does not sit in the delivery path. Turning discovery on or off does not change how rules match or how targets receive events.
Enabling discovery
You need the ARN of the bus, then you create a discoverer for it. The schemas service namespace holds all registry commands in the AWS CLI.
aws schemas create-discoverer \
--source-arn arn:aws:events:us-east-1:123456789012:event-bus/orders-bus \
--description "Learn order event schemas"The response includes a DiscovererId and a State, normally STARTED. Publish a few events with the PutEvents calls from the earlier chapter, then list what was learned:
aws schemas list-discoverers
aws schemas list-schemas --registry-name discovered-schemas
aws schemas describe-schema \
--registry-name discovered-schemas \
--schema-name "com.example.orders@OrderPlaced"Discovery is not instant, so schemas can take a few minutes to appear. describe-schema returns the schema as a JSON string in the Content field, along with SchemaVersion and LastModified.
Use stop-discoverer and start-discoverer with --discoverer-id to pause and resume. Use delete-discoverer to remove it. Deleting a discoverer does not delete the schemas it already saved.
Reading a discovered schema
A schema for OrderPlaced has two parts. The first is an AWSEvent object that describes the envelope. It carries the source and detail-type as the vendor extensions x-amazon-events-source and x-amazon-events-detail-type. The second is a named object for the detail payload. Here is a trimmed version of that structure:
{
"openapi": "3.0.0",
"info": { "version": "1.0.0", "title": "OrderPlaced" },
"paths": {},
"components": {
"schemas": {
"AWSEvent": {
"type": "object",
"required": ["detail-type", "resources", "detail", "id", "source", "time", "region", "version", "account"],
"x-amazon-events-detail-type": "OrderPlaced",
"x-amazon-events-source": "com.example.orders",
"properties": {
"detail": { "$ref": "#/components/schemas/OrderPlaced" },
"detail-type": { "type": "string" },
"source": { "type": "string" },
"id": { "type": "string" },
"account": { "type": "string" },
"region": { "type": "string" },
"time": { "type": "string", "format": "date-time" },
"version": { "type": "string" },
"resources": { "type": "array", "items": { "type": "string" } }
}
},
"OrderPlaced": {
"type": "object",
"properties": {
"orderId": { "type": "string" },
"amount": { "type": "number" },
"status": { "type": "string" }
}
}
}
}
}Use this to write patterns. The OrderPlaced object shows that amount is a number, so a numeric matcher such as { "numeric": [">=", 100] } is valid. It also shows that status is a string, so anything-but works on it.
Authoring a schema yourself
Discovery only knows what it has seen. If a field appears rarely, or a new event type has not been published yet, the inferred schema is incomplete. For a contract that you want to control, write the schema and put it in a custom registry.
aws schemas create-registry \
--registry-name orders-contracts \
--description "Reviewed event contracts for the orders domain"
aws schemas create-schema \
--registry-name orders-contracts \
--schema-name "com.example.orders@OrderPlaced" \
--type OpenApi3 \
--content file://order-placed-schema.jsonHere order-placed-schema.json holds a document like the one above. Use --type JSONSchemaDraft4 if you prefer to write plain JSON Schema. Keeping the source@detail-type naming makes your authored schema line up with what discovery would produce for the same event.
A practical workflow is to let discovery run in a development account, copy the discovered schema, tighten it by hand (mark required fields, fix types), and publish it to a custom registry as the reviewed contract.
Generating code bindings
Schemas can generate typed classes so producers and consumers do not hand-write the event structure. EventBridge supports bindings for Java, Python, TypeScript, and Go. You request a binding, then download it:
aws schemas put-code-binding \
--registry-name discovered-schemas \
--schema-name "com.example.orders@OrderPlaced" \
--language Python36
aws schemas get-code-binding-source \
--registry-name discovered-schemas \
--schema-name "com.example.orders@OrderPlaced" \
--language Python36 \
order-placed-binding.zipThe last argument is the output file. The command writes a ZIP archive there. Use describe-code-binding to check that generation has finished before you download. The same bindings are available in IDE toolkits, which is where many developers use them.
Gotchas
The registry describes events and does not police them. Keep these points in mind.
- Schemas are not enforced.
PutEventsaccepts any valid JSON inDetail, whether or not a schema exists for it. A producer that sends"amount": "250"(a string) is not rejected. The event is only a poor match for patterns that expect a number. - Discovered schemas reflect observed traffic. A bad event from a buggy producer can end up in a schema version. Review discovered schemas before treating them as contracts.
- Discovery captures event content. The sampled
detailis analyzed, so if events carry sensitive data, check your data-handling rules before enabling discovery on that bus. The stored schema holds structure and types, not values. - Discovery is per bus. Enabling it on
orders-bussays nothing about other buses, including the default bus. - Discovery has a cost. Ingested events are billed beyond a free allowance, so check current pricing before leaving it on a busy production bus. Many teams enable it in development, collect schemas, and turn it off.
- Schema changes are not automatic notifications. A new version appears in the registry, but no rule or consumer is told. Compare versions with
describe-schema --schema-versionwhen you need to see what changed.
Putting it to use
A reasonable approach for a team starting out looks like this:
- Enable discovery on a development bus.
- Exercise the producers so every event type is published at least once.
- Review the discovered schemas and copy the ones you want to keep into a custom registry.
- Turn discovery off, or limit it to non-production, to control cost.
- Generate code bindings from the reviewed schemas and share them with producers and consumers.
Exercise
A teammate enabled discovery on orders-bus and then published an OrderPlaced event where amount was the string "250". The schema in discovered-schemas now shows amount as a string. Will a rule with the pattern {"detail": {"amount": [{"numeric": [">=", 100]}]}} match that event, and does the registry reject the event?
Show answer
The rule will not match, because the numeric matcher applies only to JSON numbers and "250" is a string. The registry does not reject the event. PutEvents accepts it, and the discoverer records the observed structure as a new schema version. The fix is to correct the producer and use a reviewed schema in a custom registry as the contract.
Exercise
What name does discovery give to the schema for events with source com.example.billing and detail-type InvoicePaid, and which registry holds it?
Show answer
The schema is named com.example.billing@InvoicePaid and is stored in the discovered-schemas registry.