amazon-web-services - AWS 上的 Websocket 连接总是导致 TOO MANY REQUESTS 即使是一个请求

标签 amazon-web-services api websocket terraform

所以我有这个 terraform,它似乎将 websocket api 连接部署到 AWS 但是...... 部署后,当我连接时,我始终收到“429 太多请求”错误。 使用 terraform 0.13.4。 我已经在控制台中手动打开了请求,但每次我 wscat -c {MYENDPOINT} 我都会得到 429。 无法在网上找到任何关于此的内容或手册中的任何内容。 这是地形。想知道是否有人可以看到我的路线或集成中是否遗漏了什么? 这是我不断从日志中得到的响应: (VH_SDESljoEF7tg=) 网关响应正文:{ "message": "Too Many Requests", "connectionId": "VH_SDd21joECIeg=", "requestId": "VH_SDESljoEF7tg="}

(VH_SDESljoEF7tg=) Key throttle limit exceeded for RestApi k27g2ypii6, Stage test, Resource $connect, HttpMethod GET. Limit: 42.00 Burst: 0
resource "aws_apigatewayv2_api" "websocket-api" {
  name                       = "websocket-api"
  protocol_type              = "WEBSOCKET"
}

resource "aws_apigatewayv2_integration" "chatRoomConnectIntegration" {
  api_id           = aws_apigatewayv2_api.websocket-api.id
  integration_type = "AWS_PROXY"
  integration_uri  = aws_lambda_function.ChatRoomConnectFunction.invoke_arn
  integration_method = "POST"
}

resource "aws_apigatewayv2_route" "connectRoute" {
  api_id    = aws_apigatewayv2_api.websocket-api.id
  route_key = "$connect"
  target = "integrations/${aws_apigatewayv2_integration.chatRoomConnectIntegration.id}"
}
resource "aws_apigatewayv2_deployment" "deploy" {
  api_id      = aws_apigatewayv2_api.websocket-api.id
  description = "testing deployment"

  triggers = {
    redeployment = sha1(join(",", list(
      jsonencode(aws_apigatewayv2_integration.chatRoomConnectIntegration),
      jsonencode(aws_apigatewayv2_route.connectRoute),
    )))
  }

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_apigatewayv2_stage" "test-stage" {
  api_id = aws_apigatewayv2_api.websocket-api.id
  name   = "test"
  access_log_settings {
    destination_arn = aws_cloudwatch_log_group.access_logs.arn
    format = "$context.identity.sourceIp - - [$context.requestTime] \"$context.httpMethod $context.routeKey $context.protocol\" $context.status $context.responseLength $context.requestId $context.integrationErrorMessage"
  }
  default_route_settings {
    data_trace_enabled = true
    logging_level = "INFO"
    throttling_rate_limit = 42
  }
  route_settings {
    route_key = "$connect"
    data_trace_enabled = true
    logging_level = "INFO"
    throttling_rate_limit = 42
  }
}

resource "aws_api_gateway_account" "api_gateway_accesslogs" {
  cloudwatch_role_arn = aws_iam_role.cloudwatch.arn
}

resource "aws_iam_role" "cloudwatch" {
  name = "api_gateway_cloudwatch_global"

  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": "apigateway.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
EOF
}

resource "aws_iam_role_policy" "cloudwatch" {
  name = "default"
  role = aws_iam_role.cloudwatch.id

  policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:DescribeLogGroups",
                "logs:DescribeLogStreams",
                "logs:PutLogEvents",
                "logs:GetLogEvents",
                "logs:FilterLogEvents"
            ],
            "Resource": "*"
        }
    ]
}
EOF
}

resource "aws_lambda_permission" "allow_api_gateway" {
  action = "lambda:InvokeFunction"
  function_name = aws_lambda_function.ChatRoomConnectFunction.arn
  statement_id = "AllowExecutionFromApiGateway"
  principal = "apigateway.amazonaws.com"
  source_arn = "${aws_apigatewayv2_api.websocket-api.execution_arn}/*/*/*"
}

output "endpoint" {
  value = aws_apigatewayv2_stage.test-stage.invoke_url
}

最佳答案

我无法解释限制的原因,但我将此 block 添加到我的 aws_apigatewayv2_stage 资源,触发了新的部署,现在我可以使用 wscat< 进行连接:

  default_route_settings {
    throttling_rate_limit = 100
    throttling_burst_limit = 50
  }

( relevant docs here )

关于amazon-web-services - AWS 上的 Websocket 连接总是导致 TOO MANY REQUESTS 即使是一个请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64574215/

相关文章:

amazon-web-services - 在 AWS 上部署 super 账本 - 生产设置

amazon-web-services - 一般为 chalice 功能设置超时

mysql - Golang API gorilla MySQL 查询

javascript - WebSocket 重试次数有限制吗?

c# - WebSocketServer MSDN 示例

python - 通过AWS Gateway Api通过Lambda将文件上传到S3

amazon-web-services - 如何使用 key 和 secret 访问 s3 存储桶?

facebook - 如何获取知道线程 ID 的消息线程 URL?

python - github api v3 中的 oauth 流程不起作用

java - 如何防止Websocket连接空闲超时