Sub topic 14
Schema registry and discovery
This chapter assumes you can publish events with PutEvents, you know the envelope fields (source, detail-type, detail), and you can run aws CLI commands against a custom bus such as orders-bus.
What the schema registry does
A schema is a formal description of an event's shape: which fields exist, their types, and which are nested. The EventBridge schema registry stores these descriptions so that producers and consumers can agree on what an event looks like without reading each other's code.
Schemas are stored in registries. Two registries exist in every account:
aws.eventsholds schemas for events that AWS services emit. AWS manages it, and you can read it but not change it.discovered-schemasholds schemas that schema discovery creates from your own events.
You can also create your own registries and upload schemas by hand. Schemas use the OpenAPI 3.0 or JSON Schema Draft 4 format.
How discovery works
Schema discovery is a feature you turn on for one event bus. After that, EventBridge inspects the events that arrive on the bus and builds a schema for each distinct combination of source and detail-type. If a later event has a different shape, such as a new field, EventBridge creates a new version of that schema.
Discovery does not change how events are routed. It only observes them.
Enabling discovery
Create a discoverer that points at the bus ARN:
aws schemas create-discoverer \
--source-arn arn:aws:events:us-east-1:123456789012:event-bus/orders-bus \
--description "Discover order event schemas"Publish a few events, then list what was found:
aws schemas list-schemas --registry-name discovered-schemas
aws schemas describe-schema \
--registry-name discovered-schemas \
--schema-name "com.example.orders@OrderPlaced"Discovered schema names follow the pattern <source>@<detail-type>. The describe-schema output includes the schema document, its type, and its version number.
Discovery can take a few minutes to produce a schema after the first event arrives. If list-schemas returns nothing, wait and retry before you debug.
Using schemas in development
Discovered schemas help in three ways:
- They show the real shape of production events, including optional fields you may not have documented.
- They let you write event patterns against fields you know exist.
- They support code bindings, which are generated classes (Java, Python, or TypeScript) that represent an event as a typed object.
You request a binding with put-code-binding and download it with get-code-binding-source:
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 \
output.zipCommon errors and gotchas
- Discovery is not validation. EventBridge does not reject events that differ from a schema. A schema describes what was seen, not what is allowed.
- Schemas drift. A producer that adds or renames a field creates a new schema version. Review versions before you generate bindings.
- Test data pollutes the registry. Discovery records every event on the bus, including the
"status": "test"events from earlier chapters. Use a separate bus for experiments, or delete the stray schemas. - Cost. Discovery is billed by the number of events ingested, after a monthly free allowance. Turn it off on high-volume buses once you have the schemas you need, using
aws schemas delete-discoverer --discoverer-id <id>. - Permissions. Creating discoverers and reading schemas need
schemas:*actions in IAM, which are separate fromevents:*actions.
Exercise
A teammate says the schema in the registry for com.example.orders@OrderPlaced proves that events missing the amount field will be rejected by the bus. Is this correct, and what should you do instead if you want to enforce the shape?
Show answer
It is not correct. Discovered schemas only describe events that EventBridge has already seen, and the bus accepts events regardless of them. To enforce a shape, validate in the producer (for example with the schema or generated binding before calling PutEvents) or in the consumer, or use an event pattern on the rule so that only events with the expected fields reach the target.