AWS API Gateway的执行日志默认是存储在CloudWatch Logs中的,可以通过创建API Gateway时在Stage中配置日志设置来改变日志路径。代码示例如下:
Aws::ApiGateway::RestApi:
Properties:
Name: my-api
Description: My API Gateway
EndpointConfiguration:
Types:
- REGIONAL
Tags:
Team: myteam
Metadata:
'AWS::CloudFormation::Designer':
id: 12345
DeletionPolicy: Retain
# 创建一个API Gateway Stage,并在其中配置日志
Aws::ApiGateway::Deployment:
Type: AWS::ApiGateway::Deployment
Properties:
RestApiId: !Ref MyApi
StageName: production
Description: Production deployment
Variables:
environment: production
AccessLogSetting:
DestinationArn: !Ref AccessLogGroup
Format: '{"requestId":"$context.requestId","ip":"$context.identity.sourceIp","caller":"$context.identity.caller","user":"$context.identity.user","requestTime":"$context.requestTime","httpMethod":"$context.httpMethod","resourcePath":"$context.resourcePath","status":"$context.status","protocol":"$context.protocol","responseLength":"$context.responseLength"}'
AccessLogGroup:
Type: 'AWS::Logs::LogGroup'
Properties:
LogGroupName: /aws/api-gateway/my-api-logs
RetentionInDays: 7
该代码会创建一个API Gateway和一个API Gateway Stage,并将日志存储到指定路径的日志组中。可以根据需要更改日志格式、日志组名称等设置。