Search code examples
websocketbinaryaws-api-gatewayserverless-framework

Serverless Framework API Gateway Websockets Binary Data


Anyone know how to set a websocket's content handling strategy to binary in the serverless framework?

I have a websocket defined as follows:

  my_websocket:
    handler: src/handler.handler
    description: My websocket
    events:
      - websocket:
          route: $default
      - websocket:
          route: $connect
      - websocket:
          route: $disconnect

My handler expects binary data

https://docs.aws.amazon.com/apigateway/latest/developerguide/websocket-api-develop-binary-media-types.html

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apitgateway-method-integration.html

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-integrationresponse.html

I need to set my ContentHandling or ContentHandlingStrategy to CONVERT_TO_BINARYbut I'm not sure how I can accomplish this.

Neither of these packages seem to handle websockets:

  • serverless-apigw-binary
  • serverless-apigwy-binary

Setting either of the props (or both) fails:

    events:
      - websocket:
          route: $default
          ContentHandling: CONVERT_TO_BINARY
          ContentHandlingStrategy: CONVERT_TO_BINARY
      - websocket:
          route: $connect
          ContentHandling: CONVERT_TO_BINARY
          ContentHandlingStrategy: CONVERT_TO_BINARY
      - websocket:
          route: $disconnect
          ContentHandling: CONVERT_TO_BINARY
          ContentHandlingStrategy: CONVERT_TO_BINARY

as follows:

Serverless: Configuration warning:
Serverless:   at 'functions.my_websocket.events[0].websocket': unrecognized property 'contentHandling'
Serverless:   at 'functions.my_websocket.events[0].websocket': unrecognized property 'ContentHandlingStrategy'
Serverless:   at 'functions.my_websocket.events[1].websocket': unrecognized property 'contentHandling'
Serverless:   at 'functions.my_websocket.events[1].websocket': unrecognized property 'ContentHandlingStrategy'
Serverless:   at 'functions.my_websocket.events[2].websocket': unrecognized property 'contentHandling'
Serverless:   at 'functions.my_websocket.events[2].websocket': unrecognized property 'ContentHandlingStrategy'

It's worth noting this was achievable (by a friend) using cdk as follows:

    const messageIntegration = new CfnIntegration(this, `${name}-message-route-lambda-integration`, {
      apiId: api.ref,
      integrationType: 'AWS_PROXY',
      integrationUri: 'arn:aws:apigateway:' + config['region'] + ':lambda:path/2015-03-31/functions/' + messageFunc.functionArn + '/invocations',
      credentialsArn: role.roleArn,
      contentHandlingStrategy: 'CONVERT_TO_BINARY', // see http://amzn.to/39DkYP4
    })

I'm looking for a sls approach since all of my infra is managed in sls and I don't want to absorb the tech debt migrating (unless I have to)


Solution

  • I "solved" this by base64 encoding my binary data and using the $default handler, which allows arbitrary datatypes