Recently, AWS announced support for a new API Gateway authorizer feature, which took me back to when I had to write a Lambda function for a custom authorizer. With this release, we have an alternative that better fits our use cases and could simplify our authorization implementation. It's great to see AWS introduce features that reduce the need for custom code.
First, we'll see what authorization API Gateway supports.
- Lambda authorizers This option lets you write a Lambda function that uses bearer token authentication and supports headers, paths, query strings, and stage variables or context variables as request parameters. Lambda authorizers control who can invoke REST API methods (GET, POST, PUT, DELETE). It downloads a provider's signing keys, verifies each one, and then returns an IAM policy.
- Amazon Cognito user pool This lets you create custom authentication and authorization for REST APIs. It controls who can invoke REST API methods as well as Lambda authorizers.
- JSON Web Token (JWT) authorizers (NEW) This allows API Gateway to validate JSON Web Tokens (JWTs) directly from an OpenID Connect (OIDC) identity provider without requiring a custom authorization function. A single JWT authorizer can support multiple trusted issuers, while each API method can define its own required authorization scopes. This works on regional, edge-optimized, and private REST APIS.
Next, we'll do a bit of homework and research before jumping into the code. Here are a few questions that came to mind while I was writing and that I'd like to share. with you.
A few things to understand first
Q: What is an identity provider (IdP)?
A: An identity provider is a service that checks who a caller is and then issues signed tokens to prove it. Other systems trust signed tokens, so they don't have to manage passwords or secrets themselves. Examples of identity providers are Okta, Auth0, Microsoft Entra ID, Amazon Cognito, and Google.
After proving who they are using a Client ID and secret. Auth0 issues a signed JWT access token that says who issued it (iss), which API it is for (aud), and what the caller can do (scope). API Gateway checks the token signature using Auth0 public keys, and checks the issuer, audience, expiry, and scopes.
Q: What is authentication and authorization?
A: Authentication: the reader app proves its identity to Auth0 or another identity provider with its Client ID and secrets. Auth0 then issues a signed token with a signature, issuer, audience, and expiry. It confirms that the token is valid and comes from a caller it knows.
Authorization: API Gateway checks the token's scope claim against the method. GET /orders needs books:read, and POST /books needs books:write. The reader is known but only allowed to read, so it can't create orders.
401 Unauthorized - Authentication failed(missing token, a bad token or an expired token
403 Forbidden - Authenticated but not authorized
Q: How do you choose between JWT vs. Lambda Authorizer?
A: Use a JWT authorizer if the only job is to check a standard OIDC token. Use a Lambda Authorizer if the decision needs your own logic or the credential is not a JWT.
Token validation
JWT: API Gateway checks the signature, issuer, audience, expiry, and scope itself. You write no code.
Lambda: You write and maintain the validation code.
Here are a few more comparisons.
Claim rules:
JWT: exact matches only, such as requiring department to equal eng. Each issuer allows up to 10 rules.
Lambda: any logic you can write.
Custom authorization logic:
JWT: not supported. Checks like "does this user own order 42?" or database lookup have to go to your backend.
Lambda: supported. The function can call databases or other services before it allows the request.
Credential types:
JWT: only OIDC JTWs signed with RS or ES algorithms. HMAC-signed tokens are rejected.
Lambda: anything, including API keys, opaque tokens, HMAC signatures, and custom headers.
Revocation:
JWT: no revocation check. A token works until it expires, so keep token lifetimes short.
Lambda: can ask the identity provider whether a token is still valid.
Cost and latency:
JWT: no extra invocation and no extra cost.
Lambda: a function runs on every uncached request, which adds latency and Lambda charges.
Maintainance
JWT: configuration only.
Lambda: code you have to test, patch, and monitor.
You can't use both of them at the same time. However, you can let the JWT authorizer handle authentication and scopes, then put fine-grained rules in your backend, which receives the validated claims.
Now, let's see that in practice.
Sign up
I've already chosen the identity provider we will use. Several options are available, such as Auth0 or Okta, but let's go with Auth0 because it's free and easy to set up.
- Go to https://auth0.com/
- After signing up, note your URL: https://manage.auth0.com/dashboard/us/dev-trzz123456789/. We'll use this later. Region: us Tenant: dev-trzz123456789
- Create an application and select 'machine-to-machine application'
- Select an API. If you don't have one, click Cancel or create a new API first.
- Click the Create a new API button on the right, fill out the name and Identifier, then click the create button at the bottom of the screen.
Did you know?
Machine-to-machine (M2M) is when one program calls another program without a person logging in. For example, a nightly report job might read data from your API, or a backend service might create records. Since no person can type a password, the program authenticates as itself with a Client ID and Client Secret. This is the OAuth client credentials flow:
- The program sends its Client ID and Client Secret to the Auth0 token URL.
- Auth0 returns an access token carrying the permission you granted that app.
- The program calls your API with
Authorization: Bearer <token>
Next, we'll use a SAM template to create an API Gateway and use JWT authorizers as our authorizer.
** Infrastructure as Code (IAC)**
There are four sections in template.yml. We'll go over each one to understand what it's for.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: REST API secured with an API Gateway JWT authorizer (Auth0 as OIDC issuer)
Parameters:
Auth0Issuer:
Type: String
Description: >-
Auth0 issuer URL exactly as it appears in the token's iss claim, including the
trailing slash, e.g. https://my-tenant.us.auth0.com/ (the URL may include a region code).
AllowedPattern: '^https://.+/$'
ConstraintDescription: Must be an https URL ending with a trailing slash.
Auth0Audience:
Type: String
Description: Auth0 API Identifier (the audience of the access tokens).
MinLength: 1
StageName:
Type: String
Default: test
Description: API Gateway stage name.
DeploymentVersion:
Type: String
Default: '1'
Description: >-
Change this value (e.g. 2, 3) to force a new API deployment after editing the
authorizer or methods, because API Gateway changes only take effect in a new deployment.
The first two sections allow you to define the template version you are using. The Parameters section is where you specify values that will be used by the Resources section. These values can either have default values or be supplied through samconfig.toml or the SAM CLI.
Resources:
BooksApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: !Sub '${AWS::StackName}-books'
EndpointConfiguration:
Types: [REGIONAL]
BooksResource:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId: !Ref BooksApi
ParentId: !GetAtt BooksApi.RootResourceId
PathPart: books
BooksAuthorizer:
Type: AWS::ApiGateway::Authorizer
Properties:
RestApiId: !Ref BooksApi
Name: books-jwt
Type: JWT
IdentitySource: method.request.header.Authorization
JwtConfiguration:
DefaultAlgorithms: [RS256]
Issuers:
- Issuer: !Ref Auth0Issuer
Audiences: [!Ref Auth0Audience]
BooksFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/books/
Handler: app.lambda_handler
Runtime: python3.12
Architectures: [arm64]
MemorySize: 128
Timeout: 5
BooksFunctionInvokePermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref BooksFunction
Principal: apigateway.amazonaws.com
SourceArn: !Sub 'arn:${AWS::Partition}:execute-api:${AWS::Region}:${AWS::AccountId}:${BooksApi}/*/*/books'
GetBooks:
Type: AWS::ApiGateway::Method
Properties:
RestApiId: !Ref BooksApi
ResourceId: !Ref BooksResource
HttpMethod: GET
AuthorizationType: JWT
AuthorizerId: !GetAtt BooksAuthorizer.AuthorizerId
AuthorizationScopes: [books:read]
Integration:
Type: AWS_PROXY
IntegrationHttpMethod: POST
Uri: !Sub 'arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${BooksFunction.Arn}/invocations'
PostBooks:
Type: AWS::ApiGateway::Method
Properties:
RestApiId: !Ref BooksApi
ResourceId: !Ref BooksResource
HttpMethod: POST
AuthorizationType: JWT
AuthorizerId: !GetAtt BooksAuthorizer.AuthorizerId
AuthorizationScopes: [books:write]
Integration:
Type: AWS_PROXY
IntegrationHttpMethod: POST
Uri: !Sub 'arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${BooksFunction.Arn}/invocations'
BooksDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn: [GetBooks, PostBooks]
Properties:
RestApiId: !Ref BooksApi
Description: !Sub 'v${DeploymentVersion} issuer=${Auth0Issuer} audience=${Auth0Audience}'
BooksStage:
Type: AWS::ApiGateway::Stage
Properties:
RestApiId: !Ref BooksApi
DeploymentId: !Ref BooksDeployment
StageName: !Ref StageName
This section defines the resources we will build. In this example, we create a REST API with an API Gateway resource, an API Gateway authorizer, and two Lambda functions that handle the API's business logic. We will also define two API Gateway methods, an API Gateway deployment, and an API Gateway stage.
BooksAuthorizer:
Type: AWS::ApiGateway::Authorizer
Properties:
RestApiId: !Ref BooksApi
Name: books-jwt
Type: JWT
IdentitySource: method.request.header.Authorization
JwtConfiguration:
DefaultAlgorithms: [RS256]
Issuers:
- Issuer: !Ref Auth0Issuer
Audiences: [!Ref Auth0Audience]
This is where we configure the JwtConfiguration for the API Gateway authorizer. It specifies the default signing algorithms that the authorizer accepts and defines the allowed issuer. You can configure up to 10 issuers.
GetBooks:
Type: AWS::ApiGateway::Method
Properties:
RestApiId: !Ref BooksApi
ResourceId: !Ref BooksResource
HttpMethod: GET
AuthorizationType: JWT
AuthorizerId: !GetAtt BooksAuthorizer.AuthorizerId
AuthorizationScopes: [books:read]
Integration:
Type: AWS_PROXY
IntegrationHttpMethod: POST
Uri: !Sub 'arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${BooksFunction.Arn}/invocations'
PostBooks:
Type: AWS::ApiGateway::Method
Properties:
RestApiId: !Ref BooksApi
ResourceId: !Ref BooksResource
HttpMethod: POST
AuthorizationType: JWT
AuthorizerId: !GetAtt BooksAuthorizer.AuthorizerId
AuthorizationScopes: [books:write]
Integration:
Type: AWS_PROXY
IntegrationHttpMethod: POST
Uri: !Sub 'arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${BooksFunction.Arn}/invocations'
Both GET and POST methods accept only an authority-type JWT; the scope is books:write for POST and books:read for GET.
Deploy
Run sam build to make sure everything is good, then use the command below to deploy. Replace tenant with a previous Auth0 setup.
sam deploy --guided \
--parameter-overrides Auth0Issuer=https://YOUR_TENANT.us.auth0.com/ Auth0Audience=https://books.example.com
Note your ApiUrl output; we will use it to test.
Testing
I ran the command below, but be sure to replace those values with those from your setup.
export API_URL=<ApiUrl output>
export TOKEN_URL=https://YOUR_TENANT.us.auth0.com/oauth/token
export AUDIENCE=https://books.example.com
export READER_ID=... READER_SECRET=... WRITER_ID=... WRITER_SECRET=...
./scripts/test.sh
This is the terminal output. I was wondering why I got a 403. It turns out I needed two separate applications: one for read and one for write, each with the appropriate permissions.

Let's fix this!
I created another application for writing and kept the first one for reading. I scoped the appropriate permissions and ran the test command again. It all passed. Here is how it looks on Auth0 and the output after running a test.
This is how it looks in the API Gateway console.
Author word
I really like being able to authenticate requests without writing a Lambda function. API Gateway now checks the token's signature, issuer, audience, expiry, and scopes before a request ever reaches my code. That lets me move the remaining validation, like "can this caller edit this book?", into the backend, where the business context lives. The result is less code to maintain, no extra Lambda invocation per request, and a clear split between API Gateway handling authentication and coarse-grained permissions, and my backend handling the fine-grained rules.
Here is the GitHub: https://github.com/nadtakanfuthoem/api-gw-jwt-authorizers
Resource: https://aws.amazon.com/blogs/compute/securing-rest-apis-with-jwt-authorizers-in-amazon-api-gateway/




Top comments (1)
This is a great step toward simplifying serverless security! One thing I particularly like is keeping authentication and authorization separate. Letting API Gateway handle token validation while leaving resource-level permissions to the backend gives a much cleaner architecture. Less custom code, fewer moving parts, and easier maintenance.