CloudFormationでAPI Gatewayのアクセスログで改行コードをいれて、出力したログを改行する


はじめに

API GatewayのアクセスログをCloudFormationで改行しようと思ったのですが、2日ほどかかりました。。。
分かればあっという間でしたが、ハマっているときは全然わからないのはあるあるですね。
誰かの参考になればと思い、記事を書きます。

前提条件

・テンプレートファイルはyaml形式
・API GatewayはREST API
・API GatewayのアクセスログはKinesis Data Firehose経由でS3に出力

改行コードとして認識されたテンプレートファイル

Formatの部分がアクセスログの出力形式の設定箇所になります。
改行コードとして認識させるために\nをダブルクォーテーションで囲み、末尾に追加するためにJoinを用いています。

Resources:
  Prod:
    Type: AWS::ApiGateway::Stage
    Properties:
      StageName: Prod
      ︙
      AccessLogSetting:
        DestinationArn: "arn:aws:kinesis:region:account-id:stream/stream-name"
        Format: !Join [ "\n", [ '{ "requestId":"$context.requestId", "ip": "$context.identity.sourceIp", "caller":"$context.identity.caller", "user":"$context.identity.user","requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod","resourcePath":"$context.resourcePath", "status":"$context.status","protocol":"$context.protocol", "responseLength":"$context.responseLength" }', '' ] ]
             ︙

このFormatでログを出力した場合、下記のように改行されます。

{"requestId": …}
{"requestId": …}
{"requestId": …}

上手くいかなかったテンプレートファイル

下記のようにシングルクォーテーションの中にただ\nをいれただけでは、\nの文字列としか認識されませんでした。

Resources:
  Prod:
    Type: AWS::ApiGateway::Stage
    Properties:
      StageName: Prod
      ︙
      AccessLogSetting:
        DestinationArn: "arn:aws:kinesis:region:account-id:stream/stream-name"
        Format: '{ "requestId":"$context.requestId", "ip": "$context.identity.sourceIp", "caller":"$context.identity.caller", "user":"$context.identity.user","requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod","resourcePath":"$context.resourcePath", "status":"$context.status","protocol":"$context.protocol", "responseLength":"$context.responseLength" }\n'
             ︙

このFormatでログを出力した場合、下記のように改行されます。

{"requestId": …}\n{"requestId": …}\n{"requestId": …}

まt、下記のようにシングルクォーテーションの中にダブルクォーテーションで囲んだ"\n"をいれた場合は"\n"の文字列と認識されました。

Resources:
  Prod:
    Type: AWS::ApiGateway::Stage
    Properties:
      StageName: Prod
      ︙
      AccessLogSetting:
        DestinationArn: "arn:aws:kinesis:region:account-id:stream/stream-name"
        Format: '{ "requestId":"$context.requestId", "ip": "$context.identity.sourceIp", "caller":"$context.identity.caller", "user":"$context.identity.user","requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod","resourcePath":"$context.resourcePath", "status":"$context.status","protocol":"$context.protocol", "responseLength":"$context.responseLength" }"\n"'
             ︙

このFormatでログを出力した場合、下記のように改行されます。

{"requestId": …}"\n"{"requestId": …}"\n"{"requestId": …}

なぜうまく行ったのか?

YAMLの仕様上シングルクォーテーションを使用した場合、エスケープコードの解析は行われません。
すなわち \n はそのまま文字列 \n として認識され、"\n"は"\n"として認識されます。

{"requestId": …}\n{"requestId": …}\n{"requestId": …}
{"requestId": …}"\n"{"requestId": …}"\n"{"requestId": …}

一方で、ダブルクォーテーションを使用した場合には解析が行われ、\n は改行コードとして認識されます。

{"requestId": …}
{"requestId": …}
{"requestId": …}

最後に

この問題を対処するとき、はじめはコンソールから\nを設定したら改行されたのに、CloudFormationでは\nで文字列として出力されたので、CloudFormationのバグかFormatのいれ方に何か問題があると思いました。
しかし、YAMLの仕様だったので問題の切り分けのやり方がまだまだだったなと学びました。

参考資料

https://yaml.org/spec/1.2.2/#73-flow-scalar-styles
https://docs.ansible.com/ansible/2.9_ja/reference_appendices/YAMLSyntax.html
https://nopipi.hatenablog.com/entry/2018/11/12/122511