Hi In SST, is it possible to use API key to authe...
# help
v
Hi In SST, is it possible to use API key to authenticate lambda with API Gateway ? (Couldn’t find any reference to api key in the doc)
a
API Key Auth is directly available for the AppSync API. AWS itself doesn’t provide API key based auth for API Gateway v1 or v2. With API Gateway v2 i.e. the
sst.Api
construct you could achieve this using
HttpLambdaAuthorizer
as given in the example here - https://docs.serverless-stack.com/constructs/Api#adding-lambda-authorization. You could set the
HttpLambdaAuthorizer
’s response type to
SIMPLE
to authorize requests by just returning a boolean. For reference check this out - https://docs.aws.amazon.com/cdk/api/latest/docs/aws-apigatewayv2-authorizers-readme.html#lambda-authorizers.
@Jay it would be great if we could include the simple response type example for Lambda Authorizers in our docs as provided here - https://docs.aws.amazon.com/cdk/api/latest/docs/aws-apigatewayv2-authorizers-readme.html#lambda-authorizers.
v
Hi @Ashishkumar Pandey Thanks for the response. https://www.serverless.com/framework/docs/providers/aws/events/apigateway/#setting-api-keys-for-your-rest-api Please have a look at above link, this where I'm coming from.
a
Ah! I figured it out, what serverless is doing is creating a usage plan and binding it to the
RestApi
, here’s how you can do that using the cdk - https://docs.aws.amazon.com/cdk/api/latest/docs/aws-apigateway-readme.html#usage-plan--api-keys. You’ll have to use the
sst.ApiGatewayV1
construct for this though. HTTP APIs i.e.
sst.Api
doesn’t support usage plans.
v
Got it. Thanks @Ashishkumar Pandey
f
@Vishal Vijay yeah, you can add a custom authorizer for
Api
and manage the API keys urself inside a Lambda - https://docs.serverless-stack.com/constructs/Api#adding-lambda-authorization
The authorizer code looks something like:
Copy code
export const main = async (event) => {
  const authHeader = event.headers.authorization;
  const apiKey = ...; // parse api key from auth header
  const isAuthorized = ...; // check ie. against DynamoDB and see if the key is valid

  return { isAuthorized };
};
v
Thanks for the suggestion @Frank. I'm aware of this approach, basically I was trying to see if I can use API Gateway default feature (usage plan) for this.
f
Ah yeah. That’s only supported in API Gateway REST API. You’d need to use
sst.ApiGatewayV1Api
for that.
v
Okay 👍
t
I implemented my Authorizer with TS:
Copy code
import {
    APIGatewayRequestAuthorizerEventHeaders,
    Handler
} from 'aws-lambda';

interface APIGatewaySimpleAuthorizerEvent {
    headers: APIGatewayRequestAuthorizerEventHeaders | null;
}

interface APIGatewaySimpleAuthorizerResult {
    isAuthorized: boolean
}

/**
 * @link <https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-lambda-authorizer.html>
 */
export const handler: Handler<APIGatewaySimpleAuthorizerEvent, APIGatewaySimpleAuthorizerResult> = async (event) => {
    return handleLogic(event);
};

export function handleLogic(event: APIGatewaySimpleAuthorizerEvent) {
    // your logic here
}
it works as expected with V2