首页 > 解决方案 > 为无服务器 websocket 应用禁用 Cloudwatch 日志

问题描述

我有一个使用无服务器框架在 AWS 上托管的 WebSocket API。

问题是我每分钟看到 15 万条日志,而且成本很高。

以下日志组是问题所在:

我已经按照他们的文档禁用了这样的日志,但它显示了一个错误。

service: BotBindSocketAPI

custom:
  customDomain:
    - domainName: ${ssm:/botapi/${opt:stage}/wsName}
      stage: ${opt:stage}
      websocket: true

provider:
  name: aws
  runtime: nodejs10.x
  stage: ${opt:stage}
  region: us-east-1
  logRetentionInDays: 7
  logs:
    restApi:
      accessLogging: false
      executionLogging: false
      level: ERROR
      fullExecutionData: true
    websocket: false
    frameworkLambda: false
  iamRoleStatements:
    - Effect: Allow
      Action:
        - "execute-api:ManageConnections"
      Resource:
        - "arn:aws:execute-api:*:*:**/@connections/*"
    - Effect: Allow
      Action:
        - "s3:GetObject"
        - "s3:PutObject"
      Resource:
        - "arn:aws:s3:::botbind-addons/*"
  vpc:
    truncated

functions:
  websocketHandler:
    handler: handler.websocketHandler
    events:
      - websocket:
          route: $connect
          authorizer: authHandler
      - websocket:
          route: $disconnect
      - websocket:
          route: $default
      - websocket:
          route: ping
      - websocket:
          route: getBot
  authHandler:
    handler: handler.authHandler

plugins:
  - serverless-offline
  - serverless-domain-manager

错误:

Serverless Error ---------------------------------------

An error occurred: WebsocketsDeploymentStage - The following context variables are not supported: [$context.status] (Service: AmazonApiGatewayV2; Status Code: 400; Error Code: BadRequestException; Request ID: 4a522b6c-6ade-4f86-afcf-35017a22c30c).

这是禁用 API 日志的正确方法,因此我可以降低 CloudWatch 成本吗?

标签: amazon-web-serviceswebsocketaws-lambdaserverless

解决方案


对于无服务器框架,您将需要这样的东西:

functions:
  myCloudWatchLog:
    handler: myCloudWatchLog.handler
    events:
      - cloudwatchLog:
        logGroup: '/aws/lambda/hello'
        filter: '{$.userIdentity.type = Root}'

来源https://serverless.com/framework/docs/providers/aws/events/cloudwatch-log/


推荐阅读