Skip to content

Commit 1c37a12

Browse files
Adding documentation
1 parent fcdad4c commit 1c37a12

7 files changed

+54
-26
lines changed

docs/core/event_handler/appsync_events.md

+15-9
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,28 @@ You must have an existing AppSync Events API with real-time capabilities enabled
6767

6868
AppSync Events uses a specific event format for Lambda requests and responses. In most scenarios, Powertools simplifies this interaction by automatically formatting resolver returns to match the expected AppSync response structure.
6969

70-
=== "appsync_payload_request.json"
70+
=== "payload_request.json"
7171

7272
```python hl_lines="5 10 12"
73-
--8<-- "examples/event_handler_appsync_events/src/appsync_payload_request.json"
73+
--8<-- "examples/event_handler_appsync_events/src/payload_request.json"
7474
```
7575

76-
=== "appsync_payload_response.json"
76+
=== "payload_response.json"
7777

7878
```python hl_lines="5 10 12"
79-
--8<-- "examples/event_handler_appsync_events/src/appsync_payload_response.json"
79+
--8<-- "examples/event_handler_appsync_events/src/payload_response.json"
8080
```
8181

82-
=== "appsync_payload_response_with_error.json"
82+
=== "payload_response_with_error.json"
8383

8484
```python hl_lines="5 10 12"
85-
--8<-- "examples/event_handler_appsync_events/src/appsync_payload_response_with_error.json"
85+
--8<-- "examples/event_handler_appsync_events/src/payload_response_with_error.json"
86+
```
87+
88+
=== "payload_response_fail_request.json"
89+
90+
```python hl_lines="5 10 12"
91+
--8<-- "examples/event_handler_appsync_events/src/payload_response_fail_request.json"
8692
```
8793

8894
#### Events response with error
@@ -177,12 +183,12 @@ When processing items individually with `aggregate=False`, you can raise an exce
177183

178184
#### Handling errors with batch of items
179185

180-
When processing batch of items with `aggregate=False`, you can must format the payload according the expected response.
186+
When processing batch of items with `aggregate=True`, you must format the payload according the expected response.
181187

182-
=== "working_with_error_handling.py"
188+
=== "working_with_error_handling_multiple.py"
183189

184190
```python hl_lines="5 6 13"
185-
--8<-- "examples/event_handler_appsync_events/src/working_with_error_handling.py"
191+
--8<-- "examples/event_handler_appsync_events/src/working_with_error_handling_multiple.py"
186192
```
187193

188194
=== "working_with_error_handling_response.json"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"error": "Exception - An exception occurred"
3+
}
4+

examples/event_handler_appsync_events/src/working_with_aggregated_events.py

+4-17
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,10 @@ def handle_default_namespace_batch(payload_list: list[dict[str, Any]]):
1515

1616
# Process all events in the batch together
1717
for event in payload_list:
18-
try:
19-
# Process each event
20-
processed_event = process_event(event)
21-
results.append(processed_event)
22-
except Exception as e:
23-
# Handle errors for individual events
24-
results.append({
25-
"error": str(e),
26-
"id": event.get("id"),
27-
})
28-
29-
return {
30-
"events": results,
31-
}
32-
33-
def process_event(event):
34-
return {"payload": event}
18+
# Process each event
19+
results.append({"id": event.get("id"), "payload": {"processed": True, "originalEvent": event}})
20+
21+
return results
3522

3623
def lambda_handler(event: dict, context: LambdaContext):
3724
return app.resolve(event, context)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, Any
4+
5+
from aws_lambda_powertools.event_handler import AppSyncEventsResolver
6+
7+
if TYPE_CHECKING:
8+
from aws_lambda_powertools.utilities.typing import LambdaContext
9+
10+
app = AppSyncEventsResolver()
11+
12+
@app.on_publish("/default/*", aggregate=True)
13+
def handle_default_namespace_batch(payload_list: list[dict[str, Any]]):
14+
results: list = []
15+
16+
# Process all events in the batch together
17+
for event in payload_list:
18+
try:
19+
# Process each event
20+
results.append({"id": event.get("id"), "payload": {"processed": True, "originalEvent": event}})
21+
except Exception as e:
22+
# Handle errors for individual events
23+
results.append({
24+
"error": str(e),
25+
"id": event.get("id"),
26+
})
27+
28+
return results
29+
30+
def lambda_handler(event: dict, context: LambdaContext):
31+
return app.resolve(event, context)

0 commit comments

Comments
 (0)