

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

# 使用 AWS Systems Manager 維護 Windows 自動停止和啟動 Amazon RDS 資料庫執行個體
<a name="automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows"></a>

*Ashita Dsilva，Amazon Web Services*

## 總結
<a name="automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows-summary"></a>

此模式示範如何使用 AWS Systems Manager 維護時段，依特定排程自動停止和啟動 Amazon Relational Database Service (Amazon RDS) 資料庫執行個體 （例如，在營業時間外關閉資料庫執行個體以降低成本）。為此，Systems Manager 對於典型的使用案例具有成本效益。

AWS Systems Manager 自動化提供 `AWS-StopRdsInstance`和 `AWS-StartRdsInstance` Runbook 來停止和啟動 Amazon RDS 資料庫執行個體。這表示您不需要使用 AWS Lambda 函數撰寫自訂邏輯或建立 Amazon CloudWatch Events 規則。

Systems Manager 提供兩種排程任務的功能：[State Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-state-about.html) [和維護 Windows](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-maintenance.html)。State Manager 會為您的 Amazon Web Services (AWS) 帳戶中的資源設定和維護所需的狀態組態一次，或依特定排程進行。維護 Windows 會在特定時段內對您帳戶中的資源執行任務。雖然您可以在狀態管理員或維護時段使用此模式的方法，但我們建議您使用維護時段，因為它可以根據指派的優先順序執行一或多個任務，也可以執行 AWS Lambda 函數和 AWS Step Functions 任務。如需 State Manager 和維護 Windows 的詳細資訊，請參閱 Systems Manager 文件中的[在 State Manager 和維護 Windows 之間進行選擇](https://docs.aws.amazon.com/systems-manager/latest/userguide/state-manager-vs-maintenance-windows.html)。

此模式提供詳細步驟來設定兩個單獨的維護時段，這些時段使用 cron 表達式來停止，然後啟動 Amazon RDS 資料庫執行個體。 

## 先決條件和限制
<a name="automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows-prereqs"></a>

**先決條件**
+ 作用中 AWS 帳戶。
+ 您要在特定排程中停止和啟動的現有 Amazon RDS 資料庫執行個體。
+ 所需排程的 Cron 表達式。例如，表達式會在每個星期一、星期二、星期三、星期四和星期五的 09：00 `cron(0 9 ? * MON-FRI *)`執行任務。如需詳細資訊，請參閱 Systems Manager 文件中的[維護時段的 Cron 和 Rate 表達](https://docs.aws.amazon.com/systems-manager/latest/userguide/reference-cron-and-rate-expressions.html#reference-cron-and-rate-expressions-maintenance-window)式。
+ 熟悉 Systems Manager。
+ 啟動和停止 RDS 執行個體的許可。如需詳細資訊，請參閱 [Epics](#automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows-epics) 區段。

**限制**
+ Amazon RDS 資料庫執行個體一次最多可停止七天。七天後，資料庫執行個體會自動重新啟動，以確保收到任何必要的維護更新。
+ 您無法停止僅供讀取複本或具有僅供讀取複本的資料庫執行個體。
+ 您無法在多可用區組態中停止 Amazon RDS for SQL Server 資料庫執行個體。
+ 服務配額適用於維護 Windows 和 Systems Manager 自動化。如需服務配額的詳細資訊，請參閱 AWS 一般參考 文件中的[AWS Systems Manager 端點和配額](https://docs.aws.amazon.com/general/latest/gr/ssm.html)。 
+ 有些 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)頁面，然後選擇服務的連結。

## Architecture
<a name="automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows-architecture"></a>

下圖顯示自動停止和啟動 Amazon RDS 資料庫執行個體的工作流程。

![\[自動停止和啟動 Amazon RDS 資料庫執行個體的工作流程\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/45b81621-5674-4bcf-bf7c-75ae6f62524e/images/7d943830-716e-46a3-be44-7e668c3c01ff.png)


 

工作流程有下列步驟：

1. 建立維護時段並使用 cron 表達式來定義 Amazon RDS 資料庫執行個體的停止和啟動排程。

2. 使用 `AWS-StopRdsInstance`或 `AWS-StartRdsInstance` Runbook 將 Systems Manager Automation 任務註冊到維護時段。

3. 為您的 Amazon RDS 資料庫執行個體使用標籤型資源群組，向維護時段註冊目標。

**技術堆疊**
+ AWS CloudFormation
+ AWS Identity and Access Management (IAM)
+ Amazon RDS
+ Systems Manager

**自動化和擴展**

您可以同時停止和啟動多個 Amazon RDS 資料庫執行個體，方法是標記所需的 Amazon RDS 資料庫執行個體、建立包含所有已標記資料庫執行個體的資源群組，以及將此資源群組註冊為維護時段的目標。

## 工具
<a name="automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows-tools"></a>
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) 是一項服務，可協助您建立和設定 AWS 資源的模型。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 是一種 Web 服務，可協助您安全地控制對 AWS 資源的存取。
+ [Amazon Relational Database Service (Amazon RDS)](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Welcome.html) 是一種 Web 服務，可讓您更輕鬆地在 中設定、操作和擴展關聯式資料庫 AWS 雲端。
+ [AWS Resource Groups](https://docs.aws.amazon.com/ARG/latest/userguide/welcome.html) 可協助您將 AWS 資源組織成群組、標記資源，以及管理、監控和自動化分組資源上的任務。
+ [AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html) 是 AWS 服務 ，可用來檢視和控制您的基礎設施 AWS。此模式使用 Systems Manager 的下列功能：
  + [AWS Systems Manager 自動化](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-automation.html)可簡化 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體和其他 AWS 資源的常見維護和部署任務。
  + [AWS Systems Manager 維護 Windows](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-maintenance.html) 可協助您定義何時對執行個體執行潛在破壞性動作的排程。

## 史詩
<a name="automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows-epics"></a>

### 建立和設定 Systems Manager Automation 的 IAM 服務角色
<a name="create-and-configure-the-iam-service-role-for-sys-automation"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定 Systems Manager Automation 的 IAM 服務角色。 | 登入 AWS 管理主控台 並建立 Systems Manager Automation 的服務角色。您可以使用下列兩種方法之一來建立此服務角色：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows.html)Systems Manager Automation 工作流程會使用服務角色在 Amazon RDS 資料庫執行個體上執行啟動和停止動作，以叫用 Amazon RDS。必須使用下列[內嵌政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html#add-policies-console)來設定服務角色，該政策具有啟動和停止 Amazon RDS 資料庫執行個體的許可：<pre>{<br />    "Version": "2012-10-17",		 	 	 <br />    "Statement": [<br />        {<br />            "Sid": "RdsStartStop",<br />            "Effect": "Allow",<br />            "Action": [<br />                "rds:StopDBInstance",<br />                "rds:StartDBInstance"<br />            ],<br />            "Resource": "<RDS_Instance_ARN>"               <br />        },<br />        {<br />            "Sid": "RdsDescribe",<br />            "Effect": "Allow",<br />            "Action": "rds:DescribeDBInstances",<br />            "Resource": "*"<br />        }<br />    ]<br />}</pre>請務必將 `<RDS_Instance_ARN>`取代為 Amazon RDS 資料庫執行個體的 Amazon Resource Name (ARN)。如果您不熟悉使用 IAM 政策和角色，請遵循[排程 Amazon RDS 停止和開始使用 AWS Systems Manager](https://aws.amazon.com/blogs/database/schedule-amazon-rds-stop-and-start-using-aws-systems-manager/)部落格文章的解決方案*概觀*一節中的指示。請務必記錄服務角色的 ARN。 | AWS 管理員 | 

### 建立資源群組
<a name="create-a-resource-group"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 標記 Amazon RDS 資料庫執行個體。 | 開啟 [Amazon RDS 主控台](https://console.aws.amazon.com/rds/)，並標記您要新增至資源群組的 Amazon RDS 資料庫執行個體。標籤是指派給 AWS 資源的中繼資料，由索引鍵/值對組成。我們建議您使用*動作*做為**標籤索引鍵**，並使用 *StartStop* 做為**值**。如需詳細資訊，請參閱 Amazon RDS 文件中的[新增、列出和移除標籤](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_Tagging.html#Tagging.HowTo)。 | AWS 管理員 | 
| 為您的標記 Amazon RDS 資料庫執行個體建立資源群組。 | 開啟 [AWS Resource Groups 主控台](https://console.aws.amazon.com/resource-groups)，並根據您為 Amazon RDS 資料庫執行個體建立的標籤建立資源群組。在**分組條件**下，請確定您為資源類型選擇 **AWS::RDS::DBInstance**，然後提供標籤的鍵值對 （例如，「Action-StartStop」)。這可確保服務只會檢查 Amazon RDS 資料庫執行個體，而不是具有此標籤的其他資源。****請確定您記錄資源群組的名稱。如需詳細資訊和詳細步驟，請參閱 AWS Resource Groups 文件中的[建置標籤型查詢和建立群組](https://docs.aws.amazon.com/ARG/latest/userguide/gettingstarted-query.html#gettingstarted-query-tag-based)。  | AWS 管理員 | 

### 設定維護時段以停止 Amazon RDS 資料庫執行個體
<a name="configure-a-maintenance-window-to-stop-the-rds-db-instances"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立維護時段。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows.html)停止資料庫執行個體的任務會在啟動時幾乎立即執行，而且不會跨越維護時段的整個持續時間。此模式提供**持續時間**和**停止啟動任務**的最小值，因為它們是維護時段的必要參數。如需詳細資訊和詳細步驟，請參閱 Systems Manager 文件中的[建立維護時段 （主控台）](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-maintenance-create-mw.html)。 | AWS 管理員 | 
| 將目標指派給維護時段。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows.html)如需詳細資訊和詳細步驟，請參閱 Systems Manager 文件中的[將目標指派給維護時段 （主控台）](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-maintenance-assign-targets.html)。 | AWS 管理員 | 
| 將任務指派給維護時段。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows.html)******服務角色**選項定義維護時段執行任務所需的服務角色。不過，此角色與您先前為 Systems Manager Automation 建立的服務角色不同。如需詳細資訊和詳細步驟，請參閱 Systems Manager 文件中的將[任務指派給維護時段 （主控台）](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-maintenance-assign-tasks.html)。 | AWS 管理員 | 

### 設定維護時段以啟動 Amazon RDS 資料庫執行個體
<a name="configure-a-maintenance-window-to-start-the-rds-db-instances"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定維護時段以啟動 Amazon RDS 資料庫執行個體。 | 重複*設定維護時段中的步驟，停止 Amazon RDS 資料庫執行個體* epic，以設定另一個維護時段，在排程時間啟動 Amazon RDS 資料庫執行個體。當您設定維護時段以啟動資料庫執行個體時，必須進行下列變更：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows.html) | AWS 管理員 | 

## 相關資源
<a name="automatically-stop-and-start-an-amazon-rds-db-instance-using-aws-systems-manager-maintenance-windows-resources"></a>
+ [使用 Systems Manager Automation 文件來管理執行個體並降低非上班時間的成本 ](https://aws.amazon.com/blogs/mt/systems-manager-automation-documents-manage-instances-cut-costs-off-hours/)(AWS 部落格文章）