

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

# 使用 Cloud Custodian 和 AWS CDK 將 Systems Manager 的 AWS 受管政策自動連接至 EC2 執行個體設定檔
<a name="automatically-attach-an-aws-managed-policy-for-systems-manager-to-ec2-instance-profiles-using-cloud-custodian-and-aws-cdk"></a>

*Ali Asfour 和 Aaron Lennon，Amazon Web Services*

## 總結
<a name="automatically-attach-an-aws-managed-policy-for-systems-manager-to-ec2-instance-profiles-using-cloud-custodian-and-aws-cdk-summary"></a>

您可以將 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體與 AWS Systems Manager 整合，以自動化操作任務並提供更高的可見性和控制。若要與 Systems Manager 整合，EC2 執行個體必須具有已安裝的 [AWS Systems Manager Agent (SSM Agent)](https://docs.aws.amazon.com/systems-manager/latest/userguide/ssm-agent.html) 和連接到其執行個體描述檔的 `AmazonSSMManagedInstanceCore` AWS Identity and Access Management (IAM) 政策。 

不過，如果您想要確保所有 EC2 執行個體描述檔都已連接`AmazonSSMManagedInstanceCore`政策，則更新沒有執行個體描述檔的新 EC2 執行個體，或沒有執行個體描述檔但沒有`AmazonSSMManagedInstanceCore`政策的 EC2 執行個體時，可能會面臨挑戰。跨多個 Amazon Web Services (AWS) 帳戶和 AWS 區域新增此政策可能也很困難。

此模式透過在您的 AWS 帳戶中部署三個 [Cloud Custodian](https://cloudcustodian.io/) 政策，協助解決這些挑戰：
+ 第一個 Cloud Custodian 政策會檢查是否有具有執行個體描述檔但沒有`AmazonSSMManagedInstanceCore`政策的現有 EC2 執行個體。然後連接`AmazonSSMManagedInstanceCore`政策。 
+ 第二個 Cloud Custodian 政策會檢查沒有執行個體描述檔的現有 EC2 執行個體，並新增已連接`AmazonSSMManagedInstanceCore`政策的預設執行個體描述檔。
+ 第三個 Cloud Custodian 政策會在您的帳戶中建立 [AWS Lambda 函數](https://cloudcustodian.io/docs/aws/lambda.html)，以監控 EC2 執行個體和執行個體描述檔的建立。這可確保在建立 EC2 執行個體時自動連接`AmazonSSMManagedInstanceCore`政策。

此模式使用 [AWS DevOps](https://aws.amazon.com/devops/) 工具，將 Cloud Custodian 政策持續大規模部署至多帳戶環境，而無需佈建個別的運算環境。 

## 先決條件和限制
<a name="automatically-attach-an-aws-managed-policy-for-systems-manager-to-ec2-instance-profiles-using-cloud-custodian-and-aws-cdk-prereqs"></a>

**先決條件**
+ 兩個或多個作用中的 AWS 帳戶。一個帳戶是*安全帳戶*，另一個是*成員帳戶*。
+ 在安全帳戶中佈建 AWS 資源的許可。此模式使用[管理員許可](https://docs.aws.amazon.com/singlesignon/latest/userguide/getting-started.html)，但您應該根據組織的需求和政策授予許可。
+ 能夠從安全帳戶擔任成員帳戶的 IAM 角色，並建立必要的 IAM 角色。如需詳細資訊，請參閱 IAM 文件中的[使用 IAM 角色在 AWS 帳戶之間委派存取權](https://docs.aws.amazon.com/IAM/latest/UserGuide/tutorial_cross-account-with-roles.html)。
+ 
**重要**  
安裝並設定 AWS Command Line Interface (AWS CLI)。基於測試目的，您可以使用 `aws configure`命令或設定環境變數來設定 AWS CLI。：這不建議用於生產環境，我們建議您僅將此帳戶授予最低權限存取。如需詳細資訊，請參閱 IAM 文件中的[授予最低權限](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege)。
+ `devops-cdk-cloudcustodian.zip` 檔案 （已連接），下載到您的本機電腦。
+ 熟悉 Python。
+ 安裝和設定必要的工具 (Node.js、AWS 雲端開發套件 (AWS CDK) 和 Git)。您可以使用 `devops-cdk-cloudcustodian.zip` 檔案中的 `install-prerequisites.sh` 檔案來****安裝這些工具。****請確定您使用根權限執行此檔案。 

**限制**
+ 雖然此模式可用於生產環境，但請確保所有 IAM 角色和政策都符合您組織的需求和政策。 

**套件版本**
+ 雲端託管人 0.9 版或更新版本
+ TypeScript 3.9.7 版或更新版本
+ Node.js 14.15.4 版或更新版本
+ `npm` 7.6.1 版或更新版本
+ AWS CDK 1.96.0 版或更新版本

## Architecture
<a name="automatically-attach-an-aws-managed-policy-for-systems-manager-to-ec2-instance-profiles-using-cloud-custodian-and-aws-cdk-architecture"></a>

![\[AWS CodePipeline workflow with CodeCommit, CodeBuild, and deployment to member accounts.\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/169a7bde-789e-4ebd-b4ca-80eb28ac9927/images/8ec0b6b4-d4b0-42e5-833d-24d1e6098fd9.png)


 

該圖顯示以下工作流程：

1. 雲端託管政策會推送到安全帳戶中的 AWS CodeCommit 儲存庫。Amazon CloudWatch Events 規則會自動啟動 AWS CodePipeline 管道。

1. 管道會從 CodeCommit 擷取最新的程式碼，並將其傳送至 AWS CodeBuild 處理的持續整合和持續交付 (CI/CD) 管道的持續整合部分。

1. CodeBuild 會執行完整的 DevSecOps 動作，包括 Cloud Custodian 政策的政策語法驗證，並在`--dryrun`模式下執行這些政策，以檢查識別哪些資源。

1. 如果沒有錯誤，下一個任務會提醒管理員檢閱變更，並核准成員帳戶中的部署。

**技術堆疊**
+ AWS CDK
+ CodeBuild：
+ CodeCommit：
+ CodePipeline
+ IAM
+ Cloud Custodian 

**自動化和擴展**

除了使用 AWS CloudFormation 堆疊部署 AWS 資源之外 CodeBuild ，AWS CDK 管道模組還會佈建使用 CodePipeline 協調建置和測試原始程式碼的 CI/CD 管道。您可以針對組織中的所有成員帳戶和區域使用此模式。您也可以擴展`Roles creation`堆疊，在成員帳戶中部署其他 IAM 角色。 

## 工具
<a name="automatically-attach-an-aws-managed-policy-for-systems-manager-to-ec2-instance-profiles-using-cloud-custodian-and-aws-cdk-tools"></a>
+ [AWS Cloud Development Kit (AWS CDK)](https://docs.aws.amazon.com/cdk/latest/guide/home.html) 是一種軟體開發架構，用於定義程式碼中的雲端基礎設施，並透過 AWS CloudFormation 進行佈建。
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 是一種開放原始碼工具，可讓您使用命令列 shell 中的命令與 AWS 服務互動。
+ [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html) 是雲端中全受管的建置服務。
+ [AWS CodeCommit](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html) 是一種版本控制服務，可用來私下存放和管理資產。
+ [AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html) 是一項持續交付服務，可用來建立模型、視覺化和自動化發行軟體所需的步驟。
+ [AWS Identity and Access Management ](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html)是一種 Web 服務，可協助您安全地控制對 AWS 資源的存取。
+ [Cloud Custodian](https://cloudcustodian.io/) 是一種工具，可將大多數組織用來管理公有雲端帳戶的數十種工具和指令碼統一為單一開放原始碼工具。
+ [Node.js](https://nodejs.org/en/) 是建置在 Google Chrome 的 V8 JavaScript 引擎上的 JavaScript 執行期。 JavaScript 

**Code**

如需此模式中使用的模組、帳戶函數、檔案和部署命令的詳細清單，請參閱 `devops-cdk-cloudcustodian.zip` 檔案中的 `README` 檔案 （已連接）。

## 史詩
<a name="automatically-attach-an-aws-managed-policy-for-systems-manager-to-ec2-instance-profiles-using-cloud-custodian-and-aws-cdk-epics"></a>

### 使用 AWS CDK 設定管道
<a name="set-up-the-pipeline-with-aws-cdk"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 設定 CodeCommit 儲存庫。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-attach-an-aws-managed-policy-for-systems-manager-to-ec2-instance-profiles-using-cloud-custodian-and-aws-cdk.html)[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-attach-an-aws-managed-policy-for-systems-manager-to-ec2-instance-profiles-using-cloud-custodian-and-aws-cdk.html)如需詳細資訊，請參閱 AWS [ CodeCommit 文件中的建立 CodeCommit 儲存庫](https://docs.aws.amazon.com/codecommit/latest/userguide/how-to-create-repository.html)。 AWS CodeCommit  | 開發人員 | 
| 安裝必要的工具。 | 使用 `install-prerequisites.sh` 檔案在 Amazon Linux 上安裝所有必要的工具。這不包含 AWS CLI，因為它已預先安裝。如需詳細資訊，請參閱 [AWS CDK 文件中 AWS CDK 入門](https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html)的[先決條件](https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html#getting_started_prerequisites)一節。 | 開發人員 | 
| 安裝所需的 AWS CDK 套件。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-attach-an-aws-managed-policy-for-systems-manager-to-ec2-instance-profiles-using-cloud-custodian-and-aws-cdk.html)AWS CDK 需要下列套件，並包含在 `requirements.txt` 檔案中：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-attach-an-aws-managed-policy-for-systems-manager-to-ec2-instance-profiles-using-cloud-custodian-and-aws-cdk.html) | 開發人員 | 

### 設定您的環境
<a name="configure-your-environment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 更新所需的變數。 | 在 CodeCommit 儲存庫的根資料夾中開啟 `vars.py` 檔案，並更新下列變數：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-attach-an-aws-managed-policy-for-systems-manager-to-ec2-instance-profiles-using-cloud-custodian-and-aws-cdk.html) | 開發人員 | 
| 使用成員帳戶資訊更新 account.yml 檔案。 | 若要針對多個帳戶執行 [c7n-org Cloud Custodian](https://cloudcustodian.io/docs/tools/c7n-org.html) 工具，您必須將`accounts.yml`組態檔案放在儲存庫的根目錄。以下是 AWS 的 Cloud Custodian 組態檔案範例：<pre>accounts:<br />- account_id: '123123123123'<br />  name: account-1<br />  regions:<br />  - us-east-1<br />  - us-west-2<br />  role: arn:aws:iam::123123123123:role/CloudCustodian<br />  vars:<br />    charge_code: xyz<br />  tags:<br />  - type:prod<br />  - division:some division<br />  - partition:us<br />  - scope:pci</pre> | 開發人員 | 

### 引導 AWS 帳戶
<a name="bootstrap-the-aws-accounts"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 提升安全帳戶。 | 執行下列命令`deploy_account`，以`cloudcustodian_stack`應用程式引導 ：<pre>cdk bootstrap -a 'python3 <br />cloudcustodian/cloudcustodian_stack.py</pre> | 開發人員 | 
| 選項 1 - 自動引導成員帳戶。 | 如果 `cdk_bootstrap_member_accounts` 變數`True`在 `vars.py` 檔案中設定為 ，則 `member_accounts`變數中指定的帳戶會自動由管道引導。如有需要，您可以使用可從安全帳戶擔任的 `*cdk_bootstrap_role*` IAM 角色進行更新，而且該角色具有啟動 AWS CDK 所需的許可。新增至`member_accounts `變數的新帳戶會自動由管道引導，以便部署所需的角色。 | 開發人員 | 
| 選項 2 - 手動引導成員帳戶。 | 雖然我們不建議使用此方法，但您可以設定 的值`cdk_bootstrap_member_accounts`為 `False`，並透過執行下列命令手動執行此步驟：<pre>$ cdk bootstrap -a 'python3 cloudcustodian/member_account_roles_stack.py' \<br /><br />--trust {security_account_id} \<br /><br />--context assume-role-credentials:writeIamRoleName={role_name} \<br /><br />--context assume-role-credentials:readIamRoleName={role_name} \<br /><br />--mode=ForWriting \<br /><br />--context bootstrap=true \<br /><br />--cloudformation-execution-policies arn:aws:iam::aws:policy/AdministratorAccess</pre>請務必使用您可以從安全帳戶取得的 IAM 角色名稱，以及具有啟動 AWS CDK 所需許可的 `{security_account_id}`和 `{role_name}`值。您也可以使用其他方法來引導成員帳戶，例如使用 AWS CloudFormation。如需詳細資訊，請參閱 AWS CDK 文件中的[引導](https://docs.aws.amazon.com/cdk/latest/guide/bootstrapping.html)。 | 開發人員 | 

### 部署 AWS CDK 堆疊
<a name="deploy-the-aws-cdk-stacks"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 在成員帳戶中建立 IAM 角色。 | 執行下列命令來部署`member_account_roles_stack`堆疊，並在成員帳戶中建立 IAM 角色：<pre>cdk deploy --all -a 'python3 cloudcustodian/member_account_roles_stack.py' --require-approval never</pre> | 開發人員 | 
| 部署 Cloud Custodian 管道堆疊。 | 執行下列命令來建立部署到安全帳戶的 Cloud Custodian `cloudcustodian_stack.py`管道：<pre>cdk deploy -a 'python3 cloudcustodian/cloudcustodian_stack.py'</pre> | 開發人員 | 

## 相關資源
<a name="automatically-attach-an-aws-managed-policy-for-systems-manager-to-ec2-instance-profiles-using-cloud-custodian-and-aws-cdk-resources"></a>
+ [AWS CDK 入門](https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html)

## 附件
<a name="attachments-169a7bde-789e-4ebd-b4ca-80eb28ac9927"></a>

若要存取與本文件相關聯的其他內容，請解壓縮下列檔案： [attachment.zip](samples/p-attach/169a7bde-789e-4ebd-b4ca-80eb28ac9927/attachments/attachment.zip)