使用 IAM 授权
WebSocket API 中的 IAM 授权类似于 REST API 的授权,但有以下例外情况:
-
除了现有操作(
execute-api、ManageConnections)之外,Invoke操作也支持InvalidateCache。ManageConnections控制对 @connections API 的访问。 -
WebSocket 路由使用不同的 ARN 格式:
arn:aws:execute-api:region:account-id:api-id/stage-name/route-key -
@connectionsAPI 使用与 REST API 相同的 ARN 格式:arn:aws:execute-api:region:account-id:api-id/stage-name/POST/@connections
例如,您可以为客户端设置以下策略。此示例能让每个人为 Invoke 阶段中除密钥路由之外的所有路由发送消息 (prod),并针对所有阶段阻止每个人将消息发送回连接的客户端 (ManageConnections)。
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "execute-api:Invoke" ], "Resource": [ "arn:aws:execute-api:us-east-1:account-id:api-id/prod/*" ] }, { "Effect": "Deny", "Action": [ "execute-api:Invoke" ], "Resource": [ "arn:aws:execute-api:us-east-1:account-id:api-id/prod/secret" ] }, { "Effect": "Deny", "Action": [ "execute-api:ManageConnections" ], "Resource": [ "arn:aws:execute-api:us-east-1:account-id:api-id/*" ] } ] }