Эта ошибка возникает при вызове .postToConnection в ответ на событие $ connect.
Вы можете вызвать .postConnection без ошибок в ответ на событие $ default.
// index.js
// the handler is defined as: index.handler
const AWS = require("aws-sdk");
exports.handler = function (event, context, callback) {
console.log('event.requestContext.eventType', event && event.requestContext && event.requestContext.eventType)
if (event.requestContext.eventType === "CONNECT") {
console.log('$connect event')
// calling apigwManagementApi.postToConnection will throw an exception
callback(null, {
statusCode: 200,
body: "Connected"
});
} else if (event.requestContext.eventType === "DISCONNECT") {
console.log('$disconnect event')
// calling apigwManagementApi.postToConnection is pointless since the client has disconneted
callback(null, {
statusCode: 200,
body: "Disconnected"
});
} else {
console.log('$default event')
const ConnectionId = event.requestContext.connectionId
const bodyString = event.body
const Data = bodyString
const apigwManagementApi = new AWS.ApiGatewayManagementApi({
apiVersion: "2018-11-29",
endpoint: event.requestContext.domainName + "/" + event.requestContext.stage
});
apigwManagementApi
.postToConnection({ ConnectionId, Data })
.promise().then(() => {
callback(null, {
statusCode: 200,
body: "Disconnected"
});
})
}
};