This is a demo application that consists of an API converting a synchronous request coming from the client into an asynchronous request to the backend using AppSync Events. In order to simulate an asynchronous backend processing, we are using an asynchronous AWS Step Functions workflow, which receives an event with Name and Surname as input, waits 10 seconds and then posts an event with the full name at the AppSync Event channel. While the asynchrnous procesing is execute, the synchronous API subscribes to the AppSync channel in order to be notified when the event arrives there.
1 - The API Gateway makes a synchronous request to Lambda and waits for the response.
2 - Lambda initiates the execution of the asynchronous workflow.
3 - After starting the workflow execution, Lambda connects to AppSync and creates a channel to receive asynchronous notifications (channels are ephemeral and unlimited; in this case, it creates one channel per request using the workflow execution ID).
4 - The workflow executes asynchronously, calling other workflows.
5 - 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 Lambda using the workflow execution ID.
6 - AppSync receives the POST request and sends a notification to the subscriber, which in this case is Lambda.
7 - Lambda receives the message asynchronously, verifies if it was successful, and if so, closes the WebSocket connection with AppSync.
8 - Lambda sends the response to the API Gateway, which has been waiting for the synchronous response. \
This Terraform configuration located in /IaC/terraform
folder creates the necessary AWS infrastructure for an AppSync integration with synchronous and asynchronous capabilities.
The following AWS resources are provisioned:
- Lambda execution role with permissions for:
- Step Functions execution
- CloudWatch Logs management
- Secrets Manager access
- Step Functions execution role with permissions for:
- HTTP endpoint invocation
- EventBridge connection management
- Secrets Manager access
- CloudWatch Logs and X-Ray access
- Python 3.13 runtime
- 3000MB memory allocation
- 40 seconds timeout
- Environment variables for AppSync configuration
- REST API with regional endpoint
- POST method on
/event
resource - Lambda integration
- Development stage with logging enabled
- CloudWatch logging configuration
- API Key authorization for AppSync integration
- Credentials managed through Secrets Manager
- Deployed via CloudFormation template, which is in the
/IaC/cloudFormation
folder (As of 2025-02-02, there's no AppSync Events API terraform resource available yet for creating it via terraform) - Real-time endpoint configuration
- API key authentication
This workflow is only to simulate an async processing. You can substitue it for any other async application, making the necessary adjustment.
- Workflow with the following states:
- Wait state (10 seconds delay)
- JSON conversion
- Name binding
- HTTPS API call to AppSync
Before deploying this infrastructure, ensure you have:
- AWS CLI configured with appropriate credentials
- Terraform installed (version 0.12 or later)
- Required AWS permissions to create all specified resources
The following variables should be configured:
workflow_name
: Name for the Step Functions state machinefunction_name
: Name for the Lambda functionlambda_file
: Path to the Lambda function code
Access the IaC folder:
cd IaC/terraform
Initialize Terraform:
terraform init
Review the planned changes:
terraform plan -out plan
Apply the configuration:
terraform apply plan
The deployment provides several important outputs:
-
lambda_function_arn
: ARN of the created Lambda function -
state_machine_arn
: ARN of the Step Functions state machine -
api_endpoint
: URL endpoint for the API Gateway -
connection_arn
: ARN of the EventBridge connection -
appsync_host
: AppSync API endpoint -
appsync_host_realtime
: AppSync real-time endpoint -
secretsmanager_arn
: ARN of the created Secrets Manager secret
After deploying the solution, you just need to send a HTTP Request:
curl --location api_endpoint`/event' \
--header 'Content-Type: application/json' \
--data '{
"nome": "Ricardo",
"sobrenome": "Marques"
}'
Wait some seconds (the workflow has a wait step with 10 seconds) and receive the response:
{"id": "exec-38ecfe24-32d2-443c-813f-97d797a8a2f9", "nome completo": "Ricardo Marques"}
To remove all created resources:
terraform destroy -auto-approve
-
API Gateway endpoints are publicly accessible
-
Lambda function has restricted IAM permissions
-
Secrets are managed through AWS Secrets Manager
-
All sensitive data is encrypted at rest
-
API authentication is handled via API keys