

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

# 使用 Terraform 設定資料庫遷移的 CI/CD 管道
<a name="set-up-ci-cd-pipeline-for-db-migration-with-terraform"></a>

*Rahul Sharad Gaikwad、Ashish Bhatt、Aniket Dekate、Ruchika Modi、Tamilselvan P、Nadeem Rahaman、Aarti Rajput 和 Naveen Suthar、Amazon Web Services*

## 總結
<a name="set-up-ci-cd-pipeline-for-db-migration-with-terraform-summary"></a>

此模式旨在建立持續整合和持續部署 (CI/CD) 管道，以可靠且自動化的方式管理資料庫遷移。它涵蓋了佈建必要基礎設施、遷移資料以及使用 Terraform 自訂結構描述變更的程序，Terraform 是一種基礎設施即程式碼 (IaC) 工具。

具體而言， 模式會設定 CI/CD 管道，將內部部署 Microsoft SQL Server 資料庫遷移至 上的 Amazon Relational Database Service (Amazon RDS) AWS。您也可以使用此模式，將虛擬機器 (VM) 或其他雲端環境中的 SQL Server 資料庫遷移至 Amazon RDS。

此模式可解決下列與資料庫管理和部署相關的挑戰：
+ 手動資料庫部署非常耗時、容易出錯，而且缺乏跨環境的一致性。
+ 協調基礎設施佈建、資料遷移和結構描述變更可能很複雜且難以管理。
+ 確保資料完整性並將資料庫更新期間的停機時間降至最低，對生產系統至關重要。

此模式提供下列優點：
+ 透過實作資料庫遷移的 CI/CD 管道，簡化更新和部署資料庫變更的程序。這可降低錯誤風險、確保跨環境的一致性，並將停機時間降至最低。
+ 有助於改善可靠性、效率和協同合作。可在資料庫更新期間加快上市時間並減少停機時間。
+ 協助您針對資料庫管理採用現代 DevOps 實務，進而在軟體交付程序中提高敏捷性、可靠性和效率。

## 先決條件和限制
<a name="set-up-ci-cd-pipeline-for-db-migration-with-terraform-prereqs"></a>

**先決條件**
+ 作用中 AWS 帳戶
+ 本機電腦上已安裝 Terraform 0.12 或更新版本 （如需說明，請參閱 [Terraform 文件](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli))
+ 來自 HashiCorp 的 Terraform AWS Provider 3.0.0 版或更新版本 （請參閱此供應商的 [GitHub 儲存庫](https://github.com/hashicorp/terraform-provider-aws))
+ 最低權限 AWS Identity and Access Management (IAM) 政策 （請參閱部落格文章[撰寫最低權限 IAM 政策的技術](https://aws.amazon.com/blogs/security/techniques-for-writing-least-privilege-iam-policies/))

## Architecture
<a name="set-up-ci-cd-pipeline-for-db-migration-with-terraform-architecture"></a>

此模式實作下列架構，為資料庫遷移程序提供完整的基礎設施。

![\[CI/CD 管道架構，用於將內部部署 SQL Server 資料庫遷移至 AWS 上的 Amazon RDS。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/87845d9f-8e6e-4c51-b9ee-9e7833671d05/images/a1e95458-419a-4de9-85ef-b17d8340700a.png)


在此架構中：
+ 來源資料庫是現場部署、虛擬機器 (VM) 或由其他雲端供應商託管的 SQL Server 資料庫。此圖假設來源資料庫位於內部部署資料中心。
+ 內部部署資料中心和 透過 VPN 或 AWS 連線進行 AWS Direct Connect 連線。這可提供來源資料庫與 AWS 基礎設施之間的安全通訊。
+ 目標資料庫是在資料庫佈建管道的協助 AWS 下，託管在 虛擬私有雲端 (VPC) 內的 Amazon RDS 資料庫。
+ AWS Database Migration Service (AWS DMS) 會將您的內部部署資料庫複寫到 AWS。它用於設定來源資料庫到目標資料庫的複寫。

下圖顯示設定不同層級資料庫遷移程序的基礎設施，其中包含佈建、 AWS DMS 設定和驗證。

![\[從內部部署到 AWS 的遷移程序 CI/CD 管道詳細資訊。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/87845d9f-8e6e-4c51-b9ee-9e7833671d05/images/3aca17e5-6fd7-4317-b578-ab5e485c6efb.png)


在此程序中：
+ 驗證管道會驗證所有檢查。當所有必要的驗證完成時，整合管道會移至下一個步驟。
+ 資料庫佈建管道由各種階段組成，這些 AWS CodeBuild 階段會對資料庫提供的 Terraform 程式碼執行 Terraform 動作。當這些步驟完成時，它會在目標中部署資源 AWS 帳戶。
+  AWS DMS 管道包含各種 CodeBuild 階段，這些階段會執行測試，然後使用 IaC 佈建執行遷移的 AWS DMS 基礎設施。

## 工具
<a name="set-up-ci-cd-pipeline-for-db-migration-with-terraform-tools"></a>

**AWS 服務 和 工具**
+ [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html) 是一種全受管持續整合服務，可編譯原始程式碼、執行測試，並產生ready-to-deploy的軟體套件。
+ [AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html) 是一種全受管持續交付服務，可協助您自動化發行管道，以實現快速可靠的應用程式和基礎設施更新。
+ [Amazon Relational Database Service (Amazon RDS)](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Welcome.html) 可協助您在 中設定、操作和擴展關聯式資料庫 AWS 雲端。
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 是一種物件儲存服務，可提供可擴展性、資料可用性、安全性和效能。
+ [AWS Database Migration Service (AWS DMS)](https://docs.aws.amazon.com/dms/latest/userguide/Welcome.html) 可協助您將資料存放區遷移到 AWS 雲端 或在雲端和內部部署設定的組合之間遷移。

**其他服務**
+ [Terraform](https://www.terraform.io/) 是 HashiCorp 的 IaC 工具，可協助您建立和管理雲端和內部部署資源。

**程式碼儲存庫**

此模式的程式碼可在 GitHub [Database Migration DevOps Framework 中使用 Terraform 範例](https://github.com/aws-samples/aws-terraform-db-migration-framework-samples)儲存庫。

## 最佳實務
<a name="set-up-ci-cd-pipeline-for-db-migration-with-terraform-best-practices"></a>
+ 實作資料庫遷移的自動化測試，以驗證結構描述變更和資料完整性的正確性。這包括單元測試、整合測試和end-to-end測試。
+ 為您的資料庫實作強大的備份和還原策略，特別是在遷移之前。這可確保資料完整性，並在發生故障時提供備用選項。
+ 實作強大的復原策略，以在遷移期間發生故障或問題時還原資料庫變更。這可能包括轉返到先前的資料庫狀態或還原個別遷移指令碼。
+ 設定監控和記錄機制，以追蹤資料庫遷移的進度和狀態。這可協助您快速識別和解決問題。

## 史詩
<a name="set-up-ci-cd-pipeline-for-db-migration-with-terraform-epics"></a>

### 設定您的本機工作站
<a name="set-up-your-local-workstation"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在本機工作站上設定 Git。 | 遵循 Git 文件中的指示，[在本機工作站上安裝和設定 Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)。 | DevOps 工程師 | 
| 建立專案資料夾，並從 GitHub 儲存庫新增檔案。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-ci-cd-pipeline-for-db-migration-with-terraform.html) | DevOps 工程師 | 

### 佈建目標架構
<a name="provision-the-target-architecture"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 更新必要的參數。 | `ssm-parameters.sh` 檔案會存放所有必要 AWS Systems Manager 的參數。您可以使用專案的自訂值來設定這些參數。在本機工作站的 `setup/db-ssm-params` 資料夾中，開啟 `ssm-parameters.sh` 檔案並設定這些參數，再執行 CI/CD 管道。 | DevOps 工程師 | 
| 初始化 Terraform 組態。 | 在 `db-cicd-integration`資料夾中，輸入下列命令來初始化包含 Terraform 組態檔案的工作目錄：<pre>terraform init</pre> | DevOps 工程師 | 
| 預覽 Terraform 計劃。 | 若要建立 Terraform 計劃，請輸入下列命令：<pre>terraform plan -var-file="terraform.sample"  </pre>Terraform 會評估組態檔案，以判斷宣告資源的目標狀態。然後，它會比較目標狀態與目前狀態，並建立計劃。 | DevOps 工程師 | 
| 驗證計劃。 | 檢閱計劃並確認其已在您的目標中設定所需的架構 AWS 帳戶。 | DevOps 工程師 | 
| 部署解決方案。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-ci-cd-pipeline-for-db-migration-with-terraform.html) | DevOps 工程師 | 

### 驗證部署
<a name="verify-the-deployment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 驗證部署。 | 驗證`db-cicd-integration`管道的狀態，以確認資料庫遷移已完成。1. 登入 AWS 管理主控台，然後開啟 [AWS CodePipeline 主控台](https://console.aws.amazon.com/codesuite/codepipeline/home)。2. 在導覽窗格中，選擇**管道**。3. 選擇`db-cicd-integration`管道。4. 驗證管道執行是否成功完成。 | DevOps 工程師 | 

### 使用後清除基礎設施
<a name="clean-up-infrastructure-after-use"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 清除基礎設施。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-ci-cd-pipeline-for-db-migration-with-terraform.html) | DevOps 工程師 | 

## 相關資源
<a name="set-up-ci-cd-pipeline-for-db-migration-with-terraform-resources"></a>

**AWS 文件**
+ [Terraform 產品入門](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/getstarted-Terraform.html)

**Terraform 文件**
+ [Terraform 安裝](https://learn.hashicorp.com/tutorials/terraform/install-cli)
+ [Terraform 後端組態](https://developer.hashicorp.com/terraform/language/backend)
+ [Terraform AWS 提供者文件](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)