2

How to integrate gateway response using serverless framework?

I'm not able to find documentation in serverless.com as well as in plugin serverless-aws-documentation

enter image description here

1
  • 1
    Gateway responses is a new feature and may not be supported yet. You may want to reach out on the GitHub project to request support: github.com/serverless/serverless/issues Commented Jun 12, 2017 at 13:56

2 Answers 2

3

Added in resources section of serverless.yml as below

resources:
  Resources:
    Unauthorized:
      Type: "AWS::ApiGateway::GatewayResponse"
      Properties:
        ResponseParameters:
          gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
          gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
        ResponseTemplates:
          "application/json": _${file(gateway-responses/responses.js):unauthorized}
        ResponseType: UNAUTHORIZED
        RestApiId: _${file(serverless.env.yml):_${self:provider.stage}.REST_API_ID}
        StatusCode: '401'
    Forbidden:
      Type: "AWS::ApiGateway::GatewayResponse"
      Properties:
        ResponseParameters:
          gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
          gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
        ResponseTemplates:
          "application/json": _${file(gateway-responses/responses.js):forbidden}
        ResponseType: ACCESS_DENIED
        RestApiId: _${file(serverless.env.yml):_${self:provider.stage}.REST_API_ID}
        StatusCode: '403'
Sign up to request clarification or add additional context in comments.

Comments

-1

Good question. I believe you can do it in the s-function.json file like so:

"endpoints": [
{
  "path": "/test",
  "method": "GET",
  "authorizationType": "none",
  "apiKeyRequired": false,
  "requestParameters": {},
  "requestTemplates": {},
  "responses": {
    "400": {
      "statusCode": "400"
    },
    "default": {
      "statusCode": "200",
      "responseParameters": {},
      "responseModels": {"text/html": "Empty"},
      "responseTemplates": {"text/html": "$input.json('$')"},
      "text/html": ""
    }
  }
}
]

Sources:

https://github.com/serverless/serverless/issues/587 https://github.com/serverless/serverless/issues/463

1 Comment

This is related to Gateway Response not method response , so this answer will not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.