从 VPC 中获取 "Forbidden" 异常发布到 API 网关 Websocket API

Getting a "Forbidden" exception posting to a API Gateway Websocket API from within a VPC

我在连接到 Websocket API 网关端点的 VPC 中有一个 lambda 运行ning。当客户端连接并发送 websocket 命令时,它被调用得很好,并且也能够访问我的 VPC 内的资源 (RDS),但是当我通过 AWS.ApiGatewayManagementApi postToConnection 发送响应时,我得到一个"Forbidden" 错误。

Lambda 的角色具有 "execute-api:Invoke" 权限,并且 lambda 本身 运行 正在使用 NAT 网关和链接到 "execute-api" 服务的接口端点连接 VPC 的私有子网(在此之前添加对 postToConnection 的调用会超时)所以我很困惑我可能仍然缺少什么权限。

我需要调用与“.execute-api..amazonaws.com/”不同的端点吗?当 运行 在 VPC 之外时,同样的代码也有效。

您遇到的问题是由于从配置了 VPC 端点的 VPC 连接到 public API。有关故障排除步骤,请参阅以下文档:https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-vpc-connections/

上面的文档提到:

If you get this error when connecting to your API Gateway public APIs from an Amazon VPC, check to see if there's an interface VPC endpoint for API Gateway associated with that VPC, with private DNS enabled. When private DNS is enabled for an interface VPC endpoint associated with a VPC, all requests from the VPC to API Gateway APIs resolve to that VPC endpoint, and you can't connect to public APIs using a VPC endpoint.

为了轻松修复,您可以为 VPC 端点禁用私有 DNS,并确保 Lambda function has internet access.