AWS CloudFormation テンプレート、OpenAPI 定義、AWS コマンドラインインターフェイス (AWS CLI) コマンドを使用して、プロキシリソースを含む Amazon API Gateway API を作成したのですが、API に URL パスパラメータを追加しようとすると「Invalid mapping expression specified」というエラーメッセージが表示されます。
簡単な説明
プロキシパスパラメーター {proxy+} に URL パスパラメーターマッピングが定義されていない場合、API Gateway は「Invalid mapping expression specified」というエラーメッセージを返します。
この問題を解決するには、API の作成に使用したツールの手順を実行します。
解決策
CloudFormation テンプレート
AWS::ApiGateway::Methodセクションの RequestParameters を定義します。
次の手順を実行します。
-
RequestParameters の値が true に設定されるように CloudFormation テンプレートを更新します。
テンプレートの例:
....
.
ProxyMethod:
Type: 'AWS::ApiGateway::Method'
Properties:
.
.
RequestParameters:
method.request.path.proxy: true
Integration:
RequestParameters:
integration.request.path.proxy: 'method.request.path.proxy'
IntegrationHttpMethod: ANY
.
.
...
-
API を更新するには、CloudFormation テンプレートを使用して CloudFormation スタックを更新します。
**注:**API Gateway リソースの更新方法の詳細については、「Amazon API Gateway resource type reference」を参照してください。
OpenAPI 定義
x-amazon-apigateway-any-method オブジェクトの**[パラメーター]** セクションを定義します。
次の手順を実行します。
-
x-amazon-apigateway-any-method セクションのパラメーターが次の値になるように API 定義を更新してください。
"x-amazon-apigateway-any-method": { "parameters": [
{
"name": "proxy",
"in": "path",
"required": true,
"type": "string"
}
]
....
....
}
-
更新した API 定義ファイルを API Gateway にインポート して、API を更新します。
**注:**詳細については、Swagger のウェブサイトの「Describing parameters 」を参照してください。
AWS CLI コマンド
**注:**AWS CLI のコマンドの実行時にエラーが発生する場合は、「AWS CLI エラーのトラブルシューティング」を参照してください。また、AWS CLI の最新バージョンを使用していることを確認してください。
put-integration コマンドを実行してインテグレーションをセットアップするときに、--request-parameters パラメーターを追加します。
-
update-method コマンドを実行して、HTTP メソッドのメソッドリクエスト 設定を更新します。
aws apigateway update-method \
--rest-api-id <your-api-id> \
--resource-id <your-resource-id> \
--http-method <your-http-method> \
--patch-operations op="add",path="/requestParameters/method.request.path.proxy",value="true"
-
put-integration コマンドを実行して、--request-parameters を持つ HTTP メソッドの統合リクエスト 設定を更新します。この例のパラメータは proxy という名前で、値は method.request.path.proxy です。put-integration コマンドは、VPC リンクとの HTTP_PROXY インテグレーションをセットアップします。
aws apigateway put-integration \
--rest-api-id <your-api-id>\
--resource-id <your-resource-id> \
--http-method <your-http-method> \
--type HTTP_PROXY \
--integration-http-method <your-integration-http-method> \
--uri "<your-integration-endpoint-uri>" \
--connection-type VPC_LINK \
--connection-id <your-vpclink-connection-id> \
--request-parameters "integration.request.path.proxy=method.request.path.proxy"
**注:**例の値は、すべてご自身の値に置き換えてください。
セットアップをテストする
次の手順を実行します。
- API Gateway コンソールを開き、API の名前を選択します。
- URL パスパラメータ を展開します。
**注:**プロキシパスパラメータに正しく定義された URL パスパラメータマッピングが含まれている場合、エラーは表示されません。
関連情報
x-amazon-apigateway-integration.requestParameters オブジェクト
プロキシリソースとのプロキシ統合を設定する
API Gateway での REST API のデータ変換