AWS上的Websocket连接即使只发起一个请求也会导致TOO MANY REQUESTS错误

3
我有一个terraform脚本,看起来它是要在AWS上部署一个WebSocket API连接,但是一旦部署后,每次我连接时都会持续收到“429太多请求”的错误。使用的是terraform 0.13.4版本。我已经手动增加了控制台上的请求数量,但每次我执行wscat -c {MYENDPOINT}时都会得到一个429的错误。在网上或文档中找不到任何相关信息。下面是我的terraform代码,想知道是否有人能看到我在路由或集成方面是否漏掉了什么?以下是我一直收到的日志响应:(VH_SDESljoEF7tg=) Gateway response body: { "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
}

1
你找到这个问题的原因了吗?我也遇到了相同的情况。 - Richard Whitehouse
1个回答

5

我无法解释限速的原因,但我已经在我的aws_apigatewayv2_stage资源中添加了这个块,触发新的部署,现在我能够使用wscat连接:

  default_route_settings {
    throttling_rate_limit = 100
    throttling_burst_limit = 50
  }

(相关文档请点击此处)


2
这就是它!Terraform默认使用0,但对于HTTP API(使用默认限制)和Websocket API(将限制设置为0)似乎有不同的解释。 - Krotton
2
那也帮了我很多。我正在使用Terraform,而默认值确实是0,就像@Krotton所报告的那样。谢谢 :) - Jens

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接