Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save garysassano/36447259324d325ef1bb2dad44e698fc to your computer and use it in GitHub Desktop.
Save garysassano/36447259324d325ef1bb2dad44e698fc to your computer and use it in GitHub Desktop.
Event Api attempts

AppSync Events API Implementation Debug Log

Overview

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.

Infrastructure Setup

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

Implementation Attempts

Attempt 1: Basic Event Handler

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.

Attempt 2: Framework-handled Wrapping

Tried letting the framework handle the wrapping:

return event;

Issue: Still resulted in nested payloads.

Attempt 3: Events Array Processing

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.

Attempt 4: Stringified JSON Handling

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.

Current State

The latest implementation attempts to:

  1. Handle the events array from the request
  2. Parse stringified JSON events
  3. Let the framework handle response formatting

However, we're still seeing errors:

{
  "failed": [
    {
      "code": "CustomError",
      "identifier": "...",
      "index": 0,
      "message": "Error - Invalid event format"
    }
  ],
  "successful": []
}

Questions for Developers

  1. What is the expected format for the Lambda response?
  2. How should we handle the stringified JSON events - should we parse them or pass them through?
  3. Is there a specific structure the AppSync Events API expects for successful/failed events?
  4. Are we correctly using the AppSyncEventsResolver from AWS Powertools?

Relevant Documentation

Next Steps

Need clarification from the development team on:

  1. The correct event handling flow
  2. Expected response format
  3. Best practices for AppSync Events API with Lambda integration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment