# Amazon API Gateway Websocket API to Amazon SQS to AWS Lambda This pattern creates an Amazon API Gateway WebSocket API which sends inbound messages to an Amazon SQS FIFO queue. The queue is processed by an AWS Lambda function to return a result. The queue acts as a buffer to alleviate traffic spikes and ensure your workload can sustain the arriving load by buffering all the requests durably. It also helps downstream consumers to process the incoming requests at a consistent pace. A FIFO (First-In-First-Out) queue is used to ensure that evens are processed in order of arrival. Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-websocket-sqs-lambda. Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. ## Requirements * [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. * [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured * [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) * [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed ## Deployment Instructions 1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: ``` git clone https://github.com/aws-samples/serverless-patterns ``` 1. Change directory to the pattern directory: ``` cd apigw-websocket-api-sqs-lambda ``` 1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: ``` sam deploy --guided ``` 1. During the prompts: * Enter a stack name * Enter the desired AWS Region * Allow SAM CLI to create IAM roles with the required permissions. Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults. 1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. ## How it works The API Gateway handles websocket message events by putting the message into a SQS FIFO (First-In-First-Out) queue. The SQS FIFO queue is used to ensure processing order of the websocket messages. The message payload must contain a "MessageGroupId" field which is used by SQS FIFO queue. If message order is not required, a standard SQS queue can also be used and the message payload would not require the "MessageGroupId" field. The API Gateway websocket request integration passes the connectionId and requestId as metadata and the message body as the payload. The Lambda function can now process the SQS FIFO requests in a batch manner (10 requests at a time) and send a response back to the websocket connection. ## Testing Once the application is deployed, retrieve the WebsocketURI value from CloudFormation Outputs. To test the Websocket API, you can use [wscat](https://github.com/websockets/wscat) which is an open-source command line tool. 1. [Install NPM](https://www.npmjs.com/get-npm). 1. Install wscat: ``` $ npm install -g wscat ``` 1. Connect to your WebSocketURL by executing the following command: ``` $ wscat -c ``` 1. To test the SQS FIFO queue, send a JSON-formatted request like the following example. Note the MessageGroupId field which is required for the SQS FIFO queue. The SQS queue will be processed by the Lambda function which sends back a response: ``` $ wscat -c connected (press CTRL+C to quit) > {"MessageGroupId":"test", "data":"hello world"} < {"connectionId":"","requestId":"","message":"{\"MessageGroupId\":\"test\",\"data\":\"hello world\"}"} ``` ## Cleanup 1. Delete the stack ```bash aws cloudformation delete-stack --stack-name STACK_NAME ``` 1. Confirm the stack has been deleted ```bash aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus" ``` ## Reference - [Working with WebSocket APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api.html) - [Setting up a WebSocket API integration request in API Gateway ](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-integration-requests.html) - [Amazon SQS FIFO (First-In-First-Out) queues](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/FIFO-queues.html) - [AWS Lambda - the Basics](https://docs.aws.amazon.com/whitepapers/latest/serverless-architectures-lambda/aws-lambdathe-basics.html) - [Lambda Function Handler](https://docs.aws.amazon.com/whitepapers/latest/serverless-architectures-lambda/the-handler.html) - [Function Event Object - Overview](https://docs.aws.amazon.com/whitepapers/latest/serverless-architectures-lambda/the-event-object.html) - [Function Environment Variables](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html) ---- Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: MIT-0