AWS AppSync Events with an AWS Lambda OnPublish handler

Client → AWS AppSync Events API → AWS Lambda OnPublish → Subscribers

Validate and enrich events in an AWS Lambda function before AWS AppSync Events delivers them to WebSocket subscribers.

This pattern deploys an AWS AppSync Events API with a direct Lambda integration on the notifications channel namespace. Every event published to a notifications/... channel goes to an AWS Lambda function (REQUEST_RESPONSE) before AppSync broadcasts it to WebSocket subscribers. The function adds processedAt, enriched and messageId fields, and returns an error for a payload that isn't a JSON object.
Use the same hook to validate payloads, redact personal data or add data from another source. A second namespace, alerts, has no handler, so you can compare intercepted and plain delivery.
Publishers and subscribers authenticate with an API key that the stack creates with a 365-day expiry. The stack doesn't output the key, so read it with aws appsync list-api-keys.

< Back to all patterns

GitHub icon Download this pattern (.zip)

GitHub icon View this pattern on GitHub


Clone repo

git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/appsync-events-lambda/typescript/cdk

Deploy

npm installnpx cdk deploy


Testing

Subscribe to /notifications/* in the Pub/Sub Editor of the AWS AppSync console, then publish an event to notifications/general with the HTTP endpoint and the API key. The event arrives with the fields the function added. See the README in the GitHub repo for the commands.

Cleanup

npx cdk destroy
Delete the log group that AWS AppSync creates outside the stack: aws logs delete-log-group --log-group-name /aws/appsync/apis/<ApiId>

Created by:

Nithin Chandran R

Nithin Chandran R

Technical Account Manager at AWS