LoginSignup
1
0

More than 1 year has passed since last update.

Amazon API Gatewayの実行ログをAWS CloudFormationで設定する

Posted at

AWS CloudFormation で Amazon API Gateway を構築する時、実行ログの設定がわかりづらかったので記録します。

Amazon API Gateway は、Amazon Management Console ではステージエディターの「ログ / トレース」タブで実行ログを設定できます。実行ログは、画面上では「CloudWatch ログ」と表示されています。画面のたどり方は、「API > ステージ > ログ / トレース > CloudWatch 設定 > CloudWatch ログ」です。

この CloudWatch ログの設定では、次の値が選択できます。

  • オフ
  • エラーのみ
  • エラーと情報ログ
  • リクエストとレスポンスの全ログ

選択できる各値の AWS CloudFormation での指定の仕方を列記します。

オフ:

template.yaml
Resources:
  SampleApi:
    Type: AWS::Serverless::Api
    Properties: 
     MethodSettings:
        - LoggingLevel: OFF
          ResourcePath: "/*"
          HttpMethod: "*"
          DataTraceEnabled: False

エラーのみ:

template.yaml
Resources:
  SampleApi:
    Type: AWS::Serverless::Api
    Properties: 
     MethodSettings:
        - LoggingLevel: ERROR
          ResourcePath: "/*"
          HttpMethod: "*"
          DataTraceEnabled: False

エラーと情報ログのみ:

template.yaml
Resources:
  SampleApi:
    Type: AWS::Serverless::Api
    Properties: 
     MethodSettings:
        - LoggingLevel: INFO
          ResourcePath: "/*"
          HttpMethod: "*"
          DataTraceEnabled: False

リクエストとレスポンスの全ログ:

template.yaml
Resources:
  SampleApi:
    Type: AWS::Serverless::Api
    Properties: 
     MethodSettings:
        - LoggingLevel: INFO
          ResourcePath: "/*"
          HttpMethod: "*"
          DataTraceEnabled: True

わかりづらかったのが、この「リクエストとレスポンスの全ログ」をオフにする設定です。「リクエストとレスポンスの全ログ」は、DataTraceEnabledTrue に設定すると LoggingLevel の値が ErrorInfo かに関わらずに選択されます。又、DataTraceEnabled を省略した場合のディフォルト値は True です。本番環境での設定が推奨されない値にも関わらず、ディフォルト値が True ですので、実行ログを有効にする場合は明示的に指定するようにしてください。

参考までに公式ドキュメントで実行ログに言及している部分は、次の通りです。

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