cancel
Showing results for 
Search instead for 
Did you mean: 

OAS3, Lambda and method re-use

SOLVED
Occasional Contributor

OAS3, Lambda and method re-use

I've found an odd constraint in Swagger.

We have a pre-existing API that uses Lambda to handle method requests.  Many of our endpoints will call the same Lambda method with several Methods.  So that PUT, POST and DELETE will all be served by the same Lambda method.

In trying to define the API in Swagger/OAS, apparently I need to use the `operationID` operation to reference the name of the Lambda method.  However, this `operationID` is required to be unique, whereas our Lambda methods are designed to be re-used wherever possible.

How can I reference the same Lambda method from several API Methods,such as PUT, POST, etc?

Put another way, If we are to use Swagger/OAS we must break up our Lambda methods into multiple methods?

Or we can't use Swagger.  

This seems like a bizarre constraint.  

I really like Swagger, but this seems like a complete showstopper.

Am I missing something?  Does anyone else find this odd?

1 ACCEPTED SOLUTION

Accepted Solutions
Moderator

Re: OAS3, Lambda and method re-use

Hi Kevin,

 

AWS and Lambda bindings can be configured by using the "x-amazon-apigateway-integration" extensions. "operationId" is only used in the absence of the x-amazon-... extensions.

 

To bind different operations to the same Lambda function, specify the function name in the "uri" property inside "x-amazon-apigateway-integration". The "uri" value looks like this:

x-amazon-apigateway-integration:
  type: aws
  uri: "arn:aws:apigateway:REGION:lambda:path/2015-03-31/functions/arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME/invocations"
  ...

 

Here's an example with two endpoints, /foo and /bar, which are bound to the same Lambda function named "Foo". Make sure to replace the REGION and ACCOUNT_ID with yours. Hope this helps!

swagger: "2.0"
info:
  title: Lambda function binding test
  version: 1.0.0
produces:
- application/json
paths:
  /foo:
    get:
      operationId: foo
      responses:
        200:
          description: OK
      x-amazon-apigateway-integration:
        type: aws
        passthroughBehavior: when_no_match
        httpMethod: POST
        uri: "arn:aws:apigateway:REGION:lambda:path/2015-03-31/functions/arn:aws:lambda:REGION:ACCOUNT_ID:function:Foo/invocations"
        responses:
          default:
            statusCode: 200
        parameters: {}
        requestTemplates: {}
  /bar:
    get:
      operationId: bar
      responses:
        200:
          description: OK
      x-amazon-apigateway-integration:
        type: aws
        passthroughBehavior: when_no_match
        httpMethod: POST
        uri: "arn:aws:apigateway:REGION:lambda:path/2015-03-31/functions/arn:aws:lambda:REGION:ACCOUNT_ID:function:Foo/invocations"
        responses:
          default:
            statusCode: 200
        parameters: {}
        requestTemplates: {}

Helen Kosova
SmartBear Technical Writer
________________________
Vote up helpful replies.
Accept this reply if you think it's the best solution to your question.
2 REPLIES 2
Moderator

Re: OAS3, Lambda and method re-use

Hi Kevin,

 

AWS and Lambda bindings can be configured by using the "x-amazon-apigateway-integration" extensions. "operationId" is only used in the absence of the x-amazon-... extensions.

 

To bind different operations to the same Lambda function, specify the function name in the "uri" property inside "x-amazon-apigateway-integration". The "uri" value looks like this:

x-amazon-apigateway-integration:
  type: aws
  uri: "arn:aws:apigateway:REGION:lambda:path/2015-03-31/functions/arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME/invocations"
  ...

 

Here's an example with two endpoints, /foo and /bar, which are bound to the same Lambda function named "Foo". Make sure to replace the REGION and ACCOUNT_ID with yours. Hope this helps!

swagger: "2.0"
info:
  title: Lambda function binding test
  version: 1.0.0
produces:
- application/json
paths:
  /foo:
    get:
      operationId: foo
      responses:
        200:
          description: OK
      x-amazon-apigateway-integration:
        type: aws
        passthroughBehavior: when_no_match
        httpMethod: POST
        uri: "arn:aws:apigateway:REGION:lambda:path/2015-03-31/functions/arn:aws:lambda:REGION:ACCOUNT_ID:function:Foo/invocations"
        responses:
          default:
            statusCode: 200
        parameters: {}
        requestTemplates: {}
  /bar:
    get:
      operationId: bar
      responses:
        200:
          description: OK
      x-amazon-apigateway-integration:
        type: aws
        passthroughBehavior: when_no_match
        httpMethod: POST
        uri: "arn:aws:apigateway:REGION:lambda:path/2015-03-31/functions/arn:aws:lambda:REGION:ACCOUNT_ID:function:Foo/invocations"
        responses:
          default:
            statusCode: 200
        parameters: {}
        requestTemplates: {}

Helen Kosova
SmartBear Technical Writer
________________________
Vote up helpful replies.
Accept this reply if you think it's the best solution to your question.
Occasional Contributor

Re: OAS3, Lambda and method re-use

Thank you.  -K

New Here?
Join us and watch the welcome video:
API Testing Mistake #2
APITestingMistake#2