Using a Lambda for Authorization and Authentication on AWS API Gateway
Updated: 03 September 2023
Custom Authorizers
API Gateway allows us to handle auth by way of a lambda. AWS has two types of authorization lambdas we can use, namely:
SIMPLE - returns a message stating whether a user is authorized along with a context object
IAM - returns an IAM Policy Document stating user/resource access
We’ll be discussing at the former since it’s significantly simpler (hence the name) and is fairly poorly documented on the interwebs
Also note that I’m using SST for the definition of the Function and Api but the general concept still applies at a broader API Gateway and CDK Stack
The Authorizer Lambda
Note that the @types/aws-lambda package does not have a type def for the SIMPLE authorizer, and so I’ve provided the authorizer in JavaScript in order to keep things to the point, but in practice you should probably write more concrete types for the lambda
The expected return value for the Authorizer in SIMPLE mode looks like this:
If we want to create an Authorizer Lambda that checks for a username in the Authorization header for example, we can do something Like the below:
src/lambda/auth.js
Lastly, if you’re hooking things up manually you can find the Authorizer Settings in API Gateway for your specific API and Lambda, but if you’re using CDK/SST look to the next section for how to integrate this into your stack
The Stack
If, like me, you’re using SST for creating your API and would like to configure your Authorizer using that, you can simply add the following to your stack and attaching it to your API
PolicyDocument Based Authorizers
In the context of Authorizers we can also have the PolicyDocument based authorizers which is the typical implementation. Without any explanation this is what one of those would look like: