Defining WebSocket API Gateway Endpoints in a SAM Template

David Sandor
Build Succeeded
Published in
4 min readFeb 27, 2019

--

In a previous post I walked through creating a WebSocket based API Gateway Endpoint. At that time I had not quite figured out how to fully define the endpoint in a SAM Template. Well here you go.

In this example we are setting up a WebSocket API Gateway endpoint that has a route called test and I included a Request Authorizer Lambda because it was a bit tricky to get that all set up and working. If you do not need an authorizer simply remove it from the template.yaml.

Here is a link to the full code in github.
https://github.com/dsandor/example-ws-gateway-sam

AWS Console showing the WebSocket API Gateway we are defining in this article.

Above is what you will end up with after the SAM template is deployed. Take a look at the full template.yaml file here: https://github.com/dsandor/example-ws-gateway-sam/blob/master/template.yaml

There are two Lambda Functions defined in this template.

  • MyLambdaRouteHandlerFunction
  • LambdaRequestAuthFunction

The MyLambdaRouteHandlerFunction is a really simple function that just returns back a simple message.

module.exports.handler = async (event) => {
console.log(JSON.stringify(event, 2));
const { send } = getSocketContext(event);

await send(JSON.stringify({…

--

--