Search code examples
amazon-web-servicesaws-api-gatewayamazon-cloudwatchaws-cdkapi-gateway

AWS CDK enable API Gateway's raw request body for cloudwatch


I have an AWS API Gateway and this is my CDK code:

const api = new aws_apigateway.RestApi(this, 'kartal', {
    restApiName: "kartal",
    deployOptions: {
        stageName: process.env.ENVIRONMENT,
        loggingLevel: aws_apigateway.MethodLoggingLevel.INFO,
        metricsEnabled: true,
        accessLogFormat: aws_apigateway.AccessLogFormat.custom(JSON.stringify({
            requestId: aws_apigateway.AccessLogField.contextRequestId(),
            ip: aws_apigateway.AccessLogField.contextIdentitySourceIp(),
            requestTime: aws_apigateway.AccessLogField.contextRequestTime(),
            resourcePath: aws_apigateway.AccessLogField.contextResourcePath(),
            method: aws_apigateway.AccessLogField.contextHttpMethod(),
            error: aws_apigateway.AccessLogField.contextErrorMessage(),
            validationError: aws_apigateway.AccessLogField.contextErrorValidationErrorString(),
            status: aws_apigateway.AccessLogField.contextStatus()
         })),
        accessLogDestination: new aws_apigateway.LogGroupLogDestination(apigwLogGroup)
    },
})
  1. Is that possible to log API Gateway's raw request into Cloudwatch using CDK?
  2. Is that possible to activate Full Request and Response Logs flag using CDK? I couldn't find any reference into the CDK documentation. here
  3. If both negative, how can I log API Gateway's raw requests?

api gateway cloudwatch


Solution

  • There is an option to activate Full Request and Response Logs in the CDK, dataTraceEnabled flag in the deployOptions.

    const api = new apigateway.RestApi(this, 'books', {
      cloudWatchRole: true,
      deployOptions: {
        loggingLevel: apigateway.MethodLoggingLevel.INFO,
        dataTraceEnabled: true
      }
    })