通过云形成在 API GatewayV2 Stage 中启用 CloudWatch 日志

问题描述

一个 similar question 但它不使用 AWS::ApiGatewayV2::Stage,我需要似乎只有 V2 提供的 AutoDeploy

如何在 AWS API 网关中使用 CloudFormation 启用 CloudWatch 日志并记录完整的消息数据(根据图像)?

Enable cloudwatch logs in AWS Console

我在舞台文档中找不到任何内容 https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-stage.html#cfn-apigatewayv2-stage-deploymentid

我使用的是自动部署阶段。我能够创建日志组、在 CloudWatch 中写入日志的 IAM 角色,但我无法启用日志记录本身。

wsApiGateway:
  Type: AWS::ApiGatewayV2::Api
  Properties:
    Name: foo-ws-gateway
    Description: Api Gateway for Websockets
    ProtocolType: WEBSOCKET
    RouteSelectionExpression: $request.body.action
    disableExecuteApiEndpoint: true # I use custom domain

# routes and integrations ommitted.

wsApiStage:
  Type: AWS::ApiGatewayV2::Stage
  DependsOn:
    - wsConnectRoute
    - wsSendRoute
    - wsdisconnectRoute
  Properties:
    StageName: production
    Description: Autodeploy in production
    AutoDeploy: true
    ApiId: !Ref wsApiGateway
    AccessLogSettings:
      DestinationArn: !GetAtt wsApiGatewayLogGroup.Arn
      Format: '{"requestTime":"$context.requestTime","requestId":"$context.requestId","httpMethod":"$context.httpMethod","path":"$context.path","routeKey":"$context.routeKey","status":$context.status,"responseLatency":$context.responseLatency,"responseLength":$context.responseLength,"integrationError":$context.integration.error}'

我还必须转到 ApiGateway 以前的版本来定义帐户,以便我可以指定对帐户中的 CloudWatch 日志具有写访问权限的 IAM 角色 ARN(设置部分,控制台的 API 网关)。它似乎没有 AWS::ApiGateway2::Account

apiGatewayAccountConfig:
  Type: "AWS::ApiGateway::Account"
  Properties:
    CloudWatchRoleArn: !GetAtt apiGatewayWatchLogsRole.Arn

解决方法

如何在 AWS API 网关中使用 CloudFormation 启用 CloudWatch 日志并记录完整的消息数据(根据图像)?

你不能。 HTTP API(即 ApiGatewayV2)不支持执行日志,如 AWS here 所述:

HTTP API 目前仅支持访问日志,并且这些 API 的日志设置是不同的。有关详细信息,请参阅为 HTTP API 配置日志记录。