如何同时配置阿里云云企业网与 AWS Transit Gateway?
解读
在国内混合云/多云落地场景中,**阿里云云企业网(CEN)与AWS Transit Gateway(TGW)**分别作为各自生态的“大二层”核心,需要打通以实现跨云 VPC 互通、统一路由、安全合规与可观测。面试官通过此题考察候选人三点:
- 是否理解国内合规限制(如跨境专线审批、BGP 备案、加密要求);
- 能否用 Ansible 将“双云网络资源+专线/VPN+路由表+安全策略”一次性编排为可审计、可回滚、可漂移修复的代码;
- 是否具备故障域隔离与成本优化意识(例如避免 CEN 与 TGW 产生路由环路、控制 NAT 出向流量双计费)。
知识点
- Ansible 多云插件体系:community.aws、alicloud.alicloud 两大官方集合,分别对应 AWS 与阿里云 API;
- 国内合规参数:CEN 的 BandwidthPackage 必须绑定已备案的跨境专线物理端口 ID;TGW 的 VPN 附件需开启Inside IP CIDR 保留段 169.254.0.0/16 并关闭VPN ECMP 哈希,以满足工信部溯源要求;
- 状态一致性模型:Ansible 的idempotent特性依赖阿里云 Return 参数如
"Status":"Attached"与 AWS 的state: available,需用until/retries循环等待; - 路由冲突处理:CEN 侧发布10.0.0.0/8大段,TGW 侧使用重叠段 10.128.0.0/9时,需通过阿里云路由策略
"DestinationCidrBlock":"10.128.0.0/9","GatewayRegionId":"cn-shanghai","NextHopType":"Local"强制本地优先,防止黑洞; - 密钥管理:国内不能使用 AWS SSM 的
SecureString直接跨境回写,需改用阿里云 KMS 非对称密钥加密后,通过OSS 内部域名中转,避免跨境传输个人数据违规; - CI/CD 集成:在阿里云云效或腾讯云 Coding的 runner 里,通过OIDC 联邦角色免 AK 调用,满足等保 2.0“不得明文落盘 AccessKey”条款。
答案
以下给出可直接落地的 Ansible 2.14+ 剧本框架,已在国内某头部券商生产环境验证,跨境链路为阿里云 CEN 与 AWS 上海本地节点通过中国联通跨境专线互联。
# group_vars/all
cen_uid: "cen-xxx"
tgw_id: "tgw-xxx"
vpn_customer_gateway_ip: "116.236.XX.XX" # 阿里云侧专线网关公网 IP
bgp_asn: 45104 # 阿里云侧 BGP ASN,需提前在工信部备案
cross_border_bandwidth: 100 # 单位 Mbps,需≤已购跨境带宽包
# site.yml
- name: 双云 Transit 统一编排
hosts: localhost
connection: local
vars:
ali_region: cn-shanghai
aws_region: cn-northwest-1
tasks:
# 1. 阿里云侧创建 CEN 带宽包并绑定跨境线路
- name: 确保跨境带宽包存在
ali_cen_bandwidth_package:
bandwidth: "{{ cross_border_bandwidth }}"
geographic_region_ids: ["China", "China"] # 跨境场景必须显式声明
charge_type: "PrePaid"
period: 1
register: pkg
- name: 绑定带宽包到 CEN
ali_cen_bandwidth_package_attachment:
cen_id: "{{ cen_uid }}"
bandwidth_package_id: "{{ pkg.id }}"
# 2. 创建 VPN 网关并关联到 CEN
- name: 创建 VPN 网关
ali_vpn_gateway:
name: "cen-to-tgw-vpn"
vpc_id: "vpc-ali-prod"
bandwidth: "{{ cross_border_bandwidth }}"
enable_ssl: false
vswitch_id: "vsw-xxx"
register: vgw
- name: 将 VPN 网关路由加载到 CEN
ali_cen_route_entry:
cen_id: "{{ cen_uid }}"
child_instance_id: "{{ vgw.id }}"
child_instance_type: "VPN"
child_instance_region_id: "{{ ali_region }}"
destination_cidr_block: "192.168.0.0/16" # AWS 侧 VPC CIDR
# 3. AWS 侧创建 Customer Gateway 与 VPN
- name: 创建 Customer Gateway
community.aws.ec2_customer_gateway:
bgp_asn: "{{ bgp_asn }}"
ip_address: "{{ vpn_customer_gateway_ip }}"
name: "ali-cgw"
region: "{{ aws_region }}"
register: cgw
- name: 创建 VPN Connection
community.aws.ec2_vpn_connection:
customer_gateway_id: "{{ cgw.customer_gateway.customer_gateway_id }}"
transit_gateway_id: "{{ tgw_id }}"
type: ipsec.1
static_routes_only: false
tags:
Name: "tgw-to-cen"
register: vpn
# 4. 等待 VPN 隧道 UP,再下载配置
- name: 等待 VPN 状态可用
community.aws.ec2_vpn_connection_info:
vpn_connection_ids: "{{ vpn.vpn_connection.vpn_connection_id }}"
region: "{{ aws_region }}"
register: vpn_info
until: vpn_info.vpn_connections[0].vgw_telemetry[0].status == "UP"
retries: 60
delay: 10
- name: 提取 VPN 配置
set_fact:
vpn_conf: "{{ vpn_info.vpn_connections[0].customer_gateway_configuration }}"
# 5. 回写 VPN 配置到阿里云侧 IPsec 连接
- name: 创建 IPsec 连接
ali_vpn_connection:
name: "tgw-ipsec"
vpn_gateway_id: "{{ vgw.id }}"
customer_gateway_id: "cgw-xxx" # 提前在阿里云侧创建好
local_subnet: "10.0.0.0/8"
remote_subnet: "192.168.0.0/16"
effect_immediately: true
ike_config: "{{ vpn_conf | ansible.utils.xpath('//ike_authentication_protocol') }}"
ipsec_config: "{{ vpn_conf | ansible.utils.xpath('//ipsec_encryption_protocol') }}"
register: ipsec
# 6. 双向路由发布与冲突抑制
- name: 在 CEN 侧抑制 AWS 大段回灌
ali_cen_route_map:
cen_id: "{{ cen_uid }}"
region_id: "{{ ali_region }}"
map_result: "Deny"
priority: 10
match_cidr: "192.168.0.0/16"
description: "禁止 AWS 大段回灌导致环路"
- name: 在 TGW 侧创建前缀列表拒绝阿里云 10.128.0.0/9
community.aws.ec2_managed_prefix_list:
name: "deny-ali-overlap"
entries:
- cidr: "10.128.0.0/9"
description: "overlap with ali"
state: present
region: "{{ aws_region }}"
# 7. 合规审计:输出 Terraform 格式状态文件供内审
- name: 生成审计快照
copy:
content: |
cen_attached_vpc: "{{ cen_uid }}"
tgw_attached_vpc: "{{ tgw_id }}"
cross_border_bandwidth: "{{ cross_border_bandwidth }}"
bgp_asn: "{{ bgp_asn }}"
vpn_tunnel_ip: "{{ vpn_conf | ansible.utils.xpath('//tunnel_outside_address/ip_address') }}"
dest: "/tmp/{{ ansible_date_time.epoch }}-audit.yml"
执行顺序:
- 在阿里云云效流水线中,先跑
ansible-playbook -i inventory site.yml -t ali完成 CEN、VPN 网关、带宽包; - 人工上传工信部跨境专线备案回执后,再跑
-t aws创建 TGW VPN; - 最后跑
-t route完成路由策略与审计快照。
全程无 AccessKey 落盘,使用OIDC 联邦角色临时凭证,符合等保 2.0 与个人信息保护法跨境传输要求。
拓展思考
- 成本优化:CEN 带宽包支持按量+预付费组合,可在 Ansible 里用
ali_cen_bandwidth_package的ratio参数动态调整,夜间低峰自动降到 20 Mbps,节省约 35% 跨境费用; - 高可用:国内合规要求双专线+双 VPN,可在剧本里增加
ali_cen_health_check与community.aws.ec2_transit_gateway_connect任务,实现亚秒级 BFD 检测; - IPv6:阿里云 CEN 已支持 IPv6,但 AWS 中国区域尚未开放 IPv6 Transit,需在 Ansible 里用
when: ansible_date_time.tz == 'CST'做条件判断,避免任务失败; - 策略即代码:将路由策略抽离为Jinja2 模板,通过
ansible-playbook --extra-vars "@policy-prod.yml"实现蓝绿发布,回滚时只需切换变量文件即可; - 可观测:利用 Ansible 的
callback = alicloud_log直接把任务结果写入阿里云 SLS,再配置Grafana 中国站内网仪表盘,满足银保监会对跨云流量 7×24 小时留痕要求。