

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

# 使用 AWS Lambda 任务计划程序在亚马逊 EC2 上运行的 SQL Server Express 版中自动执行数据库任务
<a name="automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2"></a>

*Subhani Shaik，Amazon Web Services*

## Summary
<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 代理服务。此模式说明了在亚马逊弹性计算云 (Amazon) 实例上运行的 SQL Server Express 版中，如何使用任务计划程序和 Lambda 作为自动执行数据库任务的替代方案。 EC2

[任务计划程序](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 服务器的亚马逊 EC2 实例
+ 通过 Windows Server 连接到亚马逊实例的亚马逊 Elastic Block Store (A EC2 mazon EBS) 卷
+ [SQL Server Express Edition](https://www.microsoft.com/en-us/download/details.aspx?id=101064) 二进制文件

**限制**
+ 有关 SQL Server Express 版功能限制的信息，请访问 [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 Express 版的 SQL Server 2016 或更高版本

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

下图显示了安装了 SQL Server Express 版的情况下运行的亚马逊 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_cn/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](https://docs.aws.amazon.com/ebs/latest/userguide/what-is-ebs.html) EBS) 提供用于亚马逊实例的块级存储卷。 EC2 
+ [亚马逊弹性计算云 (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 工具。使用会话管理器，您可以管理您的 Amazon EC2 实例、边缘设备、本地服务器和虚拟机 (VMs)。
+ [Amazon Virtual Private Cloud（亚马逊 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)
+ [在亚马逊上部署微软 SQL Server 的最佳实践 EC2](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>

### 创建亚马逊 EC2 实例并安装 SQL Server Express 版
<a name="create-an-amazon-ec2-instance-and-install-sql-server-express-edition"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 部署 Amazon EC2 实例。 | 要创建亚马逊 EC2 实例，请打开亚马逊 EC2 控制台，网址为，[https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)然后从可用于 Windows 服务器的实例列表中选择一个[亚马逊系统映像 (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_cn/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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 确定例行任务。 | 确定要自动执行的例行任务。例如，下列任务符合自动化的条件：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2.html) | 数据库管理员 | 
| 准备 SQL 脚本。 | 要准备 SQL 脚本，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2.html) | 数据库管理员 | 
| 配置访问权限。 | 要配置访问权限，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2.html) | 数据库管理员 | 

### 通过任务计划程序自动执行任务
<a name="automate-tasks-with-task-scheduler"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 创建批处理文件。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/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_cn/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_cn/prescriptive-guidance/latest/patterns/automate-database-tasks-in-sql-server-express-edition-running-on-amazon-ec2.html)要手动运行任务，请右键单击新创建的任务，然后选择**运行**。 | 数据库管理员 | 
| 查看任务状态。 | 要在任务计划程序中查看任务的状态，请使用以下步骤：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/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>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 实施解决方案。 | 要实施此模式的解决方案，请采用以下步骤：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/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，请参阅文档[中的 Lambda 问题疑难解答](https://docs.aws.amazon.com/lambda/latest/dg/lambda-troubleshooting.html)。 AWS  | 

## 相关资源
<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 网站）