我正在尝试使用**AWS.ApiGatewayManagementApi() postToConnection**
方法向通过套接字连接的客户端发送消息。它在本地运行良好,但在ec2中不工作。唯一的区别是VPC。有问题吗?
var AWS = require('aws-sdk');
var awsGW = new AWS.ApiGatewayManagementApi({
endpoint: "https://endpoint",
accessKeyId: "accessKeyId",
secretAccessKey: "secretAccessKey",
region: "region"
});
awsGW.postToConnection({
ConnectionId: 'ConnectionId',
Data: '{"key1":"msg1","key2":"msg2"}'
}, (err, success) => {
if (err) {
console.log('Socket error', err);
} else {
}
});
错误:
{ ForbiddenException: Forbidden
at Object.extractError (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/protocol/json.js:51:27)
at Request.extractError (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/protocol/rest_json.js:55:8)
at Request.callListeners (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
at Request.emit (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
at Request.emit (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /home/ubuntu/git/backend/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/home/ubuntu/git/backend/node_modules/aws-sdk/lib/request.js:685:12)
message: 'Forbidden',
code: 'ForbiddenException',
time: 2019-06-10T07:40:02.214Z,
requestId: 'f4caef03-8b52-11e9-9cc3-91bfe40b8eb0',
statusCode: 403,
retryable: false,
retryDelay: 37.0834357877396 }
5条答案
按热度按时间vs3odd8k1#
我遇到了一个类似的问题,这是因为我使用了错误的端点。我是从
event
对象生成的,但是是在一个普通的API请求上,所以我得到了一个不同服务的端点(普通的API,而不是WebSocket)。如果你的websockets服务被部署到一个类似
wss://abcd
的URL,那么你的端点应该是https://abcd
。q5lcpyga2#
在我的例子中,在我用来示例化
注:终点可提供2种形式:
wss.my-super-website-with-websocket.com/v1
(其中v1是配置的基本路径Map)https://********.execute-api.us-east-5.amazonaws.com/prod
,其中prod是API网关中配置的stage。更重要的是,为了您的调试目的,如果您感到绝望,我发现您可以很容易地记录
aws-sdk
发出的请求,以确保您正在发出正确的请求(使用aws cli尝试请求也是一个好主意):node_modules\aws-sdk\lib\event_listeners.js
3pvhb19x3#
403听起来您需要为调用
postToConnection
的IAM角色更新策略在我的例子中,我有明确的缺失规则,因此我必须附加
ExecuteAPI
的授权o3imoua44#
我遇到了同样的问题-我在私有子网中的VPC内有Lambda,并且有API网关的VPC端点,但它总是返回
403 Forbidden
。这很奇怪,但在将VPC端点替换为NAT网关后,问题消失了。
xn1cxnb45#
这就是我的工作来源:https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-apigatewaymanagementapi/classes/posttoconnectioncommand.html