Hey guys, is there an easy way to create a Websock...
# help
h
Hey guys, is there an easy way to create a Websocket API using any of the existing constructs?
f
Hey @Harry Collin 👋
Not right now, but you can use this CDK construct and hook it up with
sst.Function
https://docs.aws.amazon.com/cdk/api/latest/docs/aws-apigatewayv2-readme.html#websocket-api
What’s your timeline on this? I can add a
sst.WebSocketApi
construct by later this week if that works for u.
h
Hey @Frank, thanks for this. I actually managed to implement it similarly to how you described. The only part the is a little annoying to deal with was the permissions. If you think a construct is required for SST then I'd be supportive!
f
Can you share a snippet of the permissions you put together?
I’ll take a look and see how we can simplify it.
h
I'll share the whole stack. Might be easier
f
Nice! Let me put something together 💪
Hey @Harry Collin There’s now an
sst.WebSocketApi
construct in v0.11.0 You can change the API definition to this, and remove all the permission code.
Copy code
new sst.WebSocketApi(this, "websocket-api", {
  defaultFunctionProps: {
    environment: {
      WEBSOCKET_CONNECTIONS_TABLE: table.dynamodbTable.tableName,
      AUDIENCE: process.env.AUDIENCE!,
      TOKEN_ISSUER: process.env.TOKEN_ISSUER!,
      JWKS_URI: process.env.JWKS_URI!
    },
    permissions: [table],
  },
  routes: {
    $connect: "src/websockets/handlers/ConnectHandler.main",
    $disconnect: "src/websockets/handlers/DisconnectHandler.main",
    $default: "src/websockets/handlers/DefaultHandler.main",
  },
});
More examples here - https://docs.serverless-stack.com/constructs/WebSocketApi
h
@Frank amazing! Thank you 🙂
Hey @Frank, I implemented this but haven't been able to deploy. I'm getting this error
CloudWatch Logs role ARN must be set in account settings to enable logging (Service: AmazonApiGatewayV2; Status Code: 400; Error Code: BadRequestException;
. Any ideas?
f
do you see a helper message near the bottom that describes how to fix it?
The issue is that the AWS API Gateway service in your AWS account does not have permissions to the CloudWatch logs service. Follow this article to create an IAM role for logging to CloudWatch - https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-cloudwatch-logs/
And then try deploy again
h
Hey @Frank, I did see this message but wondered if it was a problem with the implementation. I didn't have this message when using the stack I sent you. What changed in your implementation?
f
Ah, if you use the CDK’s WebSocketApi, access log is not enabled by default.
sst.WebSocketApi
by default enables it. If you set
accessLog: false
, you shouldn’d run into this issue.
h
@Frank thank you!