Skip to content

API Gateway import openAPI for external API and use lambda function to authenticate

0

I have a 3rd party API to use in AWS Connect (High Level) that I have created a lambda function to reauthenticate but how do i use the import of the openAPI to the 3rd party using the authentication returned from the lambda function. All the searching keeps coming back with how to secure an API created inside of AWS not to use externally. Am I approaching this all wrong?

From what I figured out i need to update each endpoint of the API which seems excessive when they all have the same authorization and base URL.

1 Answer
0

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:

  1. First, import your OpenAPI definition into API Gateway. This will create the structure of your API based on the external API's endpoints.

  2. 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.

  3. In your OpenAPI definition, you can define a security scheme that uses this Lambda authorizer. You'll use the x-amazon-apigateway-authorizer extension to specify your Lambda function as the authorizer.

  4. 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.

  5. 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-integration extension 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

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.