- Newest
- Most votes
- Most comments
It sounds like you're on the right track, but there are a few key points to consider for your specific use case.
When importing an OpenAPI definition for an external API into Amazon API Gateway, you can indeed use a Lambda function for authentication. Here's how you can approach this:
-
First, import your OpenAPI definition into API Gateway. This will create the structure of your API based on the external API's endpoints.
-
Next, you'll need to set up a Lambda authorizer (also known as a custom authorizer) that will handle the authentication. This Lambda function will receive the incoming request details and should return an IAM policy that allows or denies access to the API.
-
In your OpenAPI definition, you can define a security scheme that uses this Lambda authorizer. You'll use the
x-amazon-apigateway-authorizerextension to specify your Lambda function as the authorizer. -
Apply this security scheme to all the endpoints in your API. You don't need to update each endpoint individually - you can apply the security scheme at the global level in your OpenAPI definition.
-
For the actual integration with the external API, you'll use HTTP integration in API Gateway. The authorization token returned by your Lambda function can be passed to the external API using the
x-amazon-apigateway-integrationextension in your OpenAPI definition.
Here's a simplified example of how your OpenAPI definition might look:
openapi: 3.0.0 info: title: External API version: 1.0.0 paths: /{proxy+}: x-amazon-apigateway-any-method: security: - LambdaAuthorizer: [] x-amazon-apigateway-integration: uri: 'https://external-api.com/{proxy}' type: http_proxy httpMethod: ANY components: securitySchemes: LambdaAuthorizer: type: apiKey name: Authorization in: header x-amazon-apigateway-authorizer: type: request identitySource: method.request.header.Authorization authorizerUri: arn:aws:apigateway:region:lambda:path/2015-03-31/functions/arn:aws:lambda:region:account-id:function:auth-function/invocations
In this setup, all requests will go through the Lambda authorizer before being proxied to the external API. The Lambda function can perform the necessary authentication and return the appropriate credentials or tokens for the external API.
This approach allows you to centralize your authentication logic in the Lambda function while still leveraging the structure of the external API as defined in your OpenAPI specification.
Sources
Set up a proxy resource with Lambda proxy integration with an OpenAPI definition - Amazon API Gateway
x-amazon-apigateway-authtype property - Amazon API Gateway
Develop REST APIs using OpenAPI in API Gateway - Amazon API Gateway
answered a year ago
