Amazon API Gateway can send logs to Amazon CloudWatch Logs and Amazon Kinesis Data Firehose for centralization.
AWS SAM templates
For AWS::Serverless::Api and AWS::Serverless::HttpApi resources, this rule checks the effective stage settings directly, including values inherited from Globals.Api or Globals.HttpApi. Function Api events without RestApiId and HttpApi events without ApiId create implicit APIs, which are also checked using the applicable global settings; each implicit API finding is reported once per owning function, so functions sharing an implicit API can each receive a finding. REST APIs use AccessLogSetting and HTTP APIs use AccessLogSettings; a missing DestinationArn fails the check.
import{LogGroup}from'@aws-cdk/aws-logs';import{LogGroupLogDestination,RestApi}from'@aws-cdk/aws-apigateway';exportclassMyStackextendscdk.Stack{constructor(scope:cdk.Construct,id:string,props?:cdk.StackProps){super(scope,id,props);constmyLogGroup=newLogGroup(scope,'MyLogGroup');constmyApi=newRestApi(scope,'MyApi',{deployOptions:{// Setup logging for API GatewayaccessLogDestination:newLogGroupLogDestination(myLogGroup),accessLogFormat:JSON.stringify({"stage":"$context.stage","request_id":"$context.requestId","api_id":"$context.apiId","resource_path":"$context.resourcePath","resource_id":"$context.resourceId","http_method":"$context.httpMethod","source_ip":"$context.identity.sourceIp","user-agent":"$context.identity.userAgent","account_id":"$context.identity.accountId","api_key":"$context.identity.apiKey","caller":"$context.identity.caller","user":"$context.identity.user","user_arn":"$context.identity.userArn","integration_latency":$context.integration.latency}),}});}}
import{CfnStage,HttpApi}from'@aws-cdk/aws-apigatewayv2';exportclassMyStackextendscdk.Stack{constructor(scope:cdk.Construct,id:string,props?:cdk.StackProps){super(scope,id,props);constmyApi=newHttpApi(scope,'MyApi');// Setup logging for API Gateway using escape hatch.// See https://github.com/aws/aws-cdk/issues/11100 and// https://docs.aws.amazon.com/cdk/latest/guide/cfn_layer.html#cfn_layer_resource// for more information.constdefaultStage=myApi.defaultStage.node.defaultChildasCfnStage;defaultStage.accessLogSettings={destinationArn:'',format:JSON.stringify({"stage":"$context.stage","request_id":"$context.requestId","api_id":"$context.apiId","resource_path":"$context.resourcePath","resource_id":"$context.resourceId","http_method":"$context.httpMethod","source_ip":"$context.identity.sourceIp","user-agent":"$context.identity.userAgent","account_id":"$context.identity.accountId","api_key":"$context.identity.apiKey","caller":"$context.identity.caller","user":"$context.identity.user","user_arn":"$context.identity.userArn","integration_latency":$context.integration.latency}),};}}