

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

# 使用 AWS Lambda 和 任務排程器，在 Amazon EC2 上執行的 SQL Server Express 版本中自動化資料庫任務
<a name="automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2"></a>

*Subhani Shaik，Amazon Web Services*

## 總結
<a name="automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2-summary"></a>

此模式示範如何在 SQL Server Express 版本中排程和管理資料庫任務，這是 SQL Server 的免費版本。不過，SQL Server Express 版本缺少通常處理自動化資料庫操作的 SQL Server Agent 服務。此模式說明如何使用任務排程器和 Lambda 做為在 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體上執行的 SQL Server Express 版本中自動化資料庫任務的替代方案。

[任務排程器](https://learn.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-start-page)是內建的 Windows 系統公用程式，可促進例行任務的自動執行。它提供一種機制來排程和管理自動化操作，無需在週期性程序中手動介入。 [AWS Lambda](https://aws.amazon.com/lambda/) 是一種無伺服器運算服務，可自動執行程式碼以回應事件，而無需您管理基礎基礎設施。

## 先決條件和限制
<a name="automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2-prereqs"></a>

**先決條件**
+ 作用中 AWS 帳戶
+ 使用 Amazon Virtual Private Cloud (Amazon VPC) 建立的虛擬私有雲端 (VPC)
+ 具有 Windows Server 的 Amazon EC2 執行個體
+ 使用 Windows Server 連接至 Amazon EC2 執行個體的 Amazon Elastic Block Store (Amazon EBS) 磁碟區
+ [SQL Server Express Edition](https://www.microsoft.com/en-us/download/details.aspx?id=101064) 二進位檔

**限制**
+ 如需 SQL Server Express Edition 功能限制的相關資訊，請參閱 [Microsoft 網站](https://learn.microsoft.com/en-us/sql/sql-server/editions-and-components-of-sql-server-2019?view=sql-server-ver16)。
+ 有些 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)，然後選擇服務的連結。

**產品版本**
+ SQL Server 2016 或更新版本搭配 SQL Server Express 版本

## Architecture
<a name="automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2-architecture"></a>

下圖顯示已安裝 SQL Server Express 版本的 Amazon EC2 執行個體。執行個體可透過遠端桌面通訊協定 (RDP) 用戶端或從 存取 AWS Systems Manager Session Manager。 AWS Key Management Service (AWS KMS) 會處理 Amazon EBS 磁碟區的資料加密，以確保data-at-rest的安全性。基礎設施也包含 AWS Identity and Access Management (IAM)，可提供存取控制和管理執行 Lambda 函數的許可。Amazon Simple Storage Service (Amazon S3) 存放 Lambda 函數。

![\[在私有子網路上安裝 SQL Server Express 版本時執行的 Amazon EC2 執行個體。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/3af2174d-bf49-4e43-86f7-34759e5eea84/images/3a37dcb8-10af-42f2-8ff1-fab4f87eb646.png)


## 工具
<a name="automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2-tools"></a>

**AWS 服務**
+ [Amazon Elastic Block Store (Amazon EBS)](https://docs.aws.amazon.com/ebs/latest/userguide/what-is-ebs.html) 提供區塊層級儲存體磁碟區，可搭配使用 Amazon EC2 執行個體。
+ [Amazon Elastic Compute Cloud (Amazon EC2)](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html) 在 AWS 雲端中提供可擴展的運算容量。您可以視需要啟動任意數量的虛擬伺服器，，並快速進行擴展或縮減。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 透過控制已驗證並獲授權使用的人員，協助您安全地管理對 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) 是一項運算服務，可協助您執行程式碼，無需佈建或管理伺服器。它只會在需要時執行程式碼並自動擴展，因此您只需按使用的運算時間付費。
+ [Amazon Simple Storage Service (Amazon S3)](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 是一種雲端型物件儲存服務，可協助您儲存、保護和擷取任何數量的資料。
+ [AWS Systems Manager Session Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager.html) 是全受管 AWS Systems Manager 工具。使用 Session Manager，您可以管理 Amazon EC2 執行個體、邊緣裝置、內部部署伺服器和虛擬機器 VMs)。
+ [Amazon Virtual Private Cloud (Amazon VPC)](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) 可協助您在已定義的虛擬網路中啟動 AWS 資源。此虛擬網路與您在自己的資料中心中操作的傳統網路相似，且具備使用 AWS可擴展基礎設施的優勢。

**其他工具**
+ [Microsoft SQL Server Management Studio (SSMS)](https://learn.microsoft.com/en-us/ssms/download-sql-server-management-studio-ssms) 是一種用於管理 SQL Server 的工具，包括存取、設定和管理 SQL Server 元件。
+ [Python](https://www.python.org/) 是一種一般用途的電腦程式設計語言。您可以使用它來建置應用程式、自動化任務，以及在 上開發服務[AWS 雲端](https://aws.amazon.com/developer/language/python/)。
+ [任務排程器](https://learn.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-start-page)是一種 Microsoft 工具，可用來自動排程電腦上的例行任務。

## 最佳實務
<a name="automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2-best-practices"></a>
+ [Amazon EC2 的最佳實務](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-best-practices.html)
+ [在 Amazon EC2 上部署 Microsoft SQL Server 的最佳實務](https://docs.aws.amazon.com/prescriptive-guidance/latest/sql-server-ec2-best-practices/welcome.html)
+ [使用 AWS Lambda 函數的最佳實務](https://docs.aws.amazon.com/lambda/latest/dg/best-practices.html)
+ [IAM 中的安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)

## 史詩
<a name="automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2-epics"></a>

### 建立 Amazon EC2 執行個體並安裝 SQL Server Express 版本
<a name="create-an-amazon-ec2-instance-and-install-sql-server-express-edition"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署 Amazon EC2 執行個體。 | 若要建立 Amazon EC2 執行個體，請開啟位於 [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)：// 的 Amazon EC2 主控台，然後從適用於 Windows Server 的執行個體清單中選取 [Amazon Machine Image (AMI)](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html)。如需詳細資訊，請參閱 AWS 文件中的[啟動 Amazon EC2 執行個體](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/LaunchingAndUsingInstances.html)。 | DBA、AWS DevOps | 
| 安裝 SQL Server Express 版本。 | 若要安裝 SQL Server Express 版本，請完成下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2.html) | DBA、AWS DevOps | 

### 建立自動化資料庫維護任務
<a name="create-automated-database-maintenance-tasks"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 識別例行任務。 | 識別您要自動化的例行任務。例如，下列任務符合自動化的資格：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2.html) | DBA | 
| 準備 SQL 指令碼。 | 若要準備 SQL 指令碼，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2.html) | DBA | 
| 設定存取許可。 | 若要設定存取許可，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2.html) | DBA | 

### 使用任務排程器自動化任務
<a name="automate-tasks-with-task-scheduler"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立批次檔案。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2.html)<pre>sqlcmd -S servername -U username -P password -i <T-SQL query path.sql></pre>[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2.html)<pre><br />@echo off<br />sqlcmd -S [ServerName] -d [DatabaseName] -U username -P password -i "PathToSQLScript\Script.sql" -o "PathToOutput\Output.txt"</pre> | AWS DevOps、DBA | 
| 在任務排程器中建立任務。 | 若要在任務排程器中建立任務，請使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2.html)若要手動執行任務，請在新建立的任務上按一下滑鼠右鍵，然後選取**執行**。 | DBA | 
| 檢視任務狀態。 | 若要在任務排程器中檢視任務的狀態，請使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2.html) | DBA、AWS DevOps | 

### 使用 自動化任務 AWS Lambda
<a name="automate-tasks-with-lamlong"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 實作解決方案。 | 若要實作此模式的解決方案，請使用下列步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2.html) | AWS DevOps，DevOps 工程師 | 

## 疑難排解
<a name="automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| Lambda 問題 | 如需使用 時可能遇到的錯誤和問題的協助 AWS Lambda，請參閱 AWS 文件[中的 Lambda 中的故障診斷問題](https://docs.aws.amazon.com/lambda/latest/dg/lambda-troubleshooting.html)。 | 

## 相關資源
<a name="automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2-resources"></a>
+ [Amazon EC2 執行個體類型](https://aws.amazon.com/ec2/instance-types/)
+ [AWS Lambda 文件](https://docs.aws.amazon.com/lambda/latest/dg/with-eventbridge-scheduler.html)
+ [AWS Lambda 定價](https://aws.amazon.com/lambda/pricing/)
+ [開發人員的任務排程器](https://learn.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-start-page) (Microsoft 網站）