Forum Discussion

kevinashaw's avatar
kevinashaw
Occasional Contributor
5 years ago
Solved

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 tha...
  • HKosova's avatar
    5 years ago

    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: {}