

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

# 设置企业蓝图工厂
<a name="setup"></a>

本节将帮助您在 AWS 环境中设置企业蓝图工厂。它包括为企业蓝图工厂设置所需存储库和 AWS 资源的详细说明。

## 先决条件
<a name="setup-prereqs"></a>

以下是在您的 AWS 环境中设置企业蓝图工厂的先决条件：
+ 以下内容 AWS 账户：
  + 用于管理企业蓝图工厂和发布产品的帐户
  + 一个或多个使用已发布产品的账户
+ 所有账户都是：
  + 作为一个组织进行管理 [AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_introduction.html)
  + 位于同一个[组织单位 (OU)](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_getting-started_concepts.html#organizationalunit)
  + 该组织遵循[account-per-tenant 模式](https://aws.amazon.com/blogs/mt/managing-the-account-lifecycle-in-account-per-tenant-saas-environments-on-aws/)
+ AWS Command Line Interface (AWS CLI)，[已安装](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)并[配置](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)
+ 部署用于创建以下 AWS 资源的 AWS CloudFormation 堆栈的权限：
  + Amazon Log CloudWatch s 日志组
  + AWS CodePipeline 管道
  + AWS CodeBuild 项目
  + Amazon EventBridge 活动总线政策和规则
  + AWS Identity and Access Management (IAM) 角色和策略
  + AWS Key Management Service (AWS KMS) 密钥和密钥策略
  + AWS Service Catalog 产品组合、产品和预配置产品
  + 亚马逊简单通知服务 (Amazon SNS) Service 主题、主题政策和订阅
  + Amazon Simple Storage Service (Amazon S3) 存储桶
  + AWS Systems Manager 参数存储参数

  有关设置这些权限的更多信息，请参阅[CloudFormation 文档](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-iam-template.html)和[实施最低权限策略](https://docs.aws.amazon.com/prescriptive-guidance/latest/least-privilege-cloudformation/introduction.html)。 AWS CloudFormation
+ 一个 GitHub 账户

## 最佳实践
<a name="setup-best-practices"></a>

我们建议您在 AWS 环境中设置企业蓝图工厂时遵循以下最佳实践：
+ 配置部署企业蓝图工厂所需的权限时，请遵循最低权限原则并授予所需的最低权限。有关更多信息，请参阅 IAM 文档中的[授予最低权限](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html#grant-least-priv)和[安全最佳实践](https://docs.aws.amazon.com/IAM/latest/UserGuide/IAMBestPracticesAndUseCases.html)。
+ 配置对 Service Catalog 组合的访问权限时，请遵循最低权限原则，仅向特定角色、用户或管理员授予访问权限。遵循 Service Catalog [的安全最佳实践](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/security-best-practices.html)。

## 创建存储库
<a name="setup-create-repos"></a>

本节将帮助您为企业蓝图工厂设置[配置存储库](architecture-components.md#architecture-config-repo)[和产品存储库](architecture-components.md#architecture-product-repo)。要设置存储库，请将提供的存储库[分叉](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/fork-a-repo)进去 GitHub。然后，您可以使用创建 AWS CodeConnections 与存储 GitHub 库的[连接](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-create-github.html)。然后，将 GitHub 存储库克隆到本地计算机。

**分叉 GitHub 存储库**

1. 登录 [GitHub](https://github.com/login)。

1. 导航到[配置 GitHub 存储库存储库](https://github.com/aws-samples/aws-enterprise-blueprint-factory-config-repo)。

1. 选择 “**分叉**”。

1. 在 “**创建新分支**” 页面上，在 “**存储库名称**” 框中输入`ServiceCatalog-ConfigRepo`。

1. （可选）输入描述。

1. 选择 “**仅复制主分支**”。

1. 选择 “**创建分叉**”。

1. 重复这些步骤来分叉[代码存储 GitHub 库](https://github.com/aws-samples/aws-enterprise-blueprint-factory-code-repo)。输入此存储库`ServiceCatalog-CodeRepo`的名称。

1. 重复这些步骤来分叉[产品存储 GitHub 库](https://github.com/aws-samples/aws-enterprise-blueprint-factory-blueprint-repo)。输入此存储库`ServiceCatalog-BlueprintProductRepo`的名称。

**创建 CodeConnections 连接**

1. 在 AWS CLI 中，输入以下命令以创建 CodeConnections 与的连接 GitHub：

   ```
   aws codeconnections create-connection --provider-type GitHub --connection-name <MyConnection>
   ```

1. 使用 AWS 开发者工具控制台完成连接。有关更多信息，请参阅[更新挂起的连接](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections-update.html)。

**克隆分叉存储库**
+ 输入以下命令将 GitHub 存储库克隆到本地工作站：

  ```
  git clone git@github.com:<user>/aws-enterprise-blueprint-factory-config-repo ServiceCatalog-ConfigRepo
  git clone git@github.com:<user>/aws-enterprise-blueprint-factory-blueprint-repo ServiceCatalog-BlueprintProductRepo
  git clone git@github.com:<user>/aws-enterprise-blueprint-factory-code-repo ServiceCatalog-CodeRepo
  ```

## 设置企业蓝图工厂
<a name="setup-factory"></a>

本节中的说明描述了如何在目标账户中设置企业蓝图工厂。您从中克隆的产品存储库 GitHub 包含两个示例 CloudFormation 模板，`BP-S3`和。`BP-SNS`按照这些说明操作，您可以将这两个示例蓝图作为产品部署在 Service Catalog 中。

**要设置角色**

1. 在蓝图开发者的账户中，创建以下信任策略，然后将其另存为`sc-enduserrole-trust-policy.json`：

   ```
   { 
     "Version": "2012-10-17",		 	 	 
     "Statement": {
       "Effect": "Allow",
       "Principal": {
         "AWS": "arn:aws:iam::123456789012:role/ServiceCatalogEndUserRole"
       },
       "Action": "sts:AssumeRole"
     }
   }
   ```

1. 输入以下命令创建 `ServiceCatalogEndUserRole` IAM 角色：

   ```
   aws iam create-role \
   --role-name ServiceCatalogEndUserRole \
   --assume-role-policy-document file://sc-enduserrole-trust-policy.json  
   aws iam attach-role-policy \
   --policy-arn arn:aws:iam::aws:policy/AWSServiceCatalogEndUserFullAccess \
   -- role-name ServiceCatalogEndUserRole
   ```
**注意**  
开发人员使用该`ServiceCatalogEndUserRole`角色来配置 Service Catalog 产品。此角色不需要权限即可创建蓝图中定义的资源。这遵循了最低特权权限和职责分工的最佳实践。

1. 创建以下信任策略，然后将其另存为`sc-launchconstraintrole-trust-policy.json`：

   ```
   { 
     "Version": "2012-10-17",		 	 	 
     "Statement": {
       "Effect": "Allow",
       "Principal": {
         "Service": "servicecatalog.amazonaws.com"
       },
       "Action": "sts:AssumeRole"
     }
   }
   ```

1. 输入以下命令创建 `ServiceCataloglogLaunchConstraintRole` IAM 角色：

   ```
   aws iam create-role \
   --role-name ServiceCataloglogLaunchConstraintRole \
   --assume-role-policy-document file://sc-launchconstraintrole-trust-policy.json  
   aws iam attach-role-policy \
   --policy-arn arn:aws:iam::aws:policy/AmazonSNSFullAccess \
   --role-name ServiceCataloglogLaunchConstraintRole
   aws iam attach-role-policy \
   --policy-arn arn:aws:iam::aws:policy/AWSCloudFormationFullAccess \
   --role-name ServiceCataloglogLaunchConstraintRole
   ```

1. 向 `ServiceCataloglogLaunchConstraintRole` IAM 角色添加以下策略。包括产品资源所需的任何其他权限，如 Service Catal [og 文档中的配置启动角色](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/constraints-launch.html#constraints-launch-role)中所述：

   ```
   {
             "Statement":[
         {
            "Effect":"Allow",
            "Action":[
               "s3:GetObject"
            ],
            "Resource":"*",
            "Condition":{
               "StringEquals":{
                  "s3:ExistingObjectTag/servicecatalog:provisioning":"true"
               }
            }
      ]
   }
   ```
**注意**  
Service Catalog 使用此角色将 CloudFormation 堆栈作为产品部署到服务目录中。此角色的信任策略确保只有 Service Catalog 可以担任该角色。其他用户或服务不能担任此角色。这遵循了职责分离的最佳做法。

1. 创建以下信任策略，然后将其另存为`sc-codebuild-trust-policy.json`：

   ```
   { 
     "Version": "2012-10-17",		 	 	 
     "Statement": {
       "Effect": "Allow",
       "Principal": {
         "Service": "codebuild.amazonaws.com"
       },
       "Action": "sts:AssumeRole"
     }
   }
   ```

1. 输入以下命令创建 `codebuild-servicecatalog-admin-role` IAM 角色：

   ```
   aws iam create-role \
   --role-name codebuild-servicecatalog-admin-role \
   --assume-role-policy-document file://sc-codebuild-trust-policy.json  
   aws iam attach-role-policy \
   --policy-arn arn:aws:iam::aws:policy/AWSCodeBuildAdminAccess \
   --role-name codebuild-servicecatalog-admin-role
   ```
**注意**  
配置管道中的 CodeBuild 作业使用此角色。

**设置 Amazon S3 存储桶**
+ 要创建用于存储项目的亚马逊简单存储服务 (Amazon S3) 存储桶，请按照 Amazon S3 文档[中创建](https://docs.aws.amazon.com/AmazonS3/latest/userguide/create-bucket-overview.html)存储桶中的说明进行操作。 CodePipeline 遵循[适用于 Amazon S3 的安全最佳实践](https://docs.aws.amazon.com/AmazonS3/latest/userguide/security-best-practices.html)。

**要设置 AWS Systems Manager 参数**
+ 按照在 S [ystems Manager 中创建参数库参数](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-paramstore-su-create.html)中的说明创建下表中的 Systems Manager 参数。这些参数用于部署配置管道的 CloudFormation 模板中。  
****    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/enterprise-blueprint-factory/setup.html)

**更新 CloudFormation 模板**

1. 在代码存储库 (`ServiceCatalog-CodeRepo`) 中，打开 **ServiceCatalog-pipeline** .yml 文件。

1. 编辑此文件中以下参数的默认值：
   + `ConfigRepositoryName`是 Systems Manager 参数，用于存储企业蓝图工厂配置文件的存储 GitHub库。默认值为 `/blueprints/resources/ConfigRepo`。
   + `CodeRepositoryName`是 Systems Manager 参数，用于存储存储企业蓝图工厂配置管道代码和`Bootstrapping-Admin-Product`代码的存储 GitHub 库。默认值为 `/blueprints/resources/CodeRepo`。
   + `BlueprintRepositoryName`是 Systems Manager 参数，用于存储企业蓝图工厂蓝图蓝图的存储 GitHub库。默认值为 `/blueprints/resources/BlueprintRepo`。
   + `BranchName`是存储配置文件的配置存储库的分支。默认值为 `main`。
   + `VPCID`是存储目标 VPC 的 ID 的 Systems Manager 参数。默认值为 `/blueprints/resources/vpc_id`。
   + `Subnets`是 Systems Manager 参数， IDs 用于存储目标子网的。默认值为 `/blueprints/resources/subnets`。
   + `SecurityGroupIds`是 Systems Manager 参数，用于存储目标安全组的。 IDs 默认值为 `/blueprints/resources/securitygroups`。
   + `IamRoleName`是 CodeBuild 任务使用的 IAM 角色的名称。 默认值为`codebuild-servicecatalog-admin-role`。
   + `EnvironmentType`是您部署企业蓝图工厂的环境。默认值为 `DEV`。
   + `ArtifactBucket`是 Systems Manager 参数，用于存储存储工件的 Amazon S3 CodePipeline 存储桶。 默认值为`/blueprints/resources/artifacts-bucket-name`。
   + `CodeConnectionArn`是与的 CodeConnections连接的亚马逊资源名称 (ARN)。 GitHub

1. 保存并关闭 **ServiceCatalog-pipelin** e.yml 文件。

1. 输入以下命令将更改合并到代码存储库中：

   ```
   cd ServiceCatalog-CodeRepo
   git add ServiceCatalog-Pipeline.yml
   git commit -m "<description of change>"
   git push origin main
   ```

1. 在配置存储库 (`ServiceCatalog-ConfigRepo`) 中，打开 **bp\_c** onfig.yml 文件。

1. 根据您的组织需要更新投资组合部分中的值。例如，更新`portfolio_access_roles`和`share_to_ou`属性。有关更多信息，请参阅本指南中的[配置文件](architecture-components.md#architecture-config-file)。

1. 保存并关闭 **bp\_** config.yml 文件。

1. 输入以下命令将更改合并到代码存储库中：

   ```
   cd ServiceCatalog-ConfigRepo
   git add bp_config.yml
   git commit -m "<description of change>"
   git push origin main
   ```

**部署 CloudFormation 堆栈**

1. 登录企业蓝图工厂管理帐户。

1. 切换到具有[管理权限](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AdministratorAccess.html)的 IAM 角色。

1. 打开 [CloudFormation 管理控制台](https://console.aws.amazon.com/cloudformation/)。

1. 在屏幕顶部的导航栏上，选择目标 AWS 区域。

1. 在**堆栈**页面，选择右上角的**堆栈**，然后选择**使用新资源（标准）**。

1. 对于 **Prepare template**（准备模板），选择 **Template is ready**（模板就绪）。

1. 在**指定模板**下，选择**上传模板文件**。

1. 选择 “**选择文件”**，导航到该文件`ServiceCatalog-CodeRepo`夹，然后选择 **ServiceCatalog-pip** eline.yml。

1. 选择**下一步**继续操作并验证模板。

1. 在**堆栈名称**中，输入堆栈的名称。

1. 在 “**参数**” 部分中，请勿更改默认值。

1. 选择**下一步**。

1. 在**配置堆栈选项**页面上，不要更改默认值，然后选择**下一步**。

1. 在**查看并创建**页面上，验证模板和堆栈详细信息，然后选择**提交**。

1. 监控堆栈部署的进度。有关更多信息，请参阅 [CloudFormation 文档](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/monitor-stack-progress.html)。

1. 等待状态更改为`CREATE_COMPLETE`。

**验证部署**

1. 打开 [AWS Service Catalog 管理控制台](https://console.aws.amazon.com/servicecatalog/)。

1. 在导航窗格中，选择**产品**。

1. 确认产品列表中是否有 **ServiceCatalog-Pipelin** e。

1. 打开 [AWS CodePipeline 管理控制台](https://console.aws.amazon.com/codesuite/codepipeline/home)。

1. 在**名称**中，选择配置管道。默认情况下，管道名称为`ServiceCatalog-Pipeline`。

1. 选择 **View history (查看历史记录)**。

1. 查看管道的状态和阶段执行。有关状态的更多信息，请参阅 CodePipeline 文档中的[查看执行状态](https://docs.aws.amazon.com/codepipeline/latest/userguide/executions-view.html#pipelines-executions-status-console)。

1. 等到配置管道的状态变为`Succeeded`。

1. 打开 S [ervice Catalog 控制台](https://console.aws.amazon.com/servicecatalog/)。

1. 在导航窗格中，选择**产品**。

1. 确认 **BP-s3 产品和 **b** p-SNS 产品产品**可用。这表示示例蓝图的产品发布管道已成功完成。

1. 如果要删除在设置企业蓝图工厂时部署的示例蓝图，请按照[删除](using-factory.md#using-factory-delete)蓝图中的说明进行操作。

## 删除企业蓝图工厂
<a name="setup-delete-factory"></a>

如果您不使用企业蓝图工厂，则可以将其删除以停止产生与其 AWS 资源相关的成本。

**删除 资源**

1. 输入以下命令以删除部署在企业蓝图工厂管理账户中的 IAM 角色：

   ```
   aws iam detach-role-policy \
   --policy-arn arn:aws:iam::aws:policy/AWSServiceCatalogEndUserFullAccess \
   --role-name ServiceCatalogEndUserRole
   aws iam delete-role --role-name ServiceCatalogEndUserRole
   aws iam detach-role-policy \
   --policy-arn arn:aws:iam::aws:policy/AmazonSNSFullAccess \
   --role-name ServiceCataloglogLaunchConstraintRole
   aws iam delete-role --role-name ServiceCataloglogLaunchConstraintRole
   ```

1. 删除企业蓝图工厂的 CloudFormation 堆栈。有关说明，请参阅[从 CloudFormation 控制台删除堆栈](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-delete-stack.html)或[从中删除堆栈 AWS CLI](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/service_code_examples.html#delete-stack-sdk)。

1. 删除用于存储项目的 Amazon S3 存储 CodePipeline 桶。有关说明，请参阅 Amazon S3 文档中的[删除存储桶](https://docs.aws.amazon.com/AmazonS3/latest/userguide/delete-bucket.html)。

1. 从参数存储中删除以下 Systems Manager 参数：
   + `/blueprints/resources/vpc_id`
   + `/blueprints/resources/subnets`
   + `/blueprints/resources/securitygroups`
   + `/blueprints/resources/artifacts-bucket-name`
   + `/blueprints/resources/BlueprintRepo`
   + `/blueprints/resources/CodeRepo`
   + `/blueprints/resources/ConfigRepo`

   有关说明，请参阅 Systems Manager 文档中的[从参数存储中删除参数](https://docs.aws.amazon.com/systems-manager/latest/userguide/deleting-parameters.html)。