首页 > 解决方案 > 使用 CloudFormation 从 WAFv2 创建 WebACL

问题描述

我正在尝试使用 cloudformation 创建一个 WebACL,以保护应用程序 API 免受滥用,其想法是在 5 分钟内将 API 访问限制为最多 100 个 IP 请求。

为此,我必须使用 WAFv2,因为第一个版本似乎只支持:

WAFv2 文档: https ://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-wafv2-webacl.html

我写了这个作为例子:

AWSTemplateFormatVersion: 2010-09-09
Resources:
  WebACL:
    Type: 'AWS::WAFv2::WebACL'
    Properties:
      Name: WebAclLimit100
      Scope: "REGIONAL"
      DefaultAction:
        Type: ALLOW
      VisibilityConfig:
        SampledRequestsEnabled: true
        CloudWatchMetricsEnabled: true
        MetricName: WebAcLimit100

但是,当我尝试将其上传到 CloudFormation 时,创建失败并显示以下消息:

模型验证失败(#: extraneous key [Type] is not allowed)

我认为问题出在以下几行:

      DefaultAction:
        Type: ALLOW

但是我不知道如何分配 DefaultAction 而不会在 CloudFormation 上失败,我尝试了很多次(当然不同)并且找不到正确的方法。互联网上没有 WAFv2 的示例,第一版 WAF 的语法似乎不兼容 :(

标签: amazon-web-servicesamazon-cloudformationamazon-waf

解决方案


您需要更改“DefaultAction”,因为这需要 JSON 值:请按照此处的示例部分操作 WAFv2 模板

AWSTemplateFormatVersion: 2010-09-09
 Resources:
   WebACL:
    Type: 'AWS::WAFv2::WebACL'
    Properties:
     Name: WebAclLimit100
     Scope: "REGIONAL"
     DefaultAction:
      Allow: {}
     VisibilityConfig:
      SampledRequestsEnabled: true
      CloudWatchMetricsEnabled: true
      MetricName: WebAcLimit100

推荐阅读