0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【AWS】CDKで「AccessLogFormat.json_with_standard_fields() got an unexpected keyword」エラーが出た場合

Posted at

概要

ApiGatewayを作成するCDKを実行したら以下のエラーに。

TypeError: AccessLogFormat.json_with_standard_fields() got an unexpected keyword argument 'extended_request_id'

なぜextended_request_idが指定できない...?

原因と解決方法

CDKv2ではextended_request_idという引数はサポートされていないらしい。

そもそもextended_request_idを入れていたのは、以下の公式ドキュメントで書かれている「CloudWatch によるログの形式」の例に含まれていたため。

{ "requestId":"$context.requestId", "extendedRequestId":"$context.extendedRequestId","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" }

公式にあるものをログに出した方がいいかな、と思いそのままCDKで入れたら先述のエラーになった。
その時の指定方法は以下の通り。

        stage = apigateway.Stage(self, "Sample-ApiStage",
            deployment=deployment,
            stage_name="prod",
            logging_level=apigateway.MethodLoggingLevel.INFO,
            data_trace_enabled=True,
            access_log_destination=apigateway.LogGroupLogDestination(api_gateway_log_group),
            access_log_format=apigateway.AccessLogFormat.json_with_standard_fields(
                caller=True,
                extended_request_id=True,
                http_method=True,
                ip=True,
                protocol=True,
                request_time=True,
                resource_path=True,
                response_length=True,
                status=True,
                user=True
            )
        )

しかしこれではダメでした。
正確にいうと、extended_request_id以外は上記の指定方法で問題ないのですが、extended_request_idはサポートされていなかった、のです。

ということで手法を変えて、AccessLogFormat.custom()で明示的に指定してあげるとエラーがなくなります。

stage = apigateway.Stage(self, "Sample-ApiStage",
    deployment=deployment,
    stage_name="prod",
    logging_level=apigateway.MethodLoggingLevel.INFO,
    data_trace_enabled=False,
    access_log_destination=apigateway.LogGroupLogDestination(api_gateway_log_group),
    access_log_format=apigateway.AccessLogFormat.custom(
        '{"requestId":"$context.requestId",'
        '"extendedRequestId":"$context.extendedRequestId",'
        '"ip":"$context.identity.sourceIp",'
        '"user":"$context.identity.user",'
        '"caller":"$context.identity.caller",'
        '"requestTime":"$context.requestTime",'
        '"httpMethod":"$context.httpMethod",'
        '"resourcePath":"$context.resourcePath",'
        '"status":"$context.status",'
        '"protocol":"$context.protocol",'
        '"responseLength":"$context.responseLength",'
        '"responseLatency":"$context.responseLatency",'
        '"integrationLatency":"$context.integrationLatency"}'
    )
)

AWS管理コンソールからも確認できました。

image.png

何を指定した方がいいかについては、
「具体的にどんなログを出したいのか?」
によって固めていく必要がありますね。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?