API Gateway LogGroup duplicate and details are missing

Viewed 140

I'm struggling to set up logs in CloudWatch for my RestApi via cdk.

This is my code:

const logGroup = new LogGroup(this, `apiLogs`, {
        logGroupName: `apiLogs`,
        retention: RetentionDays.ONE_WEEK
});

const api = new apigw.RestApi(this, `apiName`, {
        // set up CORS            
        defaultCorsPreflightOptions: {
            statusCode: 200,
            allowMethods: ['OPTIONS', 'GET', 'POST', 'DELETE'],
            allowOrigins: apigw.Cors.ALL_ORIGINS
        },
        deploy: true,
        restApiName: `apiName`,
        deployOptions: {
            accessLogDestination: new apigw.LogGroupLogDestination(logGroup),
            loggingLevel: apigw.MethodLoggingLevel.INFO,
            dataTraceEnabled: true
        }}
);

In the console, the settings of the logs that I want are called "Log full request/responses data". which should be the part

deployOptions: {
            accessLogDestination: new apigw.LogGroupLogDestination(logGroup),
            loggingLevel: apigw.MethodLoggingLevel.INFO,
            dataTraceEnabled: true
        }

This setting works as I want as shown in the screenshot: CloudWatch Settings after deployment

However if I now check CloudWatch after sending requests to the API, two logGroups are created and have logs:

  1. The first is the LogGroup with the name apiLogs as defined in my code but the output of the logs are not as detailed as I want them as shown here: Logs of apiLogs

  2. The second is a LogGroup with a default name including the id of the endpoint and the logs include the detailed information that I want as shown here: Logs of default

Why are there 2 separate logGroups created and why is apiLogs not showing the detailed logs?

Has anyone faced the same problem?

1 Answers

API Gateway’s produce 2 types of logs:

execution logs

access logs

For execution logs - no way to control this, it’s an AWS managed log group (similar to Lambda). You can only configure access log behaviour.

Related