2017-06-21 4 views
0

私はAPIゲートウェイを使用しており、データをステップ関数に渡すサービスを持っています。ネストされたJSON構造のボディマッピングテンプレートにすべてのステージ変数を追加することはできますか?

関数は次の形式でJSON入力を必要とするステップ:

{ 
    "input":"", 
    "stateMachineArn": "arn:aws:states:eu-west-1:xxxxxxxxxxxx:stateMachine:StateMachine-1" 
} 

私は現在、私はあなたが使用できるデフォルトのマッピングテンプレートに知っすなわち

#set($data = $util.escapeJavaScript($input.json('$'))) 
{ 
    "input": "{ 
     \"input\": $data, 
     \"stageVariables\" : { 
      \"clientId\": \"${stageVariables.clientId}\", 
      \"clientSecret\": \"${stageVariables.clientSecret}\", 
      \"password\": \"${stageVariables.password}\" }, 
    "stateMachineArn": "arn:aws:states:eu-west-1:xxxxxxxxxxxx:stateMachine:StateMachine-1" 
} 

マニュアルのように、ステージ変数を渡していますこのようなものがステージ変数をループしてJSONを生成します:

"stage-variables" : { 
#foreach($key in $stageVariables.keySet()) 
    "$key" : "$util.escapeJavaScript($stageVariables.get($key))" 
    #if($foreach.hasNext),#end 
#end 
} 

しかし、ステップ関数で必要とされるJSONは若干異なります。手動で明示的に追加することなく、JSON形式にすべてのステージ変数を取得するにはどうすればよいですか?

おかげ

答えて

0

私は、これは動作するはずだと思うが、私はそれをテストしていません。私は単にあなたがそこにあったforeachブロックをコピーし、引用符をインラインでエスケープしました。

#set($data = $util.escapeJavaScript($input.json('$'))) 
{ 
    "input": "{ 
     \"input\": $data, 
     \"stageVariables\" : { 
      #foreach($key in $stageVariables.keySet()) 
      \"$key\" : \"$util.escapeJavaScript($stageVariables.get($key))\" 
      #if($foreach.hasNext),#end 
      #end 
     }, 
    "stateMachineArn": "arn:aws:states:eu-west-1:xxxxxxxxxxxx:stateMachine:StateMachine-1" 
} 
+0

@JackKohnにお返事ありがとうございます。私が投稿した質問に対する回答をご覧ください。敬具。 – cyorkston

0

以下の解決策があります。テンプレートは改行に非常に敏感ですが、以下のコードは合計5行で構成されています。

#set($data = $util.escapeJavaScript($input.json('$'))) 
{ 
    "input": "{ \"input\": $data, \"stageVariables\" : { #foreach($key in $stageVariables.keySet()) \"$key\" : \"$util.escapeJavaScript($stageVariables.get($key))\" #if($foreach.hasNext),#end #end } }", 
    "stateMachineArn": "arn:aws:states:eu-west-1:xxxxxxxxxxxx:stateMachine:StateMachine-1" 
}