Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket Connection on AWS always results in TOO MANY REQUESTS even with one request

So I have this terraform that seems to deploy a websocket api connection to AWS BUT.... Once deployed, when I connect, I consistently get "429 too many requests" errors. Using terraform 0.13.4. I've turned up the requests manually in the console but every time I wscat -c {MYENDPOINT} I get a 429. Can't find anything online or anything in the manuals about this. Here is the terraform. Wondering if anyone can see if I'm missing something in my routes or integrations? Here is the response I keep getting from the logs: (VH_SDESljoEF7tg=) Gateway response body: { "message": "Too Many Requests", "connectionId": "VH_SDd21joECIeg=", "requestId": "VH_SDESljoEF7tg=" }

and

(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
}
like image 329
Fern Avatar asked Oct 16 '25 02:10

Fern


1 Answers

I can't explain the cause of the throttling, but I added this block to my aws_apigatewayv2_stage resource, triggered a new deployment, and now I'm able to connect using wscat:

  default_route_settings {
    throttling_rate_limit = 100
    throttling_burst_limit = 50
  }

(relevant docs here)

like image 183
Aaron Avatar answered Oct 18 '25 01:10

Aaron