Skip to content

API Gateway security: authentication, logging, throttling and API protection

API Gateway is the front door to your APIs. This guide shows how to authenticate, rate limit and monitor requests without leaving endpoints open.

Reading time
11 min
Published

API Gateway is where your APIs meet the internet, so it sits on the front line. The most common problems are endpoints without proper authentication, no rate limiting (which leaves room for abuse) and missing logs. Without logs, the team cannot see an attack while it happens and has nothing to investigate afterward. Because the Gateway usually sits in front of Lambda functions and internal services, a flaw in it exposes everything behind it.

Before you touch any control, note that API Gateway has two main API types, and they do not offer the same security features. REST APIs come with authorizers, API keys, usage plans, resource policies, mTLS and direct integration with AWS WAF. HTTP APIs are simpler and cheaper, with native JWT and Lambda authorizers, but they lack some of those controls. In other words, picking the type is already a security decision. And whichever type you choose, every route still needs an explicit authentication decision.

The focus here is API Gateway security from the defender's side: every exposed endpoint has to be authenticated, rate limited and auditable. Authorizers and resource policy restrictions decide who gets in, rate control and the WAF integration hold back abuse, and observability records what happened. CORS gets its own treatment, because it deals with how the API is used from the browser.

Authentication and authorizers

Every exposed route needs an explicit authentication decision. API Gateway supports several mechanisms, and the right one depends on who calls the API. When the caller is another AWS service, IAM authorization does the job: the request arrives signed with temporary credentials. For end users, a JWT authorizer validates tokens issued by an identity provider. If none of the native options fits, a Lambda authorizer lets you write your own authorization logic. Watch out for routes left open by accident, which are common in APIs that grew without planning and piled up forgotten endpoints.

Because the authorizer validates the request before it reaches the backend, unwanted traffic stops there and the identity decision lives in a single place. A Lambda authorizer can cache its response for a while so it does not run on every request. That improves latency, but the cache key needs care: if it is built from the wrong identity, the response given to one user grants access to another. Always check the token's signature, issuer, audience and expiration, instead of only checking that a token is present.

API keys are not authentication

API keys exist to identify consumers and apply quotas to them through usage plans, but they do not authenticate anyone: if a key leaks (into a repository, for example), whoever has it can call the API. Use API keys alongside real authentication, never in place of it. Resource-level authorization, meaning whether that user can access that specific object, has to happen in the backend on every request, because the Gateway does not know the ownership logic of your data.

Resource policies and private APIs

Besides the authorizer, a REST API accepts a resource policy. It decides, before anything else, who can even invoke the API, based on conditions such as the network source, the source account or the VPC endpoint in use. In practice, this lets you restrict the API to a range of trusted IPs or to a specific VPC, which reduces exposure on the open internet. If the API is only consumed internally, go one step further and use a private API, which only answers through a VPC endpoint. That is better than leaving it public and stacking controls on top.

Resource policy restricting access to an IP range
{  "Effect": "Deny",  "Principal": "*",  "Action": "execute-api:Invoke",  "Resource": "arn:aws:execute-api:REGION:ACCOUNT:API_ID/*",  "Condition": {    "NotIpAddress": { "aws:SourceIp": ["203.0.113.0/24"] }  }}

Note that the resource policy filters exposure but does not replace authentication. It narrows down who can reach the API, and whoever gets past the filter still has to be authenticated and authorized. IP restriction is also fragile when the audience is broad or mobile (people using the API from their phones, on a different network every day). It works best for internal APIs, APIs used by known partners and administrative APIs.

Throttling, WAF, CORS and mTLS

Throttling limits the request rate and protects the backend against abuse and traffic spikes. API Gateway applies these limits at several levels: there is a ceiling per account and Region, limits per stage and per method and, through a usage plan, limits per consumer, each one with a burst and an average rate. The per-consumer limit is what keeps a single misbehaving client from slowing the API down for everyone. The stage ceiling stays in place as a general safety net.

Along with throttling, put a WAF in front of the API to block web attacks and malicious patterns. AWS WAF associates web ACLs directly with regional REST APIs. For HTTP APIs, the equivalent protection usually comes from CloudFront with WAF at the edge. CORS needs attention too. A permissive CORS policy that allows any origin lets third-party pages misuse your API. Restrict the origin, methods and headers to what the application actually uses, as in the example below.

CORS headers restricted to a known origin
Access-Control-Allow-Origin: https://app.your-domainAccess-Control-Allow-Methods: GET, POSTAccess-Control-Allow-Headers: Authorization, Content-TypeAccess-Control-Max-Age: 600

For partner integrations that need certainty about the client's identity, consider mTLS on the custom domain. With it, every client has to present a valid certificate, and anyone without one is blocked before even reaching the authorizer. It is a strong control for machine-to-machine APIs. The cost is operational: someone has to look after the certificate authority and keep the truststore up to date.

  • Require authentication on every exposed route, with no route left open by accident
  • Use authorizers to check the token issuer, audience and expiration before the request reaches the backend
  • Treat the API key as identification in the usage plan, not as authentication
  • Restrict exposure with a resource policy, or with a private API when consumption is internal
  • Configure throttling per stage and per consumer to hold back abuse and spikes
  • Put a WAF in front of the API to stop web attacks
  • Allow only the origins the application uses in CORS
  • Consider mTLS for machine-to-machine integrations with partners

Logs, observability and detection

Enable access and execution logs and export the metrics. Without logs, endpoint abuse and exploitation attempts go unnoticed, and an investigation has nothing to work with. The access log is built from context variables, so record what matters for an investigation: request ID, source, route, status and the authorizer result, as in the format below. Leave out the request body, tokens and sensitive data so the log itself does not become a leak.

Access log format with context variables
{  "requestId": "$context.requestId",  "ip": "$context.identity.sourceIp",  "requestTime": "$context.requestTime",  "httpMethod": "$context.httpMethod",  "routeKey": "$context.routeKey",  "status": "$context.status",  "authorizerError": "$context.authorizer.error"}

Signals that deserve an alert

Having logs is not the same as detecting. Detection starts when certain patterns trigger an alert. A burst of authorization errors, for example, usually points to an attempt at unauthorized access or to someone testing a leaked credential. Throttling spikes suggest abuse or an attack, and endpoints with unusual volume at unexpected hours also deserve a look. On the configuration side, keep the production stage more restricted than the development stages, and treat any test stage that suddenly shows up exposed as an item to review.

Practical checklist

  • Choose between REST API and HTTP API based on the controls you need
  • Require explicit authentication on every exposed route
  • Use authorizers (IAM, JWT or Lambda) that check issuer, audience and expiration
  • Enforce resource-level authorization in the backend on every request
  • Use API keys only as identification in the usage plan, never as authentication
  • Restrict exposure with a resource policy or a private API where it fits
  • Configure throttling per stage and per consumer
  • Put a WAF in front of the API
  • Restrict CORS to known origins
  • Consider mTLS for machine-to-machine integrations with partners
  • Turn on access and execution logs without recording sensitive data
  • Create alerts for authorization errors, throttling spikes and anomalous volume
  • Keep the production stage more restricted than the development stage

Best practices

  • Authenticate and authorize in layers, at the Gateway and in the backend
  • Use resource policies and private APIs to reduce exposure on the open internet
  • Use per-consumer throttling so an abusive client only hurts itself
  • Put the WAF in front of the Gateway as an edge defense
  • Keep CORS restricted and review the origin list whenever it changes
  • Monitor the API with logs and metrics from the first deploy, without recording secrets
  • Remove forgotten endpoints and stages that nobody claims as their own

Common mistakes

Route exposed without authentication by accident
Anyone can reach the backend without passing through a single barrier.
Treating the API key as authentication
A leaked key is all it takes for someone to use the API, with no other control in the way.
CORS that allows any origin
Third-party pages can use the API on the user's behalf.
No per-consumer throttling
A single abusive client overloads the backend, and costs go up for everyone.
Test stage exposed with loose controls
It becomes a back door into the same integration used in production.
API with no logs, or with tokens written to the log
In the first case, exploitation goes unseen; in the second, the log itself becomes the leak.

Authorship and review

Cesar Gargiulo

CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.

Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.

Frequently asked questions

Is an API key enough to protect my API?
No. An API key identifies consumers and applies quotas to them through usage plans, but it does not work as authentication: if the key leaks, whoever has it can call the API directly. Use the key together with real authentication, such as IAM, JWT or Lambda authorizers, and leave resource-level authorization to the backend. An API protected only by an API key is vulnerable.
Where should I check authorization, at the Gateway or in the backend?
In both places, each with its own role. The Gateway validates authentication right at the entrance, with authorizers, so unwanted traffic never even reaches the backend. Resource-level authorization (whether that user can access that object), on the other hand, has to happen in the backend on every request. Teams that rely only on the Gateway tend to leave object-level authorization gaps.
What is the difference between a REST API and an HTTP API for security controls?
REST APIs have more native controls: API keys with usage plans, resource policies, direct integration with AWS WAF, mTLS and request validation. HTTP APIs are simpler and cheaper, with native JWT and Lambda authorizers, but they lack some of these features. With HTTP APIs, WAF protection usually comes from a CloudFront distribution placed in front. Choose the type based on the controls you actually need: the lower price of an HTTP API only pays off if you will not miss the features it lacks.
Why is CORS a security concern?
Because a permissive CORS policy that allows any origin lets third-party pages send requests to your API from the user's browser, and that can be exploited for misuse. Restrict the allowed origins to the ones you control and take extra care when allowing credentials. Loose CORS is a common exposure, and many teams never even look at it.
Does throttling protect against attacks?
Partly. By limiting the request rate, throttling reduces abuse, brute force and spikes that would overload the backend. It does not solve the problem on its own, but combined with WAF and authentication it is a layer that makes a difference. Set per-consumer limits in addition to the stage ceiling, so one abusive client cannot slow the API down for everyone else.
How do I restrict access to an internal API without making it public?
There are two approaches, and they complement each other. A resource policy can deny invocation to anyone outside an IP range or a specific VPC. If you want more isolation, a private API only accepts access through a VPC endpoint and is not exposed to the internet. The choice depends on the audience: for known external partners, use an IP-based resource policy; for internal-only consumption, use a private API.

When to bring in specialist help

Reviewing API authentication, authorization and exposure is part of GUARDIASEC's Penetration Testing and Application Security services. The assessment covers APIs and endpoints, and each finding comes with evidence and the real risk it carries.