We attempted to implement an AWS AppSync Events API with Lambda integration using AWS CDK and AWS Powertools for TypeScript. Despite following the documentation, we encountered several issues with event handling and response formatting.
The CDK stack (src/stacks/my-stack.ts
) is correctly configured:
- Lambda function with Node.js 22 runtime
- AppSync Events API with API Key auth
- Direct Lambda integration with REQUEST_RESPONSE invocation type
- Channel namespace "default" with publish/subscribe handlers
Initial implementation assumed direct event handling:
return {
id: eventData.id || Date.now().toString(),
data: eventData,
timestamp: eventData.timestamp || new Date().toISOString(),
processed: true
};
Issue: Created nested payloads in response.
Tried letting the framework handle the wrapping:
return event;
Issue: Still resulted in nested payloads.
Noticed the events come in an array format:
if (!event.events || !Array.isArray(event.events)) {
throw new Error("Invalid event format");
}
return event.events;
Issue: Failed to handle event format correctly.
Discovered events are stringified JSON:
{
"channel": "default/publishChannel",
"events": [
"{\"event_1\":\"data_1\"}",
"{\"event_2\":\"data_2\"}"
]
}
Added JSON parsing:
const parsedEvents = event.events.map((evt: any) => {
if (typeof evt === 'string') {
return JSON.parse(evt);
}
return evt;
});
return parsedEvents;
Issue: Still getting errors in event processing.
The latest implementation attempts to:
- Handle the events array from the request
- Parse stringified JSON events
- Let the framework handle response formatting
However, we're still seeing errors:
{
"failed": [
{
"code": "CustomError",
"identifier": "...",
"index": 0,
"message": "Error - Invalid event format"
}
],
"successful": []
}
- What is the expected format for the Lambda response?
- How should we handle the stringified JSON events - should we parse them or pass them through?
- Is there a specific structure the AppSync Events API expects for successful/failed events?
- Are we correctly using the
AppSyncEventsResolver
from AWS Powertools?
- AWS AppSync Events API Documentation
- AWS Powertools for TypeScript AppSync Events documentation
- CDK AppSync Events API documentation
Need clarification from the development team on:
- The correct event handling flow
- Expected response format
- Best practices for AppSync Events API with Lambda integration