Journeyman
Journeyman

Reputation: 10281

AWS lambda websocket implementation using custom domain name has POST issues

I have an AWS API gateway websocket lambda implementation. I have connected a custom domain to the API gateway. I am able to connect and disconnect just fine. But when I try to post messages using the AWS.ApiGatewayManagementApi the custom domain name is causing a problem.

I was building the endpoint using parms, as follows:

const apigwManagementApi = new AWS.ApiGatewayManagementApi({
    apiVersion: '2018-11-29',
    endpoint: event.requestContext.domainName + '/' + event.requestContext.stage
  });

And then calling it as follows:

await apigwManagementApi.postToConnection({ ConnectionId: connectionId, Data: postData }).promise();

This resulted in an error as follows:

2021-01-07T09:56:12.751Z    846f4674-0ed9-422b-8b35-64f0c43a10de    INFO    NotFoundException: No method found matching route test/@connections/YxcdBcOkrPECHwA= for http method POST.
    at Object.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:52:27)
    at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/rest_json.js:55:8)
    at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:688:14)
    at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:690:12) {
  code: 'NotFoundException',
  time: 2021-01-07T09:56:12.712Z,
  requestId: '43b744b9-25ef-4a15-82ed-6ecb13062208',
  statusCode: 404,
  retryable: false,
  retryDelay: 64.57971601177839
}

I then changed the code to use a hard-coded value for the AWS gateway endpoint, which looks like this (actual id ogjkythg67m changed for privacy):

const apigwManagementApi = new AWS.ApiGatewayManagementApi({
    apiVersion: '2018-11-29',
    endpoint: "https://ogjkythg67m.execute-api.eu-west-2.amazonaws.com/test"
  });

At this point everything started working.

So, it seems that I can't use the event.requestContext.domainName + '/' + event.requestContext.stage vars to build my endpoint when using a custom domain.

I don't really want to hard-code the endpoint into the code. Is there any way to configure my AWS API gateway or lambda definition to leverage the custom domain name, or circumvent the issue in some other way?

Thanks.

Upvotes: 0

Views: 1085

Answers (2)

brianbruff
brianbruff

Reputation: 2225

I'm late to the party I'm sure, This happened to me today, I'd just built upon the sample provided by AWS.

there they also append the stage like your code above

const apigwManagementApi = new AWS.ApiGatewayManagementApi({
apiVersion: '2018-11-29',
endpoint: event.requestContext.domainName + '/' + event.requestContext.stage

});

To fix, you simply remove the code that appends the stage

 + '/' + event.requestContext.stage

then it works fine with the custom domain

Upvotes: 1

HenryTK
HenryTK

Reputation: 1287

The error message says

NotFoundException: No method found matching route test/@connections/YxcdBcOkrPECHwA= for http method POST.

so /@connections/YxcdBcOkrPECHwA= is somehow being appended to the endpoint. Looks like the websocket documenation confirms this.

Log event.requestContext.stage to confirm this yourself. The documentation doesn't seem to cover this, so you'll have to inspect event.requestContext by logging it to see if another field with just the stage is provided, or provide some string manipulation to strip the rest of that path.

Upvotes: 0

Related Questions