How to enable cache in AWS APIGateway using OpenApi yaml?

Viewed 409

I have a yaml file for the API Gateway integration like this:

  /users/{userId}:
    get:
      description: Get user details
      parameters:
        - description: Valid user id
          in: path
          name: userId
          required: true
          schema:
            type: string
      responses:
        '200':
          content: {}
          description: Success
          headers:
            Access-Control-Allow-Origin:
              schema:
                type: string
        '400':
          content: {}
          description: Invalid user id
      security:
        - NodeJsAuth: []
      summary: Get User Id Details
      tags:
        - User
      x-amazon-apigateway-integration:
        connectionId: ${stageVariables.vpcLinkId}
        connectionType: VPC_LINK
        httpMethod: GET
        passthroughBehavior: when_no_match
        requestParameters:
          integration.request.path.userId: method.request.path.userId
        responses:
          default:
            responseParameters:
              method.response.header.Access-Control-Allow-Origin: '''*'''
            statusCode: '200'
        type: http_proxy
        uri: '#{EksElbUri}#/user/{userId}'

I want to enable cache for this method. I know I can enable it manually but I want to do it in YAML file so it could be easier to execute.

I have my stage enabled the cache already (via AWS Console UI). However I could not find anywhere that is mentioning how to enable cache for this method ONLY.

I am aware that once enabled the cache in stage, it will enabled for all GET endpoints and I have many GET endpoints but I only want this one to be cached.

Is there a way that I can specify the cache in OPEN-API so I no need to disable all the other GET endpoint one by one manually?

Thank you.

1 Answers

What will you'll find easier is edit the API in the API GW Console, enabling cache. Deploy the API to your stage. Then go to Stages -> your stage, and export the API in whatever format you are currently editing in. This is what I do saves you having indentation problems, and wasting time trying to find the exact equivalent syntax.

Once you have done that once your file is easier to edit and you can typically figure out the edits you want to make.

Related