我尝试将我的 ApiGateway RestApi 连接到我的 WAF WebACL 一段时间了。
我正在使用无服务器框架,因此我有 Cloudformation 模板 serverless.yml。
这是我的设置:
MyWafWebACL:
Type: 'AWS::WAFv2::WebACL'
Properties:
Name: 'WhitelistedIPsWebACL'
Scope: 'REGIONAL'
DefaultAction:
Allow: {}
Rules:
- Name: 'AllowWhitelistedIPsIPv4'
Priority: 0
Action:
Allow: {}
Statement:
IPSetReferenceStatement:
ARN: !GetAtt MyWafIPSetIPv4.Arn
VisibilityConfig:
SampledRequestsEnabled: true
CloudWatchMetricsEnabled: true
MetricName: 'AllowWhitelistedIPsIPv4Metric'
- Name: 'AllowWhitelistedIPsIPv6'
Priority: 1
Action:
Allow: {}
Statement:
IPSetReferenceStatement:
ARN: !GetAtt MyWafIPSetIPv6.Arn
VisibilityConfig:
SampledRequestsEnabled: true
CloudWatchMetricsEnabled: true
MetricName: 'AllowWhitelistedIPsIPv6Metric'
VisibilityConfig:
SampledRequestsEnabled: true
CloudWatchMetricsEnabled: true
MetricName: 'WhitelistedIPsMetric'
ApiGatewayRestApi:
Type: 'AWS::ApiGateway::RestApi'
Properties:
Name: 'ApiGatewayRestApi'
Description: 'Standard REST gateway'
MyWafWebACLAssociation:
Type: 'AWS::WAFv2::WebACLAssociation'
Properties:
WebAclArn: !Ref MyWafWebACL
ResourceArn: !Sub "arn:aws:apigateway:${AWS::Region}::/restapis/${ApiGatewayRestApi}/stages/dev"
注释掉 WebACLAssociation 时,一切都会正常部署,并且资源会正确创建。我也可以在 Web 界面中毫无问题地进行关联,但我想部署到不同的阶段,并且在应该正常工作时必须手动执行此操作很容易出错,我想不惜一切代价避免它。
我尝试过以下方法:
有人知道为什么会发生这种情况吗?
提前致谢。
通过转发搜索找到了答案。
这里提到问题不是apigateway的ResourceArn而是WebAclArn。抛出的错误消息是错误的。
WebAclArn 不能像!Ref MyWafWebACL
那样被引用,因为这似乎是一个具有多个值的对象。正确的引用是
!GetAtt MyWafWebACL.Arn
,它直接指向 arn。这解决了我的问题。