Introduction

AWS API Gateway has facility to enable logging to send logs to Cloudwatch. This document describes the steps to do so.

Steps

  1. Create a role with the policy AmazonAPIGatewayPushToCloudwatchLogs or attach this policy to an existing role.

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d5ba3bfc-2272-4d24-8ec9-66f18febc047/Screenshot_2020-12-29_at_7.01.04_PM.png

  2. Create a Cloudwatch Log group /aws/apigw/$your-api-gw-name where $your_api_gw_name is replaced by your AWS API Gateway name. Select retention setting to be at least 5 days or set it to Never if you want to retain the logs in Cloudwatch. Click Create.

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/c48e7608-25ca-4a0a-98d6-1a25926e0bec/2021-05-28_21-56.png

  3. Go to AWS API Gateway → Settings → CloudWatch log role ARN and add the above role ARN

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/85f3d726-9c65-40d2-a71b-763e8ade4502/Screenshot_2020-12-29_at_6.58.17_PM.png

  4. Got to AWS API Gateway → Select API GW → Stages → Select Stage → Click on Logs/Tracing. Ensure that you turn off the default verbose API Gateway logging. Click on "Save Changes"

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a0baaa61-0111-4d58-8e52-2583796344a0/2021-05-28_22-26.png

  5. Got to AWS API Gateway → Select API GW → Stages → Select Stage → Click on Logs/Tracing and update the following fields. Click on "Save Changes"

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e5d3a618-c78c-48c4-a89c-7bf61938f989/2021-05-28_21-57.png

    1. Enable Access Logging - turn on.
    2. Access Log destination ARN - ARN of cloudwatch log group from Step-2.
    3. Log Format
  6. Enable logging by going to - AWS API Gateway → Stages → Select stage → Logs and tracing → "Enable CloudWatch Logs" and "Log full requests/responses data" with "Log level" = INFO

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/21b328bd-7f06-435f-860f-5043a7ea3eee/Screenshot_2020-12-29_at_6.58.40_PM.png

  7. Send requests to API GW and view them on Cloudwatch logs. Go to Cloudwatch → Log Groups → Search for /aws/apigw/$your-api-gw-name - click on corresponding log group. You should see logs like the following:

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a24c083e-320c-4322-85c4-733b9b63464a/2021-05-28_22-18.png