Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-use my x-amazon-apigateway-integration definition throughout Swagger YAML document?

I am currently using Swagger to define an API with many end-points, and each one of those end-points all have the same definition for the 'x-amazon-apigateway-integration' key. I would like to define this somewhere in the document, and re-use that definition through-out.

Either I am not understanding how the definition should be defined, I am not placing it in the correct location or a mix of the two. I have tried defining this definition within 'definitions', and as some alias under it's own key. The definition (with key information removed) is:

x-amazon-apigateway-integration:
  responses:
    default:
      statusCode: '200'
  passthroughBehavior: when_no_match
  httpMethod: POST
  uri: >-
    arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/<lambda arn>/invocations
  credentials: '<role arn>'
  type: aws     
  requestTemplates: "application/json": "<object definition>"  

I have tried defining this as an alias under it's own key (not definitions, but the same base scope):

amazon:
  Amazon: &Amazon
    - responses:
        default:
          statusCode: '200'
    - passthroughBehavior: when_no_match
    - httpMethod: POST
    - uri: >-
    arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/<lambda arn>/invocations
    - credentials: '<role arn>'
    - type: aws     
    - requestTemplates:
      "application/json": "<object definition>"

To use, I have the following:

x-amazon-apigateway-integration:
  *Amazon

The error received on API Gateway import is 'Unable to parse API definition because of a malformed integration at path /'

I have also tried defining this under 'definitions', and using 'ref' to access it:

definitions:
  Amazon:
    type: object
    x-amazon-apigateway-integration:
      responses:
        default:
          statusCode: '200'
      passthroughBehavior: when_no_match
      httpMethod: POST
      uri: >-
          arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/<lambda arn>/invocations
      credentials: '<role arn>'
      type: aws     
      requestTemplates:
        "application/json": "<object definition>"   

To use, I have the following:

x-amazon-apigateway-integration:
  $ref: '#/definitions/Amazon'

On import to API Gateway I receive the following error(s):

Your API was not imported due to errors in the Swagger file.

  • Unable to create model for 'Amazon': Invalid model specified: Validation Result: warnings : [], errors : [Invalid model schema specified. Unsupported keyword(s): ["x-amazon-apigateway-integration"]]
  • Additionally, these warnings were found:
  • Unknown integration type 'null' for 'POST /'. Ignoring.

Thank you in advance for your help.

like image 665
MikeO Avatar asked Nov 14 '18 09:11

MikeO


People also ask

How do you integrate Swagger with AWS API gateway?

Select Amazon API Gateway Integration from the list of integrations. Enter integration parameters: Name – A display name for this integration. AWS Region – Select the AWS region where you want to publish your API or that contains the existing API you want to publish to.

Does AWS API gateway support Swagger?

As mentioned before, AWS API Gateway can be configured by using API specifications written in Swagger. Additionally, a set of extensions have been defined for the API Gateway to capture most of its specific properties, like integrating Lambda functions or using Authorizers.

How do you call step function from API gateway?

Before you create your API Gateway API, you need to give API Gateway permission to call Step Functions API actions. Sign in to the IAM console and choose Roles, Create role. On the Select type of trusted entity page, under AWS service, select API Gateway from the list, and then choose Next: Permissions.

Which format is supported for API definition files AWS?

Currently, API Gateway supports OpenAPI v2. 0 and OpenAPI v3. 0 definition files.


1 Answers

Using YAML anchors seems like a good idea. The correct syntax is as follows.

Add the following on the root level of your OpenAPI file:

x-definitions:      # <--- "x-" before "definitions" prevents it from being
                    #      attempted to be parsed as an OpenAPI Schema object.
  Amazon:
    type: object
    x-amazon-apigateway-integration: &Amazon   # <--- "&Amazon" is the anchor
      responses:
        default:
          statusCode: '200'
      passthroughBehavior: when_no_match
      httpMethod: POST
      uri: >-
          arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/<lambda arn>/invocations
      credentials: '<role arn>'
      type: aws     
      requestTemplates:
        "application/json": "<object definition>" 

Then you can refer to the anchor like this:

x-amazon-apigateway-integration: *Amazon

However, it might be that AWS parser does not support YAML anchors (&..., *...). In that case you can try pre-processing your definition using a parser that can resolve YAML anchors and then feed the resolved file to AWS.

like image 62
Helen Avatar answered Nov 09 '22 18:11

Helen