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:
Type: AWS::ApiGateway::RestApi
Properties:
Name: test-api-gateway
Description: 'The main entry point of test APIs'
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
リソースやメソッドを内包するガワを作るようなものだと捉えてよさそう。
なのでメソッドやリソースはこれに関連付ける形で作成することになる。
名前と説明だけなので割愛
MainApiBasePath:
Type: AWS::ApiGateway::Resource
Properties:
ParentId:
Fn::GetAtt:
- ApiGatewayRestApi
- RootResourceId
PathPart: 'main-api'
RestApiId:
Ref: ApiGatewayRestApi
API Gatewayにリソースを作成する
ParentId:
Fn::GetAtt:
- RootResourceId
- ApiGatewayRestApi # REST APIのID
ParentId:
Ref: ParentApiPathId # REST APIのID
リソースのパス名
PathPart: 'main-api'
RestApiId: Ref: ApiGatewayRestApi
このリソースを作成するREST APIのID
ここではApiGatewayRestApi
にMainApiBasePath
で定義したmain-api
のリソースを作成したことになる
MainApiProxyPath:
Type: AWS::ApiGateway::Resource
Properties:
ParentId:
Ref: MainApiBasePath
PathPart: '{proxy+}'
RestApiId:
Ref: ApiGatewayRestApi
割愛
MainApiBasePath
の子リソースとして定義
/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
リソースの子リソースであるから、親と子は別々に定義する必要がある
割愛
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
受け取ったリクエストをよしなにする所
今回はプロキシなので処理の部分は更に他人に投げる
あとでかく
SERVERLESSフレームワークを使ってHTTPプロキシを作る【後編】 - Qiita
http proxy integration - doesn't support {proxy+} · Issue #4514 · serverless/serverless
Hi