Amazon VPC Lattice 功能介绍与最佳实践


概述

Amazon VPC Lattice(下文简称 Lattice)是全托管的 application networking 服务,用来连接、保护和监控应用的 service 与 resource。它可用于单个 VPC,也可跨多个 VPC、多个 account。

它主要解决的问题:

  • 微服务分散在不同团队、不同 account、不同 VPC 时,service-to-service 的 connectivity、service discovery、authorization、monitoring 各自实现,复杂度高。
  • 官方文档称其支持 overlapping CIDR,因此不必为了互通而先规划不冲突的地址空间。
  • 把「谁能调用谁」从网络层(routing / Security Group)上移到应用层(IAM auth policy),并统一产生 access log 和 metrics。

官方定义的三类角色(可以重叠):

角色 典型身份 职责
Service network owner network / cloud administrator 创建并共享 service network,设置 coarse-grained access control,管理 VPC association
Service owner developer 创建 service、定义 listener rule、把 service associate 到 service network,设置 fine-grained access control
Resource owner developer / DBA 为 database 等 resource 创建 resource configuration 并设置访问

核心概念

概念 说明
service 可独立部署的应用单元,由 listener、rule、target group 组成。可运行在 EC2、ECS / EKS / Fargate 或 Lambda 上
target group 一组承载应用的 target。类似 ELB 的 target group,但两者不可互换
listener 按 protocol + port 监听 request,并转发到 target group
rule listener 的 routing rule,含 priority、condition、action;每个 listener 有一条不可带 condition 的 default rule
service network service 与 resource configuration 的 logical boundary;同一 service network 内、被授权的 client 与 service 才能互通
service directory 本 account 拥有或通过 AWS RAM 共享给本 account 的所有 Lattice service 的 registry
auth policy 附加在 service 或 service network 上的 IAM 风格 policy
resource gateway / resource configuration 用于共享 RDS database、IP、domain name 等非 HTTP 的 resource:resource gateway 是 resource 所在 VPC 的 ingress point,resource configuration 是对 resource(或 resource 组)的逻辑描述

target group 支持的 target type(创建后不可更改):INSTANCE、IP、LAMBDA、ALB;ECS task 通过 IP 类型的 target group 自动注册,EKS Pod 则通过 AWS Gateway API Controller 注册。支持的 protocol 为 HTTP、HTTPS、TCP,port 范围 1-65535;TCP target group 只能配合 TLS listener 使用。

把 client VPC 接入 service network 有两种方式:

方式 特点
service network VPC association 一个 VPC 通过 association 只能连一个 service network;可在 association 上挂 Security Group;流量使用 Lattice managed prefix list 对应的地址;经 VPC Peering / Transit Gateway 过来的流量不能用这种方式穿过 service network
service network endpoint(VPC endpoint,基于 PrivateLink) 一个 VPC 可连多个 service network;使用 VPC 自己的 IP;VPC Peering、Transit Gateway、Direct Connect、VPN 过来的流量可经由它访问,是 on-premises 访问的途径

常用功能

1. Service network 与 association

  • 做什么:创建 service network,把 service、resource configuration、VPC(或 service network endpoint)associate 进来。
  • 典型用法:platform team 统一建一个 service network,各业务 account 把自己的 VPC 和 service associate 进去。
  • 注意点:没有 VPC association(或 service network endpoint)与 service association,client 无法访问 service;删除 VPC association 后,该 VPC 内的 client 无法再访问 service network 中的 service。
# Create a service network (IAM auth enabled)
aws vpc-lattice create-service-network --name my-network --auth-type AWS_IAM

# Associate a VPC with the service network, and attach a Security Group
aws vpc-lattice create-service-network-vpc-association \
    --service-network-identifier sn-0123456789abcdef0 \
    --vpc-identifier vpc-1a2b3c4d \
    --security-group-ids sg-7c2270198example

# Update the Security Groups on the association (replaces the existing list, and cannot be emptied)
aws vpc-lattice update-service-network-vpc-association \
    --service-network-vpc-association-identifier snva-0123456789abcdef0 \
    --security-group-ids sg-7c2270198example sg-903004f88example

service 与 service network 的 association 使用 create-service-network-service-association,具体参数见 CLI reference(待核实具体参数写法)。

2. Service、listener 与 rule

  • 做什么:service 通过 listener + rule 把 request 路由到 target group。
  • rule 要点:
    • action 支持 forward(可带多个 target group 与 weight)和 fixed-response(直接返回自定义 status code,如 404)。
    • condition 支持 path(prefix / exact)、header(prefix / exact / contains)、method(exact,区分大小写);均不支持 wildcard。
    • priority 范围 1-100,数值小者先评估,default rule 最后评估;rule priority 不可重复。
    • console 只能配 path condition,header / method condition 需用 CLI 或 SDK(create-rule)。
  • Protocol version:target group 默认 HTTP/1.1,可选 HTTP/2、gRPC;gRPC 要求 listener 为 HTTPS,且 target type 只能是 INSTANCE 或 IP,不能用 Lambda。
  • Timeout:connection idle timeout 默认 60 秒,可通过 idleTimeoutSeconds 在 60-600 秒调整(仅 CLI / API);connection 最长持续 10 分钟。
  • 路由算法默认 round robin;若 target group 内所有 target 都 unhealthy,会 fail open(把流量发给所有 target)。

3. Service discovery 与 custom domain name

  • 默认 domain name 格式:{service_name}-{service_id}.{partition_id}.vpc-lattice-svcs.{region}.on.aws。client 用该 DNS name 访问,经 Route 53 Resolver 导向 Lattice。
  • Custom domain name:创建 service 时指定;之后可用 Route 53 的 alias record(官方推荐,可用于 zone apex)或 CNAME 映射到 Lattice 生成的 domain name。
  • 注意点:
    • 一个 service 只能有一个 custom domain name,创建后不可修改;
    • 同一 service network 内 custom domain name 必须唯一;
    • HTTPS 必须自带 ACM certificate(没有 default certificate 兜底),否则 HTTPS 连接失败;
    • TCP listener 必须指定 custom domain name,TLS termination 在应用自己做。
aws vpc-lattice create-service --name parking \
    --custom-domain-name parking.example.com \
    --certificate-arn arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012

4. Access control(auth policy)

  • 做什么:auth policy 是 IAM policy document,可挂在 service network 和 service 两个层级,每个对象最多一份。
  • 开启方式:对象的 --auth-type 设为 AWS_IAM,再用 put-auth-policy 写入 policy;NONE 表示不做 IAM auth。开启 IAM auth 后若不配 policy,所有 request 都会被拒绝。
  • request 必须使用 SigV4 签名(anonymous caller 只有在 policy 允许时才可访问)。
  • 评估逻辑:caller identity 的 IAM policy、service network 的 auth policy、service 的 auth policy 都需要显式 Allow;任何显式 Deny 优先。
  • Action prefix 为 vpc-lattice-svcs(如 vpc-lattice-svcs:Invoke);常用 condition key 包括 vpc-lattice-svcs:SourceVpc、vpc-lattice-svcs:RequestMethod、vpc-lattice-svcs:RequestPath,以及 global condition key aws:PrincipalOrgID、aws:PrincipalTag/*、aws:ResourceTag/*。
  • 注意点:policy JSON 不能含换行或空行;变更可能需要几分钟生效;service network 的 auth policy 不适用于 resource configuration;移除 policy 前需先把 auth type 改为 NONE。
aws vpc-lattice update-service-network --service-network-identifier sn-0123456789abcdef0 --auth-type AWS_IAM
aws vpc-lattice put-auth-policy --resource-identifier sn-0123456789abcdef0 --policy file://policy.json

Policy 示例:只允许同一 AWS Organization 内已认证的 principal 调用。

{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":"*","Action":"vpc-lattice-svcs:Invoke","Resource":"*","Condition":{"StringEquals":{"aws:PrincipalOrgID":["o-123456example"]}}}]}

5. Security Group

  • VPC association(或 service network endpoint)上可挂 Security Group,控制哪些 client 能访问 service network;每个 association 最多 5 个。
  • Target-side Security Group:inbound 需放行来自 Lattice 的流量(含 health check)。因为流量来自 Lattice,不能用 client 的 Security Group 作为 source,应引用 Lattice managed prefix list com.amazonaws.{region}.vpc-lattice(IPv6 为 com.amazonaws.{region}.ipv6.vpc-lattice)。
  • Client-side Security Group:若自定义了 outbound rule,需放行到该 managed prefix list 的 listener port。

6. Cross-account sharing(AWS RAM)

  • 可共享三类实体:service network、service、resource configuration;可共享给任意 AWS account、OU 或整个 Organization,不能共享给单个 IAM user / role;不能再次共享别人共享给你的实体。
  • consumer 可以把自己的 VPC associate 到共享的 service network,或把自己的 service associate 进去,也可创建 service network endpoint;只能删除自己创建的 association,而 service network owner 可解除任何 association。
  • 停止共享后,已有 association 保留,但不允许新建 association。
  • cross-account 操作会在 CloudTrail 中记录 ...BySharee / ...ByOwner event。

7. Traffic management

  • Weighted routing:forward action 里指定多个 target group,weight 范围 0-999,按比例分流,可用于 blue/green、canary release。
  • 基于 path / header / method 的 routing,配合 fixed-response 兜底返回 404 等。
  • target 是 ALB 时,一个 internal ALB 最多可作为两个 Lattice service 的 target(分别用两个 target group)。
  • AZ affinity:Lattice 优先返回与 client 同 AZ 的地址,该 AZ 不可用时才返回其他 AZ;Lattice 不收取 cross-AZ data transfer 费用。

8. Observability

  • Access logs:默认关闭,可挂在 service network、service 或 resource configuration 上;destination 为 CloudWatch Logs、S3、Amazon Data Firehose;使用 create-access-log-subscription 启用,属于 vended logs,会产生 log 费用。
  • Log 字段包括 callerPrincipal、authDeniedReason(Service / Network / Identity)、sourceVpcId、targetGroupArn、requestId、failureReason 等,排查 403 时 authDeniedReason 可直接指出是哪一层拒绝。
  • Request tracing:Lattice 自动生成 x-amzn-requestid,传给 target、返回 client 并写入 access log;client 也可自行设置(超过 512 字节会被截断)。
  • CloudWatch metrics 位于 AWS/VpcLattice namespace,保留 15 个月。service 维度的 metrics 如 TotalRequestCount、RequestTime、HTTPCode_5XX_Count、HTTPCode_VpcLattice_403_Count 等;target group 维度(TargetGroup、AvailabilityZone)有 connection 数、bytes、TLS error 数等。
  • Metrics 默认发布到 service owner 的 account;service network owner 不会收到 VPC resource 相关的 metrics。
aws cloudwatch list-metrics --namespace AWS/VpcLattice

典型架构:服务间调用的完整请求链路

场景:Account B 的 checkout service(运行在 ECS,VPC-B)通过 service network 调用 Account C 的 orders service(VPC-C)。service network 由 Account A(platform team)创建,并通过 AWS RAM 共享给 B 和 C。

 Consumer: checkout (Account B / VPC-B)            On-premises client
 request: POST https://orders.example.com/v2/pay    (Direct Connect / VPN)
        |                                                   |
        | (1) DNS: orders.example.com                       |
        |     CNAME -> Lattice domain name                  |
        |     Route 53 Resolver -> Lattice address          |
        v                                                   v
 +------------------------------+          +------------------------------+
 | VPC association (+ SG)       |          | Service network endpoint     |
 | (2) client-side SG: egress   |          | (PrivateLink, + SG)          |
 |     to Lattice managed       |          | on-premises / Peering / TGW  |
 |     prefix list              |          | traffic enters here          |
 +--------------+---------------+          +---------------+--------------+
                |                                          |
                +--------------------+---------------------+
                                     |
                                     v
 +------------------------------------------------------------------------+
 | Service network "prod-net"  (owner: Account A, shared via AWS RAM)     |
 |                                                                        |
 |   (3) Auth policy @ service network    coarse-grained guardrail        |
 |       e.g. aws:PrincipalOrgID = o-123456example                        |
 |                      | Allow (SigV4-signed request, IAM role of B)     |
 |                      v                                                 |
 |   Service "orders"  (owner: Account C, associated to prod-net)         |
 |                                                                        |
 |   (4) Auth policy @ service            fine-grained rule               |
 |       e.g. role = checkout-task-role, RequestPath = /v2/*              |
 |                      | Allow                                           |
 |                      v                                                 |
 |   (5) Listener  HTTPS:443  (ACM certificate, custom domain name)       |
 |                      |                                                 |
 |                      v                                                 |
 |   (6) Rule, evaluated by priority (smallest first)                     |
 |       priority 10: path prefix /v2/ -> forward                         |
 |                    TG-v2 (weight 90) + TG-v2-canary (weight 10)        |
 |       default rule: fixed-response 404                                 |
 +----------------------------------+-------------------------------------+
                                    |
                                    v
 +------------------------------------------------------------------------+
 | (7) Target group  (health check, AZ affinity, round robin)             |
 |     Account C / VPC-C                                                  |
 |     target-side SG: inbound allows Lattice managed prefix list         |
 |                                                                        |
 |     +----------+   +-------------+   +----------+   +-------+          |
 |     | EC2      |   | ECS (IP)    |   | Lambda   |   | ALB   |          |
 |     | INSTANCE |   | / EKS Pod   |   | LAMBDA   |   |       |          |
 |     +----------+   +-------------+   +----------+   +-------+          |
 +------------------------------------------------------------------------+

 (8) Response goes back along the same path.
     Access logs + CloudWatch metrics are produced at the service /
     service network; x-amzn-requestid links client, Lattice and target.

请求链路分步说明(以及出问题时的典型现象):

步骤 发生了什么 出问题时的典型现象
(1) DNS client 解析 custom domain name 或默认 domain name,经 Route 53 Resolver 得到 Lattice 地址 解析不到、或解析到非 Lattice 地址:检查 VPC 的 DNS support / DNS hostnames、alias record 或 CNAME
(2) 接入 request 经 VPC association(或 service network endpoint)进入 service network;client-side Security Group 此处生效 connection timeout:Security Group 没放行到 managed prefix list,或 VPC 没有 association
(3) Network 层授权 service network 的 auth policy 校验(需 SigV4 签名) 403,authDeniedReason = Network
(4) Service 层授权 service 的 auth policy 校验 path / method / principal 等 403,authDeniedReason = Service
(5) Listener 按 protocol + port 接收;HTTPS 用 ACM certificate 终止 TLS TLS 握手失败:certificate 与 custom domain name 不匹配
(6) Rule 按 priority 匹配 condition,执行 forward(按 weight 分流)或 fixed-response 命中非预期 rule 或 default rule 的 404:检查 priority 与 condition
(7) Target 选择 healthy target(优先同 AZ),target-side Security Group 放行后到达应用 5xx / timeout:health check 失败、target-side Security Group 没放行 managed prefix list
(8) 响应与观测 响应原路返回;写入 access log 与 metrics 用 requestId 在 access log 与应用日志之间对账

最佳实践

安全

  • 两层 auth policy 分工:service network 层放 coarse-grained guardrail(如 aws:PrincipalOrgID),service 层放 fine-grained rule(path / method / role);因为两层与 caller 的 IAM policy 都要显式 Allow,任一层都能收紧。
  • 对外暴露的 service 使用 AWS_IAM 而非 NONE,并要求 SigV4 签名;因为 NONE 下 service network 内任何 client 都能访问。
  • 在 VPC association 上挂 Security Group,target-side Security Group 只放行 Lattice managed prefix list;因为这样 target 只接受 Lattice 来的流量。
  • 用 aws:PrincipalTag、aws:ResourceTag 做 policy condition;因为不必硬编码 account ID 或 service ARN。
  • 注意 service network 的 auth policy 不覆盖 resource configuration;database 类 resource 需通过 resource gateway 的 Security Group 等其他手段保护。

高可用

  • target group 配 health check,并让 target 分布在多个 AZ;因为 Lattice 优先 AZ affinity,target 过于集中会让某个 AZ 承压。
  • 对 fail open 行为要有预期:全部 target unhealthy 时流量仍会发给所有 target,需靠 CloudWatch alarm,而不是依赖自动摘除。
  • blue/green、canary release 使用 weighted target group,并保留 rollback weight;因为改 weight 即可快速切流。

性能

  • 提前评估单 service 单 AZ 的 bandwidth(默认 10 Gbps)与 request rate(默认 10,000 RPS)上限,必要时联系 SA / TAM 提升。
  • 长连接场景注意 connection 最长 10 分钟及 idle timeout;client 要有 reconnect 逻辑,必要时调大 idleTimeoutSeconds。
  • gRPC / HTTP/2 通过 target group 的 protocol version 配置;因为默认是 HTTP/1.1。

成本

  • 计费随 service 数量、request 数、数据量增长;把小 service 合并或按 path routing 到同一 service,可以减少按 service 计的小时费用(需权衡团队边界)。
  • 仅在需要时开启 access logs,并为 S3 / CloudWatch Logs 设置 retention 与 tiering 策略;因为 vended logs 会产生 log 费用。
  • Lattice 不收 cross-AZ data transfer 费,可利用这一点替代自建 cross-AZ proxy。

运维与 monitoring

  • 为 HTTPCode_5XX_Count、HTTPCode_VpcLattice_403_Count、RequestTime 的 p99 等建立 CloudWatch alarm。
  • 排查 403 时先看 access log 的 authDeniedReason 和 failureReason。
  • 用 x-amzn-requestid 串联 client、target 与 logs。
  • 用 CloudFormation / Gateway API Controller 管理 resource,避免 console 手工配置 drift;注意 auth policy 变更有几分钟延迟。
  • cross-account 操作审计看 CloudTrail 的 ...BySharee event。

常见坑与限制

  • 一个 VPC 通过 association 只能连一个 service network;要连多个需改用 service network endpoint。
  • 经 VPC Peering / Transit Gateway 来的流量,不能走 service network VPC association 穿过,必须使用 service network endpoint。
  • target group 的 target type 创建后不可改;IP 类型的 target 必须在该 VPC 的 subnet 内,不能注册 VPC endpoint 或 public routable IP;需要 VPC 外的 IP 时,用 ALB 类型并把 IP 注册到 ALB。
  • 被 service listener 引用的 target group 必须与 service 在同一 account。
  • Lambda target group 只能注册一个 function。
  • custom domain name 创建后不可修改,一个 service 仅一个;HTTPS 没有 default certificate。
  • listener rule 不支持 wildcard;console 只能配 path condition。
  • 有 6 个 AZ ID 不支持 Lattice:use1-az3、usw1-az2、apne1-az3、euw1-az4、cac1-az3、ilc1-az2,部署 subnet 前需确认 AZ ID。
  • Windows / macOS client 使用 VPC association 时需为 managed prefix list 加 static route;官方建议改用 service network endpoint 避免 static route。
  • 不能删光 VPC association 上的所有 Security Group;需要时只能删除 association 再重建。

官方文档中的主要 quota(均为 default 值,Region 级,除注明外可通过 Service Quotas 申请调整):

项目 Default 可调整
service network / Region 50 是
service / Region 2,000 是
target group / Region 500 是
VPC association / service network 500 是
service association / service network 500 是
service network endpoint / service network 200 是
listener / service 2 是
rule / listener 10 是
target group / service 10 是
target / target group 1,000 是
Security Group / association 5 否
auth policy size 10 KB 否
bandwidth / service / AZ 10 Gbps 联系 SA / TAM
request rate / service / AZ(HTTP) 10,000 RPS 联系 SA / TAM
max connection duration(service) 10 分钟 否

选型与对比

计费维度(不含价格):按 service 的 provisioned 时长(小时)、经 service 处理的数据量(GB)、HTTP / HTTPS request 数、TLS passthrough 的 TCP connection 数;VPC resource 按 service network 中 resource 的小时数及数据量计费;VPC association 与 service network endpoint 无额外费用,resource endpoint 的费用见 PrivateLink pricing 页。

维度 VPC Lattice PrivateLink(interface endpoint / endpoint service) Transit Gateway VPC Peering ALB
层级 application layer(以 L7 为主,也支持 TCP) network layer,面向单个 service / resource 的 private connectivity network layer,VPC / on-premises 互联的 routing hub network layer,两个 VPC 点对点互联 application layer 的 load balancer
解决问题 多 service、多 VPC 的 discovery、authorization、routing、monitoring 把某个 service 私有暴露给 consumer 大规模 VPC / on-premises 网络的 routing 互通 两个 VPC 互通 单个 service 的 L7 load balancing
overlapping CIDR 支持 支持 不支持(需 routing 不冲突) 不支持 不适用
Access control auth policy + Security Group endpoint policy + Security Group route table + Security Group / NACL route table + Security Group / NACL Security Group + listener rule
Cross-account AWS RAM sharing endpoint service allowlist AWS RAM 共享 Transit Gateway cross-account peering request 经 Lattice 或网络互联
典型选择 microservice 之间 service-mesh 式的互访、多团队多 account 单一 service 对外提供、SaaS 暴露 全网络互联、hybrid cloud 少量 VPC 简单互联 单个 service 的入口,可作 Lattice target

选型提示:

  • 需要 service-to-service 的 discovery + IAM authorization + 统一 monitoring:用 VPC Lattice;仅需把一个 service 私有暴露:PrivateLink 更简单。
  • 需要全量网络互通(任意 port、任意 protocol、含非 HTTP):Transit Gateway / VPC Peering。
  • ALB 是 load balancer,可作为 Lattice 的 target;两者通常组合使用而不是二选一。
  • on-premises 访问 Lattice:经 service network endpoint 走 Direct Connect / VPN。
  • 注意:service network endpoint 与 resource endpoint 都基于 PrivateLink 创建,与 Lattice 是配合关系。

SAA 考点速记

VPC Lattice 在 SAA 考试中涉及较少(待核实是否出现于现行考纲),了解到概念层即可,更常见的是网络选型:

  • 多个 VPC / account 的微服务要统一 service discovery、IAM authorization 与 monitoring,且 CIDR 可能重叠:VPC Lattice(service network)。
  • 只把单个 service 私有暴露给其他 VPC / account、无需打通整个网络:PrivateLink。
  • 大量 VPC 与 on-premises 网络互联、集中 routing:Transit Gateway;两个 VPC 简单互通:VPC Peering(不可传递、CIDR 不可重叠)。
  • Lattice 的 access control = service network 与 service 两层 auth policy(IAM,SigV4)+ VPC association 上的 Security Group。
  • cross-account 共享 Lattice 的 service / service network 使用 AWS RAM。

参考文档