Setup a basic WebSocket mock in AWS ApiGateway
I've automated the steps described by Woodrow using CloudFormation so now anyone can reproduce it more easily. The template can be found here: https://gist.github.com/maatthc/9d2dfe0448733f0ee1624d658fbac80f . To create a new stack using it you should:
- Download the template to your machine.
- Head to Cloudformation in the AWS Console (https://ap-southeast-2.console.aws.amazon.com/cloudformation/) - Change your AWS Region.
- Click in "Create Stack"
- Choose "Upload a template file" and use the template you've downloaded.
- Click Next, give the stack a name and click Next again.
- Click Next and "Create Stack".
I hope it helps.
Cheers
Using the following sources (below), I was able to get a quick and loose example AWS API Gateway connection working use MOCK endpoints with web sockets. I'm sure this can be flushed out more to provide an even better understanding of different request/response body messages and codes..
- First, go to API Gateway, click Create API
- For Choose the protocol, select WebSocket
- Give the API a name, and use the example
$request.body.message
for the route selection expression - Click the Create API button
- Select the new API from the side pane, and click Routes
- Select the
$connect
route - Under Route overview, select the Mock radio button for endpoint
- For the Request Template, enter an expression (using 200 for example).
- For Template Key, enter a key (using 200 for example)
- For the popup window on passthrough behavior, select No, Use Current Settings for now
Click on the Template Key
200
, and enter a template:{"statusCode" : 200}
and then click the Save button.Select Route Overview to go back to the
$connect
overview page and then select the Add Integration Response buttonCreate a response key of $default, which will reference the
$default
route under RoutesFor the
$disconnect
route, please repeat steps 7 - 13 for this specific route.- Once done setting up the
$disconnect
route, please select the$default
route under the Routes pane. - Create an Integration Request the same way that was done for
$connect
and$disconnect
(steps 7 - 11) - For Integration Response under
$default
, we will be setting the route response up slightly different than$connect
and$disconnect
as those 2 routes' responses actually reference the$default
response within their own respective Integration Responses. - Create a response key of $default
- Enter a Template Selection Expression (using 200 for this example)
- Then create a Template Key (using 200 for this example)
- Click on the Template Key
200
, and enter a template:{"statusCode" : 200, "connectionId" : "$context.connectionId"}
and then click the Save button. {"connectionId" : "$context.connectionId"}
was added to the response payload to show how variables can be used within the response to represent an actual value of the connection.- Finally, under the Actions dropdown, select Deploy API, and enter a deployment stage name. This should generate a WebSocket URL that can now be connected to e.g. (
wss://#######.execute-api.us-east-1.amazonaws.com/Test
) - Using
wscat
via cli, I am now able to connect to my WebSocket URL (see source below for installing wscat):wscat -c wss://#######.execute-api.us-east-1.amazonaws.com/Test
Sources:
https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-how-to-call-websocket-api-wscat.html
https://forums.aws.amazon.com/thread.jspa?threadID=299642&tstart=0