SAM API 액세스 로그 미설정

SAM API의 요청 기록을 남길 액세스 로그를 설정하세요.

설명

API Gateway 액세스 로그에는 요청별 ID와 응답 상태를 기록할 수 있습니다.

잠재적 영향

이 기록이 없으면 실패한 요청이나 의심스러운 호출을 추적하기 어렵습니다.

해결 방법

AWS::Serverless::Api에는 AccessLogSetting, AWS::Serverless::HttpApi에는 AccessLogSettings를 지정하세요. 로그 대상과 보존 기간을 정하고 전송 권한을 부여한 뒤 로그 형식에 요청 ID를 포함하세요.

예시

REST API 예시는 별도로 만든 로그 그룹에 $context.requestId와 $context.status를 기록합니다.

변경 전

yaml
Resources:
  ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod
      TracingEnabled: true

변경 후

yaml
Resources:
  ApiGatewayApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod
      TracingEnabled: true
      AccessLogSetting:
        DestinationArn: arn:aws:logs:ap-northeast-2:123456789012:log-group:api-access-log
        Format: '{"requestId":"$context.requestId","status":"$context.status"}'

참조