AWS Compute Blog
Modernizing applications with AWS AppSync Events
In today’s fast-paced digital world, organizations are facing challenges for modernizing their applications. A common problem is the smooth shift from synchronous to asynchronous communication without substantial client or frontend alterations. When modernizing applications, it is often necessary to move from a synchronous communication model to an asynchronous one. However, this transition can be complex, especially when the client or frontend communicates synchronously. Adapting the current code for asynchronous communication demands significant time and resources.
AWS AppSync Events helps address this challenge by enabling you to build event-driven APIs that can bridge between synchronous and asynchronous communication models. With AppSync Events, you can modernize your backend architecture to leverage asynchronous patterns while maintaining compatibility with existing synchronous clients.
Overview
The solution comprises an API that converts client synchronous requests to asynchronous backend requests using AppSync Events.
For demonstrating the integration between the API and the backend, I’m simulating the backend processing using an asynchronous AWS Step Functions workflow. This workflow receives a Name and Surname event, waits 10 seconds, and posts a full-name event to the AppSync Event channel. To receive event notifications, the API subscribes to the AppSync channel. At the same time, the backend handles events asynchronously.

Figure 1: Representation of an API integrating a synchronous frontend with an asynchronous backend using AWS AppSync Events.
- The Amazon API Gateway makes a synchronous request to AWS Lambda and waits for the response.
- Lambda function starts the execution of the asynchronous workflow.
- After starting the workflow execution, Lambda connects to AppSync and creates a channel to receive asynchronous notifications (channels are ephemeral and unlimited. Here it creates one channel per request using the workflow execution ID).
- The workflow executes asynchronously, calling other workflows.
- Upon completion of the main workflow, it sends a POST request to the AppSync events API with the processing result. The POST is made to the channel that was created by the Lambda function using the workflow execution ID.
- AppSync receives the POST request and sends a notification to the subscriber, which in this case is the Lambda function. The entire process must be finished within the Lambda functions’s timeout limit you defined.
- Lambda sends the response to the API Gateway, which has been waiting for the synchronous response.
To better understand the Event API WebSocket Protocol used in this solution, refer to this AppSync documentation.
You can access the GitHub repo through this link: AppSync_Sync_Async_Integration.
The repository includes a comprehensive README file that walks you through the process of setting up and configuring the preceding solution.
Prerequisites
To follow this walkthrough, you need the following prerequisites:
- An AWS account
- Necessary AWS permissions to create all specified resources
- AWS Command Line Interface (AWS CLI) configured with appropriate credentials
- Terraform installed (version 0.12 or later)
With the full code, including API Gateway and Step Functions, on GitHub, this post only covers the core components: the AppSync Events API and the Lambda function.
Walkthrough
The following steps walk you through this solution.
Creating an AppSync event API with API Key Authorization
An AppSync Event API allows calls using API key, Amazon Cognito user pools, Lambda authorizer, OIDC, or AWS identity and Access Management (IAM). This solution uses API Key.
The infrastructure as code (IaC) has been created using Terraform. However, as of writing this post, there weren’t Terraform AppSync Event API resource available. Therefore, the AppSync Event API resources were made with AWS CloudFormation, which is imported and implemented by Terraform.
In the resource AWS:AppSync:Api, define the API name and Auth method:
To have the Host DNS, Realtime Endpoint, and Secret Manager created referenced by the Terraform template, output them:
The key information needed from the AppSync Event API is:
- Host DNS: This DNS is used to send events to the API Channel through HTTP Post requests.
- Realtime endpoint: This endpoint is a WebSocket endpoint where the Lambda function connects to receive the events posted in the AppSync Channel.
- API Key: This key is used not only in the Post HTTP requests, but also to connect and subscribe to the AppSync channel.
Lambda Sync/Async API
In this solution, the Lambda function runs two tasks:
- Start an asynchronous workflow
- Subscribe to an event channel through WebSocket
To handle the WebSocket connection, use the websocket-client lib, which is a powerful Python lib developed for working with WebSockets.
Request isolation is maintained by using the same UUID for workflow name and AppSync channel name.
First, to initialize the WebSocket Connection, the subprotocols must be defined:
- WEBSOCKET_PROTOCOL
- Headers:
- Host: The AppSync Host DNS (even with a WebSocket Connection, the HTTP Host must be sent)
- x-api-key: The API key create fot the Event API.
- Sec-Websocket-Protocol: WEBSOCKET_PROTOCOL
Once the WebSocket connection is established, a first message with the type CONNECTION_INIT_TYPE
must be sent.
To subscribe to the channel by which our function is notified when the Step Functions workflow finishes, send a second message with the type SUBSCRIBE_TYPE, an ID, the channel name and authorization.
For more information about types of message, read this AppSync documentation.
After receiving the message confirming the subscription, wait for messages with the type data
. Whenever a message with this type arrives, execute the logic to identify if the workflow was successfully executed, and then close the connection.
Conclusion
In this post, you learned how to use event-driven architectures and the capabilities of AWS AppSync Events to integrate synchronous and asynchronous communication patterns in your applications. This allows you to modernize your systems without the need for extensive modifications to your existing frontend codebase. Explore the demonstrations and documentation provided in the GitHub repository to gain a deeper understanding of how AppSync Events can be applied to your specific use cases.
To learn more about serverless architectures and asynchronous invocation patterns, see Serverless Land.