

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

# 使用 AWS 在亚马逊 EC2 上自动设置 UiPath RPA 机器人 CloudFormation
<a name="set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation"></a>

*Rahul Sharad Gaikwad 博士和 Tamilselvan P，Amazon Web Services*

## Summary
<a name="set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation-summary"></a>

此模式说明了如何在亚马逊弹性计算云 (Amazon) 实例上部署机器人流程自动化 (RPA EC2) 机器人。它使用 I [EC2 mage Builder](https://docs.aws.amazon.com/imagebuilder/latest/userguide/what-is-image-builder.html) 管道来创建自定义的亚马逊系统映像 (AMI)。AMI 是一种预配置的虚拟机 (VM) 映像，其中包含用于部署 EC2 实例的操作系统 (OS) 和预安装的软件。此模式使用 AWS CloudFormation 模板在自定义 AMI 上安装 [UiPath Studio 社区版](https://www.uipath.com/product/studio)。 UiPath 是一款 RPA 工具，可帮助您设置机器人来自动执行任务。

作为该解决方案的一部分，使用基本 AMI 启动 EC2 Windows 实例，并在实例上安装 UiPath Studio 应用程序。该模式使用 Microsoft 系统准备（Sysprep）工具复制自定义的 Windows 安装。之后，它删除主机信息并从实例创建最终 AMI。然后，您可以使用最终的 AMI 以及您自己的命名约定和监控设置来按需启动实例。


| 
| 
| 注意：此模式不提供有关使用 RPA 机器人的任何信息。有关该信息，请参阅[UiPath 文档](https://docs.uipath.com/)。您也可使用此模式根据您的要求自定义安装步骤来设置其他 RPA 机器人应用程序。 | 
| --- |

此模式提供以下自动化和优点：
+ 应用程序部署和共享：您可以构建 Amazon EC2 AMIs 以进行应用程序部署，并通过 I EC2 mage Builder 管道在多个账户之间共享它们，该管道使用 AWS CloudFormation 模板作为基础设施即代码 (IaC) 脚本。
+ Amazon EC2 配置和扩展： CloudFormation IaC 模板提供自定义计算机名称序列和 Active Directory 自动加入功能。
+ 可观察性和监控：该模式设置了亚马逊 CloudWatch 控制面板，以帮助您监控亚马逊 EC2 指标（例如 CPU 和磁盘使用率）。
+ RPA 为您的企业带来的好处：RPA 提高了准确性，因为机器人可以自动、一致地执行分配的任务。RPA 还可以提高速度和生产力，因为它消除了不增加价值的操作并处理重复的活动。

## 先决条件和限制
<a name="set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation-prereqs"></a>

**先决条件**
+ 一个有效的 [Amazon Web Services account](https://aws.amazon.com/free/)
+ 用于部署 CloudFormation 模板@@ [的 AWS Identity and Access Management (IAM) 权限](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-iam-template.html)
+ 用于@@ [使用 Im EC2 age Builder 设置跨账户 AMI 分发的 IAM 策略](https://docs.aws.amazon.com/imagebuilder/latest/userguide/cross-account-dist.html)

## 架构
<a name="set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation-architecture"></a>

![\[用于在亚马逊上设置 RPA 机器人的目标架构 EC2\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/5555a62d-91d4-4e81-9961-ff89faedd6ad/images/1893d2d3-8912-4473-adf1-6633b5badcd9.png)


1. 管理员在`ec2-image-builder.yaml`文件中提供基本 Windows AMI，并在 CloudFormation 控制台中部署堆栈。

1.  CloudFormation 堆栈部署 EC2 Image Builder 管道，其中包括以下资源：
   + `Ec2ImageInfraConfiguration`
   + `Ec2ImageComponent`
   + `Ec2ImageRecipe`
   + `Ec2AMI`

1.  EC2 Image Builder 管道使用基本 AMI 启动临时 Windows EC2 实例并安装所需的组件（在本例中为 UiPath Studio）。

1.  EC2 Image Builder 会删除所有主机信息并从 Windows 服务器中创建 AMI。

1. 您可以使用自定义 AMI 更新`ec2-provisioning yaml`文件并根据您的要求启动多个 EC2 实例。

1. 您可以使用 CloudFormation 模板部署 Count 宏。此宏为 CloudFormation 资源提供了 C **ount** 属性，因此您可以轻松地指定多个相同类型的资源。

1. 更新 CloudFormation `ec2-provisioning.yaml`文件中宏的名称并部署堆栈。

1. 管理员根据要求更新 `ec2-provisioning.yaml` 文件并启动堆栈。

1. 该模板使用 UiPath Studio 应用程序部署 EC2 实例。

## 工具
<a name="set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation-tools"></a>

**AWS 服务**
+ [AWS CloudFormation](https://aws.amazon.com/cloudformation/) 可帮助您以自动且安全的方式对基础设施资源进行建模和管理。
+ [Amazon CloudWatch](https://aws.amazon.com/cloudwatch/) 可帮助您观察和监控 AWS、本地和其他云上的资源和应用程序。
+ [亚马逊弹性计算云 (Amazon EC2](https://aws.amazon.com/ec2/)) 在 AWS 云中提供安全且可调整大小的计算容量。您可以根据需要启动任意数量的虚拟服务器，并快速纵向扩展或缩减这些服务器。
+ [EC2 Image Builder](https://aws.amazon.com/image-builder/) 简化了在 AWS 上或本地使用的虚拟机和容器镜像的构建、测试和部署。
+ [Amazon EventBridge](https://aws.amazon.com/eventbridge/) 可帮助您在 AWS、现有系统或软件即服务 (SaaS) 应用程序中大规模构建事件驱动型应用程序。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 可帮助您安全地控制对您 AWS 资源的访问。借助 IAM，您可以集中管理控制用户可访问哪些 AWS 资源的权限。可以使用 IAM 来控制谁通过了身份验证(准许登录)并获得授权(拥有权限)来使用资源。
+ [AWS Lambda](https://aws.amazon.com/lambda/) 是一项无服务器、事件驱动计算服务，让您能够为几乎任何类型的应用程序或后端服务运行代码，而无需预调配或管理服务器。您可以从 200 多种 Amazon Web Services 和 SaaS 应用程序中调用 Lambda 函数，并且只需按实际使用量付费。
+ [Amazon Simple Storage Service (Amazon S3)](https://aws.amazon.com/s3/) 是一项基于云的对象存储服务，可帮助您存储、保护和检索任意数量的数据。
+ [AWS Systems Manager 代理 (SSM 代理)](https://docs.aws.amazon.com/systems-manager/latest/userguide/ssm-agent.html) 可帮助 Systems Manager 更新、管理和配置 EC2 实例、边缘设备、本地服务器和虚拟机 (VMs)。

**代码存储库**

此模式的代码可在[使用 CloudFormation存储库的 GitHub UiPath RPA 机器人设置](https://github.com/aws-samples/uipath-rpa-setup-ec2-windows-ami-cloudformation)中找到。该模式还使用 A [WS Mac CloudFormation ros 存储库中提供的宏。](https://github.com/aws-cloudformation/aws-cloudformation-macros/tree/master/Count)

## 最佳实践
<a name="set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation-best-practices"></a>
+ AWS 每月 AMIs都会发布新的 [Windows](https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/windows-ami-version-history.html)。其中包含最新的操作系统补丁、驱动程序和启动代理。您应该在启动新实例或构建自己的自定义镜像时利用最新的 AMI。
+ 在镜像生成期间应用所有可用的 Windows 或 Linux 安全补丁。

## 操作说明
<a name="set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation-epics"></a>

### 为基础镜像部署镜像管道
<a name="deploy-an-image-pipeline-for-the-base-image"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 设置 EC2 Image Builder 管道。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html) | AWS DevOps | 
| 查看 EC2 Image Builder 设置。 |  EC2 Image Builder 设置包括基础架构配置、分发设置和安全扫描设置。若要查看设置，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html)最佳做法是，应仅通过 CloudFormation 模板对 EC2 Image Builder 进行任何更新。 | AWS DevOps | 
| 查看镜像管道。 | 要查看已部署的镜像管道，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html) | AWS DevOps | 
| 查看 Image Builder 日志。 | EC2 Image Builder 日志按 CloudWatch 日志组进行聚合。要查看日志，请执行 CloudWatch以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html)EC2 Image Builder 日志也存储在 S3 存储桶中。若要查看存储桶中的日志，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html) | AWS DevOps | 
| 将 UiPath 文件上传到 S3 存储桶。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html) | AWS DevOps | 

### 部署并测试 Count 宏
<a name="deploy-and-test-the-count-macro"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 部署计数宏。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html)如果要使用控制台，请按照上一篇长篇故事或[CloudFormation 文档](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-create-stack.html)中的说明进行操作。  | DevOps 工程师 | 
| 测试计数宏。 | 若要测试宏的功能，请尝试启动宏附带的示例模板。 <pre>aws cloudformation deploy \<br />    --stack-name Count-test \<br />    --template-file test.yaml \<br />    --capabilities CAPABILITY_IAM</pre> | DevOps 工程师 | 

### 部署 CloudFormation 堆栈以使用自定义映像配置实例
<a name="deploy-the-cloudformation-stack-to-provision-instances-with-the-custom-image"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 部署 Amazon EC2 配置模板。 | 要部署 EC2 映像管道，请使用 CloudFormation以下方法：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html) | AWS DevOps | 
| 查看 Amazon EC2 设置。 | Amazon EC2 设置包括安全、联网、存储、状态检查、监控和标签配置。若要查看这些配置，请执行以下操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html) | AWS DevOps | 
| 查看 CloudWatch 控制面板。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html)预调配堆栈后，需要一段时间才能在控制面板中填充指标。控制面板提供以下指标：`CPUUtilization`、`DiskUtilization`、`MemoryUtilization`、`NetworkIn`、`NetworkOut`、`StatusCheckFailed`。 | AWS DevOps | 
| 查看关于内存和磁盘使用情况的自定义指标。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html) | AWS DevOps | 
| 查看内存与磁盘使用情况警报。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html) | AWS DevOps | 
| 验证快照生命周期规则。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html) | AWS DevOps | 

### 删除环境（可选）
<a name="delete-the-environment-optional"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 删除堆栈。 | PoC 或试点项目完成后，我们建议您删除创建的堆栈，以确保您无需为这些资源付费。[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html)堆栈删除操作开始后就无法停止。堆栈进入 `DELETE_IN_PROGRESS`状态。如果删除失败，则堆栈将处于 `DELETE_FAILED` 状态。有关解决方案，请参阅 AWS CloudFormation 疑难解答文档中的[删除堆栈失败](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/troubleshooting.html#troubleshooting-errors-delete-stack-fails)。有关保护堆栈不被意外删除的信息，请参阅 AWS CloudFormation 文档中的[保护堆栈不被删除](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-protect-stacks.html)。 | AWS DevOps | 

## 问题排查
<a name="set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
| 当你部署 Amazon EC2 配置模板时，你会收到错误消息：*收到来自转换 123xxxx:: Count 的格式错误的响应*。 | 这是一个已知问题。（参见 [AW CloudFormation S 宏存储库](https://github.com/aws-cloudformation/aws-cloudformation-macros/pull/20)中的自定义解决方案和 PR。）[要修复此问题，请打开 AWS Lambda 控制台并`index.py`使用存储库中的GitHub 内容进行更新。](https://raw.githubusercontent.com/aws-cloudformation/aws-cloudformation-macros/f1629c96477dcd87278814d4063c37877602c0c8/Count/src/index.py)  | 

## 相关资源
<a name="set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation-resources"></a>

**GitHub 存储库**
+ [UiPath 使用 RPA 机器人设置 CloudFormation](https://github.com/aws-samples/uipath-rpa-setup-ec2-windows-ami-cloudformation)
+ [计数 CloudFormation 宏](https://github.com/aws-cloudformation/aws-cloudformation-macros/tree/master/Count)

**AWS 参考**
+ 在 [AWS CloudFormation 控制台上创建堆栈](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-create-stack.html)（CloudFormation 文档）
+ [故障排除 CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/troubleshooting.html)（CloudFormation 文档）
+ [监控亚马逊 EC2 实例的内存和磁盘指标](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/mon-scripts.html)（亚马逊 EC2 文档）
+ [如何使用 CloudWatch 代理在 Windows 服务器上查看 “性能监视器” 的指标？](https://repost.aws/knowledge-center/cloudwatch-performance-monitor-windows) （AWS re: Post 文章）

**其他参考资料**
+ [UiPath 文档](https://docs.uipath.com/)
+ 在 [ SysPreped AMI 中设置主机名](https://blog.brianbeach.com/2014/07/setting-hostname-in-syspreped-ami.html)（Brian Beach 的博客文章）
+ [当参数发生变化时，如何让 Cloudformation 通过宏重新处理模板？](https://stackoverflow.com/questions/59828989/how-do-i-make-cloudformation-reprocess-a-template-using-a-macro-when-parameters) 堆栈溢出