How to implement redirect (301 code) mock in serverless framework config (for AWS) without lambda

邮差的信 提交于 2021-02-10 20:12:56

问题


I'd like that the root path of my API redirect (301) to completely another site with docs. So I have a lambda at e.g /function1 path and the / should return code 301 with another location. And I'd like to do it without another lambda.

This is exactly what is described here, but via aws command line tool. I tried this approach - it works perfectly, but I'd like to configure such API gateway mock via serverless framework config.


回答1:


Fortunately, the series of CLI commands you linked to can be reproduced in CloudFormation, which can then be dropped into the Resources section of your Serverless template.

In this example, a GET to /function1 will invoke a lambda function, while a GET to / will return a 301 to a well-known search engine.

service: sls-301-mock

provider:
  name: aws
  runtime: nodejs12.x
  stage: dev
  region: us-east-1

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: function1
          method: get

resources:
  Resources:
    Method:
      Type: AWS::ApiGateway::Method
      Properties:
        HttpMethod: GET
        ResourceId: 
          !GetAtt ApiGatewayRestApi.RootResourceId
        RestApiId: 
          Ref: ApiGatewayRestApi
        AuthorizationType: NONE
        MethodResponses:
          - ResponseModels: {"application/json":"Empty"}
            StatusCode: 301
            ResponseParameters: 
              "method.response.header.Location": true
        Integration:
          Type: MOCK
          RequestTemplates:
            "application/json": "{\n \"statusCode\": 301\n}"         
          IntegrationResponses:
            - StatusCode: 301
              ResponseParameters:
                "method.response.header.Location": "'https://google.com'"

Tested with:

Framework Core: 1.62.0
Plugin: 3.3.0
SDK: 2.3.0
Components Core: 1.1.2
Components CLI: 1.4.0

Notes

ApiGatewayRestApi is, by convention, the logical name of the API Gateway Stage resource created by Serverless on account of the http event.

Relevant CloudFormation documentation

ApiGateway::Method

ApiGateway::Method Integration


EDIT

This answer is not as verbose, and uses an http event instead of the Resources section. I haven't tested it, but it may also work for you.




回答2:


Managed to achieve it with referring one function twice. But see also reply of Mike Patrick - looks more universal

    ...
    events:
      - http:
          path: main/root/to/the/function
          method: get
          cors: true
      - http:
          path: /
          method: get
          integration: mock
          request:
            template:
              application/json: '{"statusCode": 301}'
          response:
            template: redirect
            headers:
              Location: "'my-redirect-url-note-the-quotes-they-are-important'"
            statusCodes:
              301:
                pattern: ''


来源:https://stackoverflow.com/questions/59904227/how-to-implement-redirect-301-code-mock-in-serverless-framework-config-for-aw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!