如何通过Cloudformation模板为AWS API GW启用Cloudwatch日志记录

时间:2020-05-29 15:22:19

标签: amazon-web-services amazon-cloudformation aws-api-gateway api-gateway amazon-api-gateway

我正在尝试通过cloudformation模板启用AWS API Gateway的cloudwatch日志,但未启用。我尝试在阶段描述和方法设置中将日志记录级别设置为INFO。对我想念的东西有任何想法吗?

当我通过UI手动启用日志记录时,它可以工作。当我尝试通过如下所示的cloudformation模板启用时不起作用-

注意:我只是在使用普通的cloudformation模板,并且已经在我的帐户中添加了具有API网关权限的角色ARN来登录cloudwatch

TestDeployment:
  Type: AWS::ApiGateway::Deployment
  Properties:
    Description: API Deployment
    RestApiId: testApi
    StageName: 'dev'
    StageDescription:
      Description: Stage - DEV
      LoggingLevel: INFO
      MethodSettings:
        - ResourcePath: "/testresource"
          HttpMethod: "POST"
          LoggingLevel: INFO

2 个答案:

答案 0 :(得分:0)

您是否已经按照API文档https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html

中的说明配置了API Gateway RestApi以便将日志写入CloudWatch。

请注意,您必须设置CloudWatch日志记录的权限。

答案 1 :(得分:0)

请在StageDescription中将MetricsEnabled属性添加到已在阶段级别启用的CloudWatch日志中。如果要在方法级别启用CloudWatch日志,请在MethodSettigns中添加MetricsEnabled属性。在以下示例中,我在两个地方都启用了日志。

TestDeployment:
  Type: AWS::ApiGateway::Deployment
  Properties:
    Description: API Deployment
    RestApiId: testApi
    StageName: 'dev'
    StageDescription:
      Description: Stage - DEV
      LoggingLevel: INFO
      MetricsEnabled: True
      MethodSettings:
        - ResourcePath: "/testresource"
          HttpMethod: "POST"
          LoggingLevel: INFO
          MetricsEnabled: True