

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在跨区域自动复制 Amazon RDS 实例 AWS 账户
<a name="automate-the-replication-of-amazon-rds-instances-across-aws-accounts"></a>

*Parag Nagwekar 和 Arun Chandapillai，Amazon Web Services*

## Summary
<a name="automate-the-replication-of-amazon-rds-instances-across-aws-accounts-summary"></a>

此模式向您展示了如何使用 AWS Step Functions 和自动跨不同的 AWS 账户 复制、跟踪和回滚亚马逊关系数据库服务 (Amazon RDS) 数据库实例。 AWS Lambda您可以使用此自动化来执行 RDS 数据库实例的大规模复制，而不会影响性能或运营开销 - 无论您的组织规模如何。您还可以使用这种模式来帮助您的组织遵守强制性的数据治理策略或合规性要求，这些策略或合规性要求要求在不同的 AWS 账户 和之间复制和冗余数据 AWS 区域。大规模 Amazon RDS 数据的跨账户复制是低效且容易出错的手动过程，可能成本高昂且耗时，但此模式中的自动化可以帮助您安全、有效且高效地实现跨账户复制。

## 先决条件和限制
<a name="automate-the-replication-of-amazon-rds-instances-across-aws-accounts-prereqs"></a>

**先决条件**
+ 二 AWS 账户
+ 一个 RDS 数据库实例，在源中启动并正在运行 AWS 账户
+ 目标中 RDS 数据库实例的子网组 AWS 账户
+ 在源中创建 AWS 账户 并与目标账户共享的 AWS Key Management Service (AWS KMS) 密钥（有关策略详细信息的更多信息，请参阅此模式[的其他信息](#automate-the-replication-of-amazon-rds-instances-across-aws-accounts-additional)部分。）
+ 目标 AWS KMS key 中的一个 AWS 账户 ，用于加密目标账户中的数据库

**限制**
+ 有些 AWS 服务 并非全部可用 AWS 区域。有关区域可用性，请参阅[按区域划分的AWS 服务](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)。有关特定端点，请参阅[服务端点和配额](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)页面，然后选择相应服务的链接。

**产品版本**
+ Python 3.9（使用 AWS Lambda）
+ PostgreSQL 11.3、13.x 和 14.x

## 架构
<a name="automate-the-replication-of-amazon-rds-instances-across-aws-accounts-architecture"></a>

**技术堆栈**
+ Amazon Relational Database Service(Amazon RDS)
+ Amazon Simple Notification Service（Amazon SNS）
+ AWS Key Management Service (AWS KMS)
+ AWS Lambda
+ AWS Secrets Manager
+ AWS Step Functions

**目标架构**

下图显示了一种架构，用于使用 Step Functions 编排 RDS 数据库实例从源账户（账户 A）到目标账户（账户 B）的定时按需复制。

![\[使用 Step Functions 在源账户和目标账户之间复制 Amazon RDS 数据库实例。\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/6310ad9b-1b1a-4a67-b684-ef605fef3e87/images/001550bb-cf6b-493d-9de9-0229a43753a1.png)


在源账户（图中的账户 A）中，Step Functions 状态机执行以下操作：

1. 从账户 A 的 RDS 数据库实例创建快照

1. 使用 AWS KMS key 来自账户 A 复制和加密快照。为确保传输过程中的加密，无论数据库实例是否加密，都将对快照进行加密。

1. 通过授予账户 B 对快照的访问权限，与账户 B 共享数据库快照。

1. 向 SNS 主题推送通知，然后 SNS 主题调用账户 B 中的 Lambda 函数。

在目标账户（图中的账户 B）中，Lambda 函数运行 Step Functions 状态机来编排以下内容：

1. 将共享快照从账户 A 复制到账户 B，同时使用 AWS KMS key 来自账户 A 先解密数据，然后使用账户 B AWS KMS key 中的账户对数据进行加密。

1. 从 Secrets Manager 中读取密钥，以捕获当前数据库实例的名称。

1. 使用新名称和 Amazon RDS 的默认 AWS KMS key 名称从快照中恢复数据库实例。

1. 读取新数据库的端点并使用新的数据库端点更新 Secrets Manager 中的密钥，然后为以前的数据库实例添加标签，以便日后将其删除。

1. 保留数据库最新 N 个实例，并删除所有其他实例。

## 工具
<a name="automate-the-replication-of-amazon-rds-instances-across-aws-accounts-tools"></a>

**AWS 服务**
+ [Amazon Relational Database Service（Amazon RDS）](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Welcome.html)可帮助您在 AWS 云中设置、操作和扩展关系数据库。
+ [Amazon Simple Notification Service（Amazon SNS）](https://docs.aws.amazon.com/sns/latest/dg/welcome.html)可帮助您协调和管理发布者与客户端（包括 Web 服务器和电子邮件地址）之间的消息交换。
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)帮助您设置 AWS 资源，快速一致地配置资源，并在和的整个 AWS 账户 生命周期中对其进行管理 AWS 区域。
+ [AWS Key Management Service (AWS KMS)](https://docs.aws.amazon.com/kms/latest/developerguide/overview.html) 可帮助您创建和控制加密密钥以帮助保护您的数据。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一项计算服务，可帮助您运行代码，无需预调配或管理服务器。它只在需要时运行您的代码，并自动进行扩展，因此您只需为使用的计算时间付费。
+ [适用于 Python (Boto3) 的 AWS SDK](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html)是一个软件开发套件，可帮助您将 Python 应用程序、库或脚本与集成 AWS 服务。
+ [AWS Secrets Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html) 可帮助您将代码中的硬编码凭证（包括密码）替换为对 Secrets Manager 进行的 API 调用，以便以编程方式检索此密钥。
+ [AWS Step Functions](https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html)是一项无服务器编排服务，可帮助您组合 Lambda 函数和其他函数 AWS 服务 来构建业务关键型应用程序。

**代码存储库**

此模式的代码可在 GitHub [跨账户 RDS 复制](https://github.com/aws-samples/aws-rds-crossaccount-replication)存储库中找到。

## 操作说明
<a name="automate-the-replication-of-amazon-rds-instances-across-aws-accounts-epics"></a>

### 只需单击一下即可自动复制 RDS 数据库实例 AWS 账户
<a name="automate-the-replication-of-rds-db-instances-across-aws-accounts-with-a-single-click"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 在源账户中部署 CloudFormation 堆栈。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-the-replication-of-amazon-rds-instances-across-aws-accounts.html) | 云管理员、云架构师 | 
| 在目标账户中部署 CloudFormation 堆栈。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-the-replication-of-amazon-rds-instances-across-aws-accounts.html) | 云架构师、 DevOps 工程师、云管理员 | 
| 验证目标账户中是否创建了 RDS 数据库实例。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-the-replication-of-amazon-rds-instances-across-aws-accounts.html) | 云管理员、云架构师、 DevOps 工程师 | 
| 将 Lambda 函数订阅至 SNS 主题。 | 您必须运行以下 AWS Command Line Interface (AWS CLI) 命令才能将目标账户（账户 B）中的 Lambda 函数订阅到源账户（账户 A）中的 SNS 主题。在账户 A 中，运行以下命令：<pre>aws sns add-permission \<br />--label lambda-access --aws-account-id <DestinationAccount> \<br />--topic-arn <Arn of SNSTopic > \<br />--action-name Subscribe ListSubscriptionsByTopic </pre>在账户 B 中，运行以下命令：<pre>aws lambda add-permission \<br />--function-name <Name of InvokeStepFunction> \<br />--source-arn <Arn of SNSTopic > \<br />--statement-id function-with-sns \<br />--action lambda:InvokeFunction \<br />--principal sns.amazonaws.com</pre>在账户 B 中，运行以下命令：<pre>aws sns subscribe \<br />--protocol "lambda" \<br />--topic-arn <Arn of SNSTopic> \<br />--notification-endpoint <Arn of InvokeStepFunction></pre> | 云管理员、云架构师、数据库管理员 | 
| 将源账户中的 RDS 数据库实例与目标账户同步。 | 通过在源账户中启动 Step Functions 状态机，启动按需数据库复制。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-the-replication-of-amazon-rds-instances-across-aws-accounts.html)调度程序可帮助您按计划自动运行复制，但默认情况下调度程序处于关闭状态。您可以在目标账户 CloudFormation 堆栈的 “**资源**” 选项卡中找到计划程序的 Amazon CloudWatch 规则名称。有关如何修改 CloudWatch 事件规则的说明，请参阅 CloudWatch 文档中的[删除或禁用 CloudWatch 事件规则](https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/Delete-or-Disable-Rule.html)。 | 云架构师、 DevOps 工程师、云管理员 | 
| 需要时，可以将数据库回滚至之前的任何副本。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-the-replication-of-amazon-rds-instances-across-aws-accounts.html) | 云管理员、数据库管理员、工程师 DevOps  | 

## 相关资源
<a name="automate-the-replication-of-amazon-rds-instances-across-aws-accounts-resources"></a>
+ [跨区域只读副本](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RDS_Fea_Regions_DB-eng.Feature.CrossRegionReadReplicas.html)（Amazon RDS 文档）
+ [蓝绿部署](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RDS_Fea_Regions_DB-eng.Feature.BlueGreenDeployments.html)（Amazon RDS 文档）

## 附加信息
<a name="automate-the-replication-of-amazon-rds-instances-across-aws-accounts-additional"></a>

您可以使用以下示例策略来 AWS KMS key 共享您的信息 AWS 账户。

```
{
    "Version": "2012-10-17",		 	 	 
    "Id": "cross-account-rds-kms-key",
    "Statement": [
        {
            "Sid": "Enable user permissions",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::<SourceAccount>:root"
            },
            "Action": "kms:*",
            "Resource": "*"
        },
        {
            "Sid": "Allow administration of the key",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::<DestinationAccount>:root"
            },
            "Action": [
                "kms:Create*",
                "kms:Describe*",
                "kms:Enable*",
                "kms:List*",
                "kms:Put*",
                "kms:Update*",
                "kms:Revoke*",
                "kms:Disable*",
                "kms:Get*",
                "kms:Delete*",
                "kms:ScheduleKeyDeletion",
                "kms:CancelKeyDeletion"
            ],
            "Resource": "*"
        },
        {
            "Sid": "Allow use of the key",
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::<DestinationAccount>:root",
                    "arn:aws:iam::<SourceAccount>:root"
                ]
            },
            "Action": [
                "kms:Encrypt",
                "kms:Decrypt",
                "kms:ReEncrypt*",
                "kms:GenerateDataKey*",
                "kms:DescribeKey",
                "kms:CreateGrant"
            ],
            "Resource": "*"
        }
    ]
}
```