LoginSignup
1
1

More than 5 years have passed since last update.

s-templates.jsonでJSONフォーマットを超えるVTLを書きたいときは代わりにs-templates.yamlを書けば良い

Last updated at Posted at 2016-06-01

s-templates.jsonに

{
  "apiGatewayRequestTemplate": {
    "application/json": {
      {
        "accountId": "$context.identity.accountId",
        "apiId": "$context.apiId",
        "apiKey": "$context.identity.apiKey",
        "caller": "$context.identity.caller",
        "headers": {
      #foreach( $key in $input.params().header.keySet() )
          "$key": "$input.params().header.get($key)"#if( $foreach.hasNext ),#end
      #end
        },
        "httpMethod": "$context.httpMethod",
        "path": "$context.resourcePath",
        "pathParameters": {
      #foreach( $key in $input.params().path.keySet() )
          "$key": "$input.params().path.get($key)"#if( $foreach.hasNext ),#end
      #end
        },
        "queryParameters": {
      #foreach( $key in $input.params().querystring.keySet() )
          "$key": "$input.params().querystring.get($key)"#if( $foreach.hasNext ),#end
      #end
        },
        "requestId": "$context.requestId",
        "requestParameters": $input.json('$'),
        "resourceId": "$context.resourceId",
        "sourceIp": "$context.identity.sourceIp",
        "stage": "$context.stage",
        "user": "$context.identity.user",
        "userAgent": "$context.identity.userAgent",
        "userArn": "$context.identity.userArn"
      }
    }
  }
}

みたいなVTLを書きたくなるかと思いますが、これはJSONのフォーマットに準拠していないのでServerlessではパースエラーです。

JSONを全部エスケープして1行の文字列に変換してしまえばいいらしいですが、ヒトがさわれるものではなくなってしまいます。

じゃあ、どうするか。

「YAMLで中に複数行で書けばいいんじゃね?」というコメントを見つけました。

apiGatewayRequestTemplate:
  application/json: |
    {
      "accountId": "$context.identity.accountId",
      "apiId": "$context.apiId",
      "apiKey": "$context.identity.apiKey",
      "caller": "$context.identity.caller",
      "headers": {
        #foreach( $key in $input.params().header.keySet() )
          "$key": "$input.params().header.get($key)"#if( $foreach.hasNext ),#end
        #end
      },
      "httpMethod": "$context.httpMethod",
      "path": "$context.resourcePath",
      "pathParameters": {
        #foreach( $key in $input.params().path.keySet() )
          "$key": "$input.params().path.get($key)"#if( $foreach.hasNext ),#end
        #end
      },
      "queryParameters": {
        #foreach( $key in $input.params().querystring.keySet() )
          "$key": "$input.params().querystring.get($key)"#if( $foreach.hasNext ),#end
        #end
      },
      "requestId": "$context.requestId",
      "requestParameters": $input.json('$'),
      "resourceId": "$context.resourceId",
      "sourceIp": "$context.identity.sourceIp",
      "stage": "$context.stage",
      "user": "$context.identity.user",
      "userAgent": "$context.identity.userAgent",
      "userArn": "$context.identity.userArn"
    }

参考URL

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1