【发布时间】:2019-11-03 03:25:58
【问题描述】:
为了保护我们的 API,我正在尝试使用 RateBasedRule 部署 WAFRegional。 API 网关位于 SAM 模板中,其中我还有一个嵌套堆栈,用于保存 WAFRegional 配置的子模板。下面提供了 WAFRegional 配置的子模板。在 ExecuteChangeSet 阶段发生的情况如下:
CamerasIpSet 已创建
CamerasRateRule 已创建
WAFCamerasWebACL CREATE_FAILED:引用的项目不存在。 (服务:AWSWAFRegional;状态码:400;错误码:WAFNonexistentItemException
我在大约 2 个月前发现了以下帖子,其中有人在使用无服务器时遇到了同样的问题:https://forum.serverless.com/t/dependon-api-gateway-deployment/7792
我在这里错过了什么?
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Template for WAF Configuration'
Parameters:
CamerasApi:
Description: "Arn of the Cameras Api"
Type: String
Default: cameras-api-dev
StageName:
Description: "Stage name of the Cameras Api"
Type: String
Default: v
Blocking:
Description: "Number of calls per 5 minutes for WAF IP blocking."
Type: Number
Default: 2000
EnvironmentType:
Type: String
Default: "dev"
Description: "Type of environment: dev, staging or prod."
Resources:
WAFCamerasWebACL:
Type: AWS::WAFRegional::WebACL
DependsOn: CamerasRateRule
Properties:
DefaultAction:
Type: ALLOW
MetricName: !Join ['', ['IPBlockingMetric', !Ref EnvironmentType]]
Name: !Join ['', ['IPBlockingACL', !Ref EnvironmentType]]
Rules:
-
Action:
Type: "BLOCK"
Priority: 1
RuleId: !Ref CamerasRateRule
CamerasRateRule:
Type: AWS::WAFRegional::RateBasedRule
Properties:
MetricName: UnallowedAccessCount
Name: FiveMinuteRule
RateKey: IP
RateLimit: !Ref Blocking
MatchPredicates:
-
DataId: !Ref CamerasIpSet
Negated: false
Type: "IPMatch"
CamerasIpSet:
Type: AWS::WAFRegional::IPSet
Properties:
Name: !Join ['-', ['IpBlacklist', !Ref EnvironmentType]]
MyWebACLAssociation:
Type: AWS::WAFRegional::WebACLAssociation
Properties:
ResourceArn: !Sub arn:aws:apigateway:${AWS::Region}::/restapis/${CamerasApi}/stages/${StageName}
WebACLId: !Ref WAFCamerasWebACL
Outputs:
WebACL:
Description: Name of the web ACL
Value: !Ref WAFCamerasWebACL
【问题讨论】:
-
不相关,但
WAFCamerasWebACL中的两个连接可以使用 Sub:MetricName: !Sub IPBlockingMetric${EnvironmentType}更简单清晰地编写 -
当 Cloudformation 实现 WAFv2(他们建议您现在使用)时,问题将得到解决。最终,我们需要 Cloudformation 来支持创建和关联(即到 API 网关或负载均衡器),这样其他变通方法就不会到位,因为它们不是很容易转移。 GitHub 票是:github.com/aws-cloudformation/…
标签: amazon-cloudformation aws-sam amazon-waf