

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

# 使用 AWS CloudFormation 在 Amazon EC2 上自動設定 UiPath RPA 機器人
<a name="set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation"></a>

*Rahul Sharad Gaikwad 醫生和 Tamilselvan P，Amazon Web Services*

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

此模式說明如何在 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體上部署機器人程序自動化 (RPA) 機器人。它使用 [EC2 Image Builder](https://docs.aws.amazon.com/imagebuilder/latest/userguide/what-is-image-builder.html) 管道來建立自訂 Amazon Machine Image (AMI)。AMI 是預先設定的虛擬機器 (VM) 映像，其中包含作業系統 (OS) 和預先安裝的軟體來部署 EC2 執行個體。此模式使用 AWS CloudFormation 範本在自訂 AMI 上安裝 [UiPath Studio Community Edition](https://www.uipath.com/product/studio)。UiPath 是一種 RPA 工具，可協助您設定機器人來自動化任務。

作為此解決方案的一部分，EC2 Windows 執行個體是使用基本 AMI 啟動，且 UiPath Studio 應用程式安裝在執行個體上。模式使用 Microsoft System Preparation (Sysprep) 工具來複製自訂 Windows 安裝。之後，它會移除主機資訊，並從執行個體建立最終 AMI。然後，您可以使用最終 AMI 搭配您自己的命名慣例和監控設定，以隨需啟動執行個體。


| 
| 
| 注意：此模式不會提供有關使用 RPA 機器人的任何資訊。如需該資訊，請參閱 [UiPath 文件](https://docs.uipath.com/)。您也可以使用此模式，根據您的需求自訂安裝步驟來設定其他 RPA 機器人應用程式。 | 
| --- |

此模式提供下列自動化和優點：
+ 應用程式部署和共用：您可以建置應用程式部署的 Amazon EC2 AMIs，並透過 EC2 Image Builder 管道跨多個帳戶共用這些 AWS CloudFormation 範本做為基礎設施做為程式碼 (IaC) 指令碼。
+ Amazon EC2 佈建和擴展：CloudFormation IaC 範本提供自訂電腦名稱序列和 Active Directory 聯結自動化。
+ 可觀測性和監控： 模式會設定 Amazon CloudWatch 儀表板，以協助您監控 Amazon EC2 指標 （例如 CPU 和磁碟用量）。
+ 業務的 RPA 優勢：由於機器人可以自動且一致地執行指派的任務，因此 RPA 可提高準確性。RPA 也會提高速度和生產力，因為它會移除不增加價值並處理重複活動的操作。

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

**先決條件 **
+ 作用中的[ AWS 帳戶](https://aws.amazon.com/free/)
+ 部署 CloudFormation 範本的 [AWS Identity and Access Management (IAM) 許可](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-iam-template.html) 
+ 使用 EC2 Image Builder 設定跨帳戶 AMI 分佈的 [IAM 政策](https://docs.aws.amazon.com/imagebuilder/latest/userguide/cross-account-dist.html) 

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

![在 Amazon EC2 上設定 RPA 機器人的目標架構](http://docs.aws.amazon.com/zh_tw/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 Server 建立 AMI。

1. 您可以使用自訂 AMI 更新 `ec2-provisioning yaml` 檔案，並根據您的需求啟動多個 EC2 執行個體。

1. 您可以使用 CloudFormation 範本部署計數巨集。此巨集提供 CloudFormation 資源的 **Count** 屬性，因此您可以輕鬆指定相同類型的多個資源。

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 Elastic Compute Cloud (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 個 AWS 服務和 SaaS 應用程式呼叫 Lambda 函數，並且只需為您使用的項目付費。
+ [Amazon Simple Storage Service (Amazon S3) ](https://aws.amazon.com/s3/)是一種雲端型物件儲存服務，可協助您存放、保護和擷取任何數量的資料。
+ [AWS Systems Manager Agent (SSM Agent)](https://docs.aws.amazon.com/systems-manager/latest/userguide/ssm-agent.html) 可協助 Systems Manager 更新、管理和設定 EC2 執行個體、邊緣裝置、內部部署伺服器和虛擬機器 VMs)。

**程式碼儲存庫**

此模式的程式碼可在使用 CloudFormation 儲存庫的 GitHub UiPath RPA 機器人設定中使用。 [UiPath CloudFormation](https://github.com/aws-samples/uipath-rpa-setup-ec2-windows-ami-cloudformation) 模式也會使用可從 [AWS CloudFormation Macros 儲存庫](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 每月都會發行新的 [Windows AMIs](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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定 EC2 Image Builder 管道。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/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_tw/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_tw/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html) | AWS DevOps | 
| 檢視映像建置器日誌。 | EC2 Image Builder 日誌會在 CloudWatch 日誌群組中彙總。若要在 CloudWatch 中檢視日誌：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html)<br />EC2 Image Builder 日誌也會存放在 S3 儲存貯體中。若要檢視儲存貯體中的日誌：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/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_tw/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html) | AWS DevOps | 

### 部署和測試計數巨集
<a name="deploy-and-test-the-count-macro"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署計數巨集。 | [See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html)如果您想要使用 主控台，請遵循上一個 epic 或 [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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署 Amazon EC2 佈建範本。 | 若要使用 CloudFormation 部署 EC2 映像管道：[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/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_tw/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_tw/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_tw/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_tw/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_tw/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>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 刪除堆疊。 | 當您的 PoC 或試行專案完成時，我們建議您刪除您建立的堆疊，以確保您不會支付這些資源的費用。[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/set-up-uipath-rpa-bots-automatically-on-amazon-ec2-by-using-aws-cloudformation.html)堆疊刪除操作開始後就無法停止。堆疊繼續進行到 `DELETE_IN_PROGRESS` (正在刪除) 狀態。<br />如果刪除失敗，堆疊將處於 `DELETE_FAILED`狀態。如需解決方案，請參閱 AWS CloudFormation 疑難排解文件中的[刪除堆疊失敗](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/troubleshooting.html#troubleshooting-errors-delete-stack-fails)。<br />如需有關保護堆疊免於意外刪除的資訊，請參閱 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 收到格式不正確的回應*。 | 這是已知問題。（請參閱 [AWS CloudFormation 巨集儲存庫](https://github.com/aws-cloudformation/aws-cloudformation-macros/pull/20)中的自訂解決方案和 PR。)<br />若要修正此問題，請開啟 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 儲存庫**
+ [使用 CloudFormation 的 UiPath RPA 機器人設定](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 文件）
+ [監控Amazon EC2 執行個體forAmazon記憶體和磁碟指標](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/mon-scripts.html) (Amazon 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) （堆疊溢位）