Skip to content

Instantly share code, notes, and snippets.

@og24715
Last active January 10, 2020 00:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save og24715/31abb1782ea9d3cc3132d443eb252075 to your computer and use it in GitHub Desktop.
Save og24715/31abb1782ea9d3cc3132d443eb252075 to your computer and use it in GitHub Desktop.
Setting an HTTP Proxy on API Gateway by using Serverless framework.

HTTP オールスループロキシーを serverless で定義する

service: httpAllThroughProxy

provider:
  name: aws
  runtime: nodejs6.10

  stage: dev
  region: ap-northeast-1

resources:
  Resources:
    ApiGatewayRestApi:
      Type: AWS::ApiGateway::RestApi
      Properties:
        Name: test-api-gateway
        Description: 'The main entry point of test APIs'
    MainApiBasePath:
      Type: AWS::ApiGateway::Resource
      Properties:
        ParentId:
          Fn::GetAtt:
            - ApiGatewayRestApi
            - RootResourceId
        PathPart: 'main-api'
        RestApiId:
          Ref: ApiGatewayRestApi
    MainApiProxyPath:
      Type: AWS::ApiGateway::Resource 
      Properties:
        ParentId:
          Ref: MainApiBasePath
        PathPart: '{proxy+}'
        RestApiId:
          Ref: ApiGatewayRestApi
    MainApiProxyAnyMethod:
      Type: AWS::ApiGateway::Method 
      Properties:
        AuthorizationType: NONE
        HttpMethod: ANY
        Integration: 
          IntegrationHttpMethod: ANY
          Type: HTTP_PROXY
          Uri: <existing_api_endpoint>/{proxy}
          PassthroughBehavior: WHEN_NO_MATCH
          RequestParameters:
            'integration.request.path.proxy': 'method.request.path.proxy'
        MethodResponses:
          - StatusCode: 200
        RequestParameters:
            'method.request.path.proxy': true
        ResourceId:
          Ref: MainApiProxyPath
        RestApiId:
          Ref: ApiGatewayRestApi

[Feature Proposal] Support HTTP Proxy Api Gateway Integration · Issue #4539 · serverless/serverless

ApiGatewayRestApi

ApiGatewayRestApi:
  Type: AWS::ApiGateway::RestApi
  Properties:
    Name: test-api-gateway
    Description: 'The main entry point of test APIs'

Type

AWS::ApiGateway::RestApi

The AWS::ApiGateway::RestApi resource contains a collection of Amazon API Gateway resources and methods that can be invoked through HTTPS endpoints. AWS::ApiGateway::RestApi - AWS CloudFormation

リソースやメソッドを内包するガワを作るようなものだと捉えてよさそう。

なのでメソッドやリソースはこれに関連付ける形で作成することになる。

Properties

名前と説明だけなので割愛

MainApiBasePath

MainApiBasePath:
  Type: AWS::ApiGateway::Resource
  Properties:
    ParentId:
      Fn::GetAtt:
        - ApiGatewayRestApi
        - RootResourceId
    PathPart: 'main-api'
    RestApiId:
      Ref: ApiGatewayRestApi

Type

AWS::ApiGateway::Resource

API Gatewayにリソースを作成する

Properties

ParentId

親リソース(ルート)の場合
ParentId:
  Fn::GetAtt:
    - RootResourceId
    - ApiGatewayRestApi # REST APIのID
子リソース(リソースの入れ子)の場合
ParentId: 
  Ref: ParentApiPathId # REST APIのID

PathPart

リソースのパス名

PathPart: 'main-api'

RestApiId

RestApiId: Ref: ApiGatewayRestApi

このリソースを作成するREST APIのID

ここではApiGatewayRestApiMainApiBasePathで定義したmain-apiのリソースを作成したことになる

MainApiProxyPath

MainApiProxyPath:
  Type: AWS::ApiGateway::Resource 
  Properties:
    ParentId:
      Ref: MainApiBasePath
    PathPart: '{proxy+}'
    RestApiId:
      Ref: ApiGatewayRestApi

Type

割愛

Properties

ParentId

MainApiBasePathの子リソースとして定義

PathPart

/main-api/{proxy+}の形でプロキシーを定義

ちなみに

/main-api/{proxy+}というリソースを作るのであれば初めから

MainApiProxyPath:
  Type: AWS::ApiGateway::Resource
  Properties:
    ParentId:
      Fn::GetAtt:
        - ApiGatewayRestApi
        - RootResourceId
    PathPart: 'main-api/{proxy+}'
    RestApiId:
      Ref: ApiGatewayRestApi

と、してしまいたくなるがこれはダメ🙅

sls deployすると次のようなエラーが返る

Serverless Error ---------------------------------------

  An error occurred: MainApiProxyPath - Resource's path part only allow a-zA-Z0-9._- and curly braces at the beginning and the end..

なぜなら、PathPartにはa-zA-Z0-9._-と最初と最後の{}しか許されていないことから分かるように、main-api/{proxy+}main-apiリソースの子リソースであるから、親と子は別々に定義する必要がある

RestApiId

割愛

MainApiProxyAnyMethod

MainApiProxyAnyMethod:
  Type: AWS::ApiGateway::Method 
  Properties:
    AuthorizationType: NONE
    HttpMethod: ANY
    Integration: 
      IntegrationHttpMethod: ANY
      Type: HTTP_PROXY
      Uri: <existing_api_endpoint>/{proxy}
      PassthroughBehavior: WHEN_NO_MATCH
      RequestParameters:
        'integration.request.path.proxy': 'method.request.path.proxy'
    MethodResponses:
      - StatusCode: 200
    RequestParameters:
        'method.request.path.proxy': true
    ResourceId:
      Ref: MainApiProxyPath
    RestApiId:
      Ref: ApiGatewayRestApi

Type

AWS::ApiGateway::Method

受け取ったリクエストをよしなにする所

今回はプロキシなので処理の部分は更に他人に投げる

Properties

あとでかく

参考

SERVERLESSフレームワークを使ってHTTPプロキシを作る【後編】 - Qiita

http proxy integration - doesn't support {proxy+} · Issue #4514 · serverless/serverless

Serverless Framework - AWS Lambda Events - API Gateway

@ricardojonathanromero
Copy link

Hi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment