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: ApiGatewayRestApiAPI Gatewayにリソースを作成する
ParentId:
Fn::GetAtt:
- RootResourceId
- ApiGatewayRestApi # REST APIのIDParentId:
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