限制对 Application Load Balancer 的访问的最佳方法是什么?

What is the best way to restrict access to an Application Load Balancer?

理想情况下,我想锁定我的 ALB,以便它只能通过 API 网关访问。

我研究了是否可以将 API 网关与入站规则相关联 - 但是,我发现 API 网关不能与 IP 地址或安全组相关联。我还研究了一个面向内部的 ALB,但我一直无法让它们正常工作,因为 VPC link 仅支持 NLB。

非常感谢任何帮助 - 我一直在查看网关设置,但找不到此选项。

解决这个问题的最佳方法是什么,以便尽可能限制 ALB?

API 网关没有静态 IP,ALB 目前不提供除 Cognito 用户池之外的任何身份验证。因此,我会说您最好的选择是在您建议时将 VPC link 与网络负载均衡器一起使用,并将请求通过 NLB 隧道传输到您的 ALB。

或者,您可以让 VPC 中的 Lambda 调用 ALB,但这会慢很多,但对于小批量来说更便宜,因为您跳过了 NLB。

根据用例,一种可能性是使用客户端 SSL 证书保护您的后端而不是 ALB。 Generate and Configure an SSL Certificate for Backend Authentication

使用 WAF 验证在 API GW

处设置的自定义 HTTP Header 值

在 API GW HTTP 集成方法的集成请求中插入自定义 HTTP header。按照 Amazon API Gateway API request and response data mapping reference.

中的说明使用 静态值

'STATIC_VALUE'. The STATIC_VALUE is a string literal and must be enclosed within a pair of single quotes.

与 AWS 文档的情况一样,如果我们应该使用“integration.request.header”,则会造成混淆。格式。如果在 AWS 控制台中设置,则无需键入“integration.request.header”。只需键入 header 名称即可。确保 header 值是 单引号

但是,当使用像CDK或CFN这样的工具时,我们需要使用“integration.request.header”。部分。

cdk_api_method: aws_apigateway.Method = cdk_api_resource.add_method(
    http_method="post",
    integration=aws_apigateway.HttpIntegration(
        url=url,
        http_method="post",
        proxy=True,
        options=aws_apigateway.IntegrationOptions(
            request_parameters={
                "integration.request.header.{}".format(HTTP_HEADER_X_VALIDATION_CLIENT_NAME): "'{}'".format(HTTP_HEADER_X_VALIDATION_CLIENT_VALUE)
            }
        )
    )
)

设置 WAF 以验证 HTTP header 值并将 ALB 关联到 WAF ACL。

# https://github.com/aws-samples/wafv2-json-yaml-samples/blob/master/JSON/rule-001.json
aws_wafv2.CfnWebACL.RuleProperty(
    name='header-x-validation-client',
    action=aws_wafv2.CfnWebACL.RuleActionProperty(
        allow={}
    ),
    statement=aws_wafv2.CfnWebACL.StatementOneProperty(
        byte_match_statement=aws_wafv2.CfnWebACL.ByteMatchStatementProperty(
            field_to_match=aws_wafv2.CfnWebACL.FieldToMatchProperty(
                single_header={
                  "Name": HTTP_HEADER_X_VALIDATION_CLIENT_NAME
                }
            ),
            positional_constraint="EXACTLY",
            search_string=HTTP_HEADER_X_VALIDATION_CLIENT_VALUE,
            text_transformations=[
                aws_wafv2.CfnWebACL.TextTransformationProperty(
                    priority=0,
                    type="NONE"
                )
            ]
        )
    ),
    visibility_config=aws_wafv2.CfnWebACL.VisibilityConfigProperty(
        sampled_requests_enabled=True,
        cloud_watch_metrics_enabled=True,
        metric_name='waf-rule-header-x-validation-client'
    ),
    priority=0
)