AWS 上的 Websocket 连接总是会导致太多请求,即使只有一个请求

Fer*_*ern 3 api amazon-web-services websocket terraform

所以我有这个 terraform,它似乎部署了一个到 AWS 的 websocket api 连接,但是...部署后,当我连接时,我总是收到“429 请求太多”错误。使用 terraform 0.13.4。我已经在控制台中手动打开了请求,但每次我wscat -c {MYENDPOINT}都会收到 429。无法在网上找到任何内容或手册中与此相关的任何内容。这是地形。想知道是否有人可以看到我的路线或集成中是否遗漏了某些内容?这是我不断从日志中得到的响应: (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
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)

Aar*_*ron 5

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

  default_route_settings {
    throttling_rate_limit = 100
    throttling_burst_limit = 50
  }
Run Code Online (Sandbox Code Playgroud)

相关文档在这里

  • 就是这个!Terraform 默认使用 0,对于 HTTP API(使用默认限制)的解释似乎与 Websocket API(将限制固定为 0)不同。 (2认同)
  • 这也对我有帮助。我使用的是 Terraform,默认值确实是“0”,就像 @Krotton 报道的那样。谢谢 :) (2认同)