为 AWS WAF WebAcl 启用日志在 ​​CDK 中不起作用

Mth*_*enn 2 amazon-web-services aws-cloudformation aws-cdk

我的目标是通过 AWS CDK 启用区域 WebAcl 的日志记录。这似乎可以通过 Cloud Formation 实现,并且 CDK 中有适当的构造。但是,当使用以下代码创建日志组并将其链接到LoggingConfiguration时...

  const webAclLogGroup = new LogGroup(scope, "awsWafLogs", {
    logGroupName: `aws-waf-logs`
  });

  // Create logging configuration with log group as destination
  new CfnLoggingConfiguration(scope, "webAclLoggingConfiguration", {
    logDestinationConfigs: webAclLogGroup.logGroupArn, // Arn of LogGroup
    resourceArn: aclArn // Arn of Acl
  });
Run Code Online (Sandbox Code Playgroud)

...我在 期间遇到异常cdk deploy,指出 LogdestinationConfig 中的字符串不是正确的 Arn(日志消息中 Arn 的某些部分已被删除):

Resource handler returned message: "Error reason: The ARN isn't valid. A valid ARN begins with arn: and includes other information separated by colons or slashes., field: LOG_DESTINATION, parameter: arn:aws:logs:xxx:xxx:xxx-awswaflogsF99ED1BA-PAeH9Lt2Y3fi:* (Service: Wafv2, Status Code: 400, Request ID: xxx, Extended Request ID: null)" 
Run Code Online (Sandbox Code Playgroud)

我在生成的 Cloud Formation 代码中看不到错误cdk synth

"webAclLoggingConfiguration": {
  "id": "webAclLoggingConfiguration",
  "path": "xxx/xxx/webAclLoggingConfiguration",
  "attributes": {
    "aws:cdk:cloudformation:type": "AWS::WAFv2::LoggingConfiguration",
    "aws:cdk:cloudformation:props": {
      "logDestinationConfigs": [
        {
          "Fn::GetAtt": [
            {
              "Ref": "awsWafLogs58D3FD01"
            },
            "Arn"
          ]
        }
      ],
      "resourceArn": {
        "Fn::GetAtt": [
          "webACL",
          "Arn"
        ]
      }
    }
  },
  "constructInfo": {
    "fqn": "aws-cdk-lib.aws_wafv2.CfnLoggingConfiguration",
    "version": "2.37.1"
  }
},
Run Code Online (Sandbox Code Playgroud)

我将 Cdk 与 Typescript 一起使用,并且 Cdk 版本当前设置为,2.37.1但它也不适用于2.16.0.

mrg*_*ain 5

WAF 对日志目标配置的命名和格式有特殊要求,如其文档中所述所示。

具体来说,日志组的 ARN 不能结束,:*不幸的是Cloudformation 中日志组 ARN 的返回值是这样的。

解决方法是像这样手动构建所需的 ARN 格式,这将省略后缀:*。另请注意,它logDestinationConfigs采用一个字符串列表,尽管其中仅包含 1 个元素。

  const webAclLogGroup = new LogGroup(scope, "awsWafLogs", {
    logGroupName: `aws-waf-logs`
  });

  // Create logging configuration with log group as destination
  new CfnLoggingConfiguration(scope, "webAclLoggingConfiguration", {
    logDestinationConfigs: [
      // Construct the different ARN format from the logGroupName
      Stack.of(this).formatArn({
        arnFormat: ArnFormat.COLON_RESOURCE_NAME,
        service: "logs",
        resource: "log-group",
        resourceName: webAclLogGroup.logGroupName,
      })
    ],
    resourceArn: aclArn // Arn of Acl
  });
Run Code Online (Sandbox Code Playgroud)

PS:我在 AWS 的 CDK 团队工作。