Skip to content

Amazon API Gateway apigateway

See which API calls fail and why, down to the Lambda run behind them

Find the APIs that fail or run out of time, see who can call each route, and follow a failed call from its access log line to the log lines of the Lambda run that served it, without clicking through the console.

  • REST, HTTP and WebSocket APIs
  • Read-only: never calls your API
  • The Lambda run's own lines, by request ID
  • Plain text outside Jupyter

The examples use acme's orders-api, a REST API whose POST /orders route calls the orders-etl Lambda function. Some of its calls take longer than API Gateway's 29 seconds and come back as a 504, while orders-etl is still working.

Watch it run

Two calls in a notebook, from every API to the route that runs out of time: each call typed and run, and the report it draws, with what to notice. It plays as you scroll to it; click it to pause, or pick a chapter to jump to a call.

Set up in SageMaker

  1. Install it in a notebook cell:

    %pip install aws-analyzer
    
  2. Make the view. It uses the notebook's IAM role and region, so there's nothing to configure.

    from aws_analyzer import APIGatewayView
    
    apigw = APIGatewayView()
    apigw.help()             # every command, grouped by task; apigw.help("errors") shows one in full
    
Another region or profile, plain text

APIs are regional, so if apis() comes back empty, check the region first. Only boto3 is required.

from aws_analyzer import APIGatewayAnalyzer, APIGatewayView

apigw = APIGatewayView(APIGatewayAnalyzer(region="eu-west-1", profile="dev"))
apigw = APIGatewayView(mode="text")    # plain text, also what you get outside Jupyter

Start here

Five questions people bring to an API, and the one line that answers each. None of them call your API or change it.

Health and cost

How an API works

An API is named by its name, its ID, "name/stage", its invoke URL or a link copied from the console.

How to read a report

Every report puts the answer first: cards with the numbers that matter, then the findings, warnings first, each a bold headline with why it matters and what to do underneath. The tables of detail come after, and Next at the bottom lists two or three commands with the arguments filled in. One click on a command selects all of it, ready to copy.

Your APIs

apis() lists every REST, HTTP and WebSocket API in the region with its requests, its share of 4xx and 5xx, how long its slowest calls take and its estimated monthly cost, the ones failing most first.

apigw.apis()              # or apis(days=30)

apis(): six APIs with their stages, requests, 4xx and 5xx share, p95, auth and cost, and findings about orders-api's server errors, partner-api's open routes and an unused legacy-v1 apis(): six APIs with their stages, requests, 4xx and 5xx share, p95, auth and cost, and findings about orders-api's server errors, partner-api's open routes and an unused legacy-v1

apigw.apis(): orders-api fails most and runs close to the time limit, partner-api has two routes anyone can call, and legacy-v1 had no calls.

The numbers are CloudWatch's (AWS/ApiGateway), in whole UTC days. Auth says who can call an API's routes: an authorizer, IAM, an API key, or none ×2 when two routes answer anyone. A health check that answers with a mock doesn't count, since it reaches nothing.

Worth knowing

API Gateway gives up on a request after 29 seconds (30 for an HTTP API) and answers 504, even when the Lambda function behind it is still working and will finish the job. A route whose slowest calls come close to that is a warning, and performance() shows which. api_info() also reads the timeout of each Lambda function behind a route: one longer than API Gateway waits is a note, since past that the function runs on, billed, for an answer nobody gets.

One API

api_info() shows one API's numbers on one stage, its stages and its routes, with where each route goes and who can call it.

apigw.api_info("orders-api")              # its main stage: prod, $default, or the one deployed last
apigw.api_info("orders-api/dev")          # another stage

api_info('orders-api'): cards with requests, 5xx, p95, routes, stages and cost, warnings that POST /orders runs out of time and fails 9.6% of its calls, a note that orders-etl's 60 s timeout outlasts the 29 s API Gateway waits for it, then the stages and routes api_info('orders-api'): cards with requests, 5xx, p95, routes, stages and cost, warnings that POST /orders runs out of time and fails 9.6% of its calls, a note that orders-etl's 60 s timeout outlasts the 29 s API Gateway waits for it, then the stages and routes

apigw.api_info("orders-api"): POST /orders runs out of time, while orders-etl may still be working.

A route's own numbers need the stage's detailed metrics. Without them the routes table has none, and performance() works them out from the access log instead.

What's failing

errors() groups the calls that failed by status, error and route, the most common first, and says what each status means and what to read next.

apigw.errors("orders-api")                 # the last 24 hours
apigw.errors("orders-api", since="7d")
apigw.calls("orders-api", status="5xx")    # every server error, call by call
apigw.calls("orders-api", search="c1f08a2e")   # one request ID

A 504 is a timeout, a 502 an answer from Lambda that API Gateway can't use (it needs statusCode and a body that's a string, and no exception), a 429 throttling, and a 403 Missing Authentication Token a path or method that doesn't exist. For a call that reached Lambda, the group shows LambdaView().logs('orders-etl', request_id='…'), the run that served it.

Worth knowing

CloudWatch only counts calls. Which call failed, and why, is only in the stage's access log, one line per call, and a stage keeps one only if it's turned on. Without it, errors() shows the counts and the aws apigateway update-stage command that turns it on, in a format that logs everything these reports read.

How an access log line is read

The owner of a stage chooses its access log's format, made of $context variables: JSON like {"requestId":"$context.requestId","status":"$context.status"}, or text like the Common Log Format. The format is in the stage's settings, so each line is read by its own format: a JSON key is known by the variable it holds, whatever its name, and a text format becomes a pattern with a place for each variable. To say where a call's time went and which Lambda run served it, the format needs $context.integration.latency and $context.integration.requestId; a finding says when they're missing, and shows a format that has them.

Routes

routes() lists every route with who can call it, where it goes and how long API Gateway waits, with the curl command that calls it.

apigw.routes("partner-api")
apigw.stages("orders-api")       # each stage's throttling, logs, tracing, cache and WAF

A route anyone can call (no authorizer, no IAM, no API key) is a warning, unless it's meant to be public. The curl command leaves credentials as $TOKEN, $API_KEY or your AWS keys, and a WebSocket API gets wscat instead. The command is there to copy: nothing here sends it.

How long calls take

performance() shows each route's average, 95th percentile and longest call, how much of the time was the backend's, and where the slowest call's time went.

apigw.performance("orders-api")

The routes' numbers come from the access log when the stage has one, else from detailed metrics. A call's time splits into the authorizer, the integration (the backend) and API Gateway itself.

Explore by clicking

explore() opens a window with every API; click one to read its routes as a tree and its calls one by one, each with where its time went and the Lambda run behind it.

apigw.explore()                               # the API failing most, or the first
apigw.explore("orders-api", tab="calls")      # straight to its calls

The API Gateway explorer guide covers every tab, with its own video tour.

Take a report into pandas or Excel

Every report ends with 🐼 This report as data: its tables for pandas, each column explained, and a workbook to share. A call's row names the Lambda function behind its route and the request ID of the run that answered it.

apigw.errors("orders-api")
data = apigw.data()
data["causes"]           # each way of failing: status, error, route, count, share, the Lambda run to read
data["failed"]           # every failed call: time, route, status, latency_ms, integration_ms, function, lambda_request
apigw.to_excel()         # aws-analyzer-exports/apigateway-errors-orders-api.xlsx

data("performance", "orders-api") gives the same without drawing the report: each route's p95_ms and p95_share_of_limit (over 0.8 comes close to the 504), and every call with its time split into integration_ms, authorizer_ms and gateway_ms. A call's lambda_request opens its run: LambdaView().logs(function, request_id=...).

Which tables each report gives
Report Tables
apis() apis (requests, 4xx and 5xx shares, p95, open routes, estimated monthly cost), days (a row per API per UTC day)
api_info() stages, routes (with their numbers when the stage has detailed metrics), days
routes() routes (who can call it, where it goes, its timeout, the curl command), authorizers
stages() stages (URL, throttling, access log, tracing, cache and what it costs, WAF)
calls() calls (the ones status= keeps), routes (their numbers from those calls), hours
errors() causes, failed
performance() routes (the slowest first), calls (the slowest first, each with where its time went)

A report with nothing to read, such as calls() on a stage without an access log, gives no tables and says why.

That's the guide. Everything below is reference for when you need it: the data in Python, cost, permissions, troubleshooting and every command.

Use the data in Python

apigw.core is the APIGatewayAnalyzer. Every report has a data method on it.

core = apigw.core

ov = core.overview(days=7)                       # Overview: .apis, .numbers (by API ID), .errors
api = core.api("orders-api")                     # Api: .kind, .stages, .routes, .authorizers, .open_routes
api.route("POST /orders").integration.function   # 'orders-etl'
page = core.calls("orders-api", since="1h")      # CallPage: .calls, .failed, .fields, .truncated
page.to_df()                                     # every call as a DataFrame
run = core.lambda_run(api, page.failed[0])       # LambdaRun: .function, .lines, .duration_ms, .note

The analysis functions make no AWS calls, so they also work on log lines you already have:

from datetime import datetime, timezone
from aws_analyzer.apigateway import parse_access_line, read_format, group_errors

fmt = read_format(stage_format)                  # the stage's accessLogSettings format
calls = [parse_access_line(line, datetime.now(timezone.utc), fmt) for line in lines]
group_errors([c for c in calls if c])

Cost

Costs are estimates at us-east-1 list prices (APIGATEWAY_PRICES, the first tier): REST APIs $3.50 per million requests, HTTP APIs $1.00 per million, WebSocket APIs $1.00 per million messages, plus a REST cache by the hour ($0.02 an hour for 0.5 GB). Requests are what CloudWatch counted over the window, scaled to a month. Pass your own prices with APIGatewayAnalyzer(prices={"rest_request": 0.0000037}).

apis() reads 6 CloudWatch metrics per API ($0.01 per 1,000) and says how many it read; api_info() reads 6 more per route when the stage has detailed metrics. APIs, routes, stages and access logs cost nothing to read.

Permissions

Everything is read-only. Anything the notebook's role can't read shows up as a note instead of an error. API Gateway authorizes every read, REST, HTTP and WebSocket alike, as apigateway:GET on the resource. This policy covers every command:

IAM policy
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ReadApis",
      "Effect": "Allow",
      "Action": "apigateway:GET",
      "Resource": "arn:aws:apigateway:*::/*"
    },
    {
      "Sid": "ReadNumbers",
      "Effect": "Allow",
      "Action": "cloudwatch:GetMetricData",
      "Resource": "*"
    },
    {
      "Sid": "ReadAccessLogsAndLambdaRuns",
      "Effect": "Allow",
      "Action": ["logs:FilterLogEvents", "lambda:GetFunction"],
      "Resource": "*"
    }
  ]
}

The last statement is for the access logs and the Lambda run behind a call; without it calls() and errors() say the log couldn't be read, and the counts still show.

Troubleshooting

“No API named …”

Names are case-sensitive and APIs are regional. The note lists the closest names; apis() lists them all. Use APIGatewayView(APIGatewayAnalyzer(region="eu-west-1")) for another region.

“Stage … keeps no access log”

API Gateway writes a line per call only when the stage's access log is on. The note shows the aws apigateway update-stage (or aws apigatewayv2 update-stage) command that turns it on; calls after that show up.

A call says “-” for Backend and has no Lambda run

The stage's access log format doesn't log $context.integration.latency or $context.integration.requestId. stages() shows the format, and a finding shows one that logs them.

The routes table has no numbers

Per-route numbers need the stage's detailed metrics (a setting on the stage, billed as custom metrics). performance() works them out from the access log instead.

Command reference

Every APIGatewayView command. apigw.help() prints the same list grouped by task, and apigw.help("errors") shows one command's full description.

Command What it shows
apis(days=7) Every API: requests, 4xx and 5xx share, p95, who can call it, cost
api_info(name, stage=None, days=7) One API: its numbers on a stage, its stages and its routes, and what needs attention
routes(name, stage=None) Every route: who can call it, where it goes, its timeout, and the curl command that calls it
stages(name) Every stage's throttling, logs, tracing, cache and WAF, and its access log format
calls(name, stage=None, since="24h", status=None, search=None, n=50) The access log, call by call, newest first
errors(name, stage=None, since="24h") Failed calls grouped by status, error and route, with what each means and the Lambda run to read
performance(name, stage=None, since="24h") How long calls take, route by route, and where the slowest call's time went
data(command=None, *args, **kwargs) The last report's tables as pandas DataFrames, each column explained
to_excel(command=None, *args, path=None, **kwargs) The last report as an Excel workbook: summary, charts, a sheet per table and how to read them
explore(name=None, tab=None, stage=None, since="24h", height=None) The explorer window: every API, its routes as a tree, its calls with their Lambda runs, performance and stages
help(command=None) This list, grouped by task; help("name") shows one command in full