

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 自動化跨 的 Amazon RDS 執行個體複寫 AWS 帳戶
<a name="automate-the-replication-of-amazon-rds-instances-across-aws-accounts"></a>

*Parag Nagwekar 和 Arun Chandapillai，Amazon Web Services*

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

此模式說明如何 AWS 帳戶 使用 和 自動化複寫、追蹤和復原不同 Amazon Relational Database Service (Amazon RDS) 資料庫執行個體的程序 AWS Step Functions 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

## Architecture
<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_tw/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. 使用 AWS KMS key Amazon RDS 的新名稱和預設值，從快照還原資料庫執行個體。

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 [Crossaccount RDS 複寫](https://github.com/aws-samples/aws-rds-crossaccount-replication)儲存庫中使用。

## 史詩
<a name="automate-the-replication-of-amazon-rds-instances-across-aws-accounts-epics"></a>

### AWS 帳戶 只需按一下，即可自動化跨 的 RDS 資料庫執行個體複寫
<a name="automate-the-replication-of-rds-db-instances-across-aws-accounts-with-a-single-click"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在來源帳戶中部署 CloudFormation 堆疊。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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_tw/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_tw/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> | 雲端管理員、雲端架構師、DBA | 
| 從來源帳戶同步 RDS 資料庫執行個體與目的地帳戶。 | 透過啟動來源帳戶中的 Step Functions 狀態機器來啟動隨需資料庫複寫。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-the-replication-of-amazon-rds-instances-across-aws-accounts.html)排程器已就緒，可協助您按排程自動執行複寫，但排程器預設為關閉。您可以在目的地帳戶中 CloudFormation 堆疊的 資源索引標籤中找到排程器的 Amazon CloudWatch 規則名稱。****如需如何修改 CloudWatch Events 規則的說明，請參閱 [ CloudWatch 文件中的刪除或停用 CloudWatch Events 規則](https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/Delete-or-Disable-Rule.html)。 CloudWatch  | 雲端架構師、DevOps 工程師、雲端管理員 | 
| 視需要將資料庫復原至任何先前的複本。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-the-replication-of-amazon-rds-instances-across-aws-accounts.html) | 雲端管理員、DBA、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": "*"
        }
    ]
}
```