如何在中国区使用 CDK Pipelines 自动部署 Neptune 到两个 AZ?
解读
面试官真正想考察的是:
- 你是否了解 中国区(cn-north-1 / cn-northwest-1) 与 Global Region 在 服务可用性、IAM 隔离、CDK Bootstrap、资产发布 上的差异;
- 能否用 CDK Pipelines(基于 AWS CodePipeline) 把 Neptune 双可用区 + 子网组 + 参数化 + 安全合规 一次性自动化落地;
- 是否具备 故障域、网络隔离、最小权限、加密、审计 等生产级意识,而不是只把“双 AZ”简单理解为在两个子网里放实例。
知识点
- Neptune 双可用区架构:必须创建 DbSubnetGroup 且至少跨 两个私有子网(不同 AZ);Neptune 会自动把主实例与只读副本放在不同 AZ,无需手动指定 AZ。
- 中国区专属限制:
– CDK 资产(Lambda 层、模板文件 > 2 MB)必须落入 中国区的 S3 桶(aws-cn 域名),Bootstrap 时需显式指定
--trust-for-lookup与--qualifier。 – CodePipeline 的 Artifact Bucket 必须开启 KMS 加密,且 Bucket Policy 需允许 北京与宁夏 Region 互访(多 Region 灾备场景)。 – Neptune 本身 不支持免费试用版,VPC 必须 启用 DNS Resolution & DNS Hostnames,否则创建会卡在“验证阶段”。 - CDK Pipelines 关键构造:
–
CodePipeline主机构,默认生成 Self-Mutating 管道; –ShellStep用于跑cdk synth; –NeptuneSubnetGroup与DatabaseCluster放在 Stage 里,才能被管道顺序部署。 - 最小权限:Pipeline 角色只需
neptune:*、ec2:DescribeSubnets、kms:Decrypt、s3:GetObject等细粒度策略,禁止通配 * 资源。 - 加密与合规: – 中国区金融、政企项目强制 KMS-CMK 加密,Neptune 存储加密开启后 不可关闭; – 安全组仅放行 VPC 内 8182 端口,并附加 SG 级 SSM Session Manager 内网运维通道。
答案
-
环境准备
a. 在 中国区账户 执行cdk bootstrap aws-cn://123456789012/cn-northwest-1 --trust-for-lookup --qualifier neptune;
b. 创建 专有 VPC(含 2 个私有子网,分布在 cn-northwest-1a / 1b),enableDnsHostnames=true。 -
CDK 代码骨架(TypeScript)
export class NeptuneStage extends Stage {
constructor(scope: Construct, id: string, props?: StageProps) {
super(scope, id, props);
const stack = new Stack(this, 'NeptuneDualStack');
// 1. 子网组
const subnetGroup = new neptune.CfnDBSubnetGroup(stack, 'NeptuneSubnetGroup', {
dbSubnetGroupDescription: 'Dual AZ for Neptune',
subnetIds: props!.subnetIds, // 从上下文读入
});
// 2. 安全组
const sg = new ec2.SecurityGroup(stack, 'NeptuneSG', { vpc: props!.vpc });
sg.addIngressRule(ec2.Peer.ipv4(props!.vpc.vpcCidrBlock), ec2.Port.tcp(8182), 'Neptune Port');
// 3. 集群
const cluster = new neptune.CfnDBCluster(stack, 'Cluster', {
dbSubnetGroupName: subnetGroup.ref,
vpcSecurityGroupIds: [sg.securityGroupId],
engineVersion: '1.2.0.0',
dbClusterIdentifier: 'neptune-cn-prod',
preferredBackupWindow: '03:00-04:00',
backupRetentionPeriod: 7,
storageEncrypted: true,
kmsKeyId: props!.kmsKey.keyId,
enableCloudwatchLogsExports: ['audit'],
deletionProtection: true,
});
// 4. 双 AZ 实例
['1', '2'].forEach((idx, azIdx) => {
new neptune.CfnDBInstance(stack, `Instance${idx}`, {
dbClusterIdentifier: cluster.ref,
dbInstanceClass: 'db.r5.large',
dbInstanceIdentifier: `neptune-${azIdx}`,
// 不指定 AvailabilityZone,Neptune 自动分配
});
});
}
}
- 管道定义
const pipeline = new CodePipeline(app, 'NeptunePipeline', {
pipelineName: 'NeptuneDualAZ-CN',
synth: new ShellStep('Synth', {
input: CodePipelineSource.codeCommit(repo, 'main'),
commands: ['npm ci', 'npx cdk synth'],
primaryOutputDirectory: 'cdk.out',
}),
});
pipeline.addStage(new NeptuneStage(app, 'ProdNeptune', {
env: { account: '123456789012', region: 'cn-northwest-1' },
vpc: vpcLookup,
subnetIds: vpcLookup.selectSubnets({ subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }).subnetIds,
kmsKey: kmsKeyLookup,
}));
- 部署验证
a.cdk deploy触发管道,CodePipeline 控制台 显示源→构建→自修改→部署四阶段;
b. 部署完成后,在 Neptune 控制台 确认 Writer + Reader 位于 不同可用区;
c. 执行curl -G https://neptune-cluster.cluster-xyz.cn-northwest-1.neptune.amazonaws.com.cn:8182/status返回"status":"healthy"即成功。
拓展思考
- 跨 Region 只读副本:中国区北京与宁夏之间 没有专线计费豁免,需评估 数据传输成本;可通过 Neptune 全球数据库(Global Database) 做跨 Region 灾备,但 Global 功能在中国区尚未上线,需改用 DMS + Kinesis DataStream 自研同步。
- 蓝绿升级:Neptune 引擎升级会触发 双实例同时重启,若业务要求 零停机,需提前启用 代理终端(Neptune-Proxy) 并在管道里加入 Canary 测试步骤。
- 成本优化:双 AZ 最小规格为 db.r5.large;若开发环境仅需功能验证,可用 CDK 条件判断 在
isChina()且isDev()时切换为 单 AZ + 暂停自动启停 的 Neptune Serverless(已在中国区商用),节省 70% 费用。 - 合规审计:中国区等保三级要求 日志留存 6 个月,需把 CloudWatch Audit Log 通过 Kinesis Firehose 投递到 OSS 或 Glacier Deep Archive;CDK 里可用
CfnDeliveryStream构造与 Neptune 日志组 联动,实现 一键合规。