Mock integration endpoint

aws

#1

Does anyone have any examples on how to setup a mock integration request? I haven’t been able to find any in the forum or documentation. I’m guessing I will somehow need to set it up in the resources section of my serverless template.


#2

I took the HTTP proxy example and modified to implement MOCK integration until it matched what I wanted. I used the solution suggested here to pass in secrets: Storing Database credentials securely

If your method is anything other than a basic GET or POST, an OPTIONS method for CORS will also be needed.

resources:
  Resources:
    MockResource:
      Type: AWS::ApiGateway::Resource
      Properties:
        ParentId:
          Fn::GetAtt:
            - ApiGatewayRestApi # our default Rest API logical ID
            - RootResourceId
        PathPart: yourpath # the endpoint in your API that is set as proxy
        RestApiId:
          Ref: ApiGatewayRestApi
    MockMethod:
      Type: AWS::ApiGateway::Method
      Properties:
        AuthorizationType: None
        HttpMethod: GET
        ResourceId:
          Ref: MockResource
        RestApiId:
          Ref: ApiGatewayRestApi
        MethodResponses:
          - ResponseModels:
              application/json: Empty
            ResponseParameters:
              method.response.header.Access-Control-Allow-Origin: true
            StatusCode: 200
        Integration:
          RequestTemplates:
            application/json: |
              {"statusCode": 200}
          Type: MOCK
          IntegrationResponses:
            - ResponseParameters:
                method.response.header.Access-Control-Allow-Origin: "'*'"
              ResponseTemplates:
                application/json: |
                  {
                    "number": 12345,
                    "data": {
                      "identitypool": "${self:custom.secrets.IDENTITYPOOL}",
                      "region": "${self:custom.secrets.REGION}",
                      "poolclient": "${self:custom.secrets.POOLCLIENT}",
                      "userpool": "${self:custom.secrets.USERPOOL}"
                    }
                  }
              StatusCode: 200

#3

If anyone else comes across this… Just spent a few hours trying to figure this out.

I needed to add a mock HEAD response to an existing endpoint at ‘users/{id}’. After a bit of trial and error, I looked at the generated “cloudformation-template-create-stack.json” that gets created for my existing endpoints and followed that template.

Below is the answer. Note that the key part is the referencing the existing api using the serverless naming schema - ApiGatewayResource{ YOUR_FUNCTION_NAME }{DYNAMIC_VAR_NAME}Var

resources:
  Resources:
    MockMethod:
      Type: AWS::ApiGateway::Method
      Properties:
        AuthorizationType: None
        HttpMethod: HEAD
        ResourceId:
          Ref: ApiGatewayResourceUsersIdVar
        RestApiId:
          Ref: ApiGatewayRestApi
        MethodResponses:
          - ResponseModels:
              application/json: Empty
            ResponseParameters:
              method.response.header.Access-Control-Allow-Origin: true
            StatusCode: 200
        Integration:
          RequestTemplates:
            application/json: |
              {"statusCode": 200}
          Type: MOCK
          IntegrationResponses:
            - ResponseParameters:
                method.response.header.Access-Control-Allow-Origin: "'*'"
              ResponseTemplates:
                application/json: |
                  {}
              StatusCode: 200