

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

# 使用企業藍圖工廠
<a name="using-factory"></a>

本節可協助您在環境中建立、更新或刪除藍圖。它提供在其整個[生命週期](architecture-blueprint-lifecycle.md)中管理藍圖的詳細說明。

若要建立或更新自訂藍圖，您必須了解如何建立 IaC 範本，例如 AWS CloudFormation [範本](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-guide.html)或 AWS Cloud Development Kit (AWS CDK) [建構](https://docs.aws.amazon.com/cdk/v2/guide/constructs.html)。本指南不包含有關如何定義透過企業藍圖工廠發行之藍圖的資訊或指示。

## 先決條件
<a name="using-factory-prereqs"></a>

以下是在您的 AWS 環境中使用 Enterprise Blueprint Factory 的先決條件：
+ 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)
+ 擔任 `ServiceCatalogEndUserRole` AWS Identity and Access Management (IAM) 角色的許可
+ CloudFormation 範本或 AWS CDK 建構

## 建立藍圖
<a name="using-factory-create"></a>

企業藍圖工廠管道會部署您在組態檔案中定義的藍圖。開發人員透過將組態檔案合併到組態儲存庫來啟動組態管道。然後，企業藍圖工廠會使用 `ServiceCatalogLaunchConstraintRole`將藍圖部署為 Service Catalog 中的產品。如需組態管道和發行管道在建立藍圖時所執行動作的詳細資訊，請參閱本指南中的[藍圖建立](architecture-blueprint-lifecycle.md#architecture-blueprint-lifecycle-create)。

**將藍圖新增至產品儲存庫**

1. 請確定您已根據本指南中設定企業藍圖工廠的指示[來設定您的企業藍圖工廠](setup.md)。

1. 確認`ServiceCataloglogLaunchConstraintRole`角色的政策可讓您佈建藍圖中定義的資源。

1. 在產品儲存庫 (`ServiceCatalog-BlueprintProductRepo`) 中，為新藍圖建立資料夾。

1. 將 IaC 範本 AWS CDK (CloudFormation 範本或建構） 貼到您建立的資料夾中。

1. 在您建立的資料夾中建立名為 **product\_config.json** 的檔案。

1. 開啟** product\_config.json** 檔案，並將以下內容貼到檔案中：

   ```
   {
       "SchemaVersion": "1.0",
       "ProductVersionName": "1.0.1",
       "Deprecated_Versions" : [],
       "ProductVersionDescription": "<description>",
       "ProductType": "CLOUD_FORMATION_TEMPLATE",
       "Properties": {
           "TemplateFilePath": "./<folder name>/<file name>"
       }
   }
   ```

   其中：
   + `<description>` 是藍圖版本的簡短描述
   + `<folder name>` 是您在產品儲存庫中建立的資料夾名稱
   + `<file name>` 是 IaC 範本的名稱
**注意**  
您可以更新結構描述版本或產品版本名稱，以符合組織的政策。

1. 儲存並關閉 **product\_config.json 檔案。**

1. 輸入下列命令，將變更合併到產品儲存庫：

   ```
   cd ServiceCatalog-BlueprintProductRepo
   git add <folder name>/<file name> <folder name>\product_config.json
   git commit -m "The first version of <file name> blueprint"
   git push origin main
   ```

**更新組態檔案**

1. 在組態儲存庫 (`ServiceCatalog-ConfigRepo`) 中，開啟 **config.yml 檔案。**

1. 視需要編輯新藍圖的 `portfolios` 區段和 `products`區段。如需詳細資訊，請參閱本指南中的[組態檔案](architecture-components.md#architecture-config-file)。

1. 儲存並關閉 **config.yml 檔案。**

1. 輸入下列命令，將變更合併到組態儲存庫：

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

   此提取請求的核准會啟動組態管道。組態管道會建立產品的發行管道。

**若要檢閱部署日誌**

1. 登入企業藍圖工廠管理帳戶。

1. 開啟 [AWS CodePipeline 主控台](https://console.aws.amazon.com/codesuite/codepipeline/home)。

1. 在**名稱**中，選擇產品的發行管道。根據預設，管道名稱為 `BluePrint_<Product-Name>-<CloudFormation-Stack-Name>`。

1. 選擇 **View history (檢視歷程記錄)**。

1. 檢視管道和階段執行的狀態。如需狀態的詳細資訊，請參閱 CodePipeline 文件中的[檢視執行狀態](https://docs.aws.amazon.com/codepipeline/latest/userguide/executions-view.html#pipelines-executions-status-console)。

1. 如果管道失敗，請檢閱失敗的原因。如需如何設定管道監控的指示，請參閱 CodePipeline 文件中的[監控管道](https://docs.aws.amazon.com/codepipeline/latest/userguide/monitoring.html)。如果發行管道因為 cfn-lint 或 cfn\_nag 檢查而失敗，請更正範本中的錯誤。將另一個提取請求提交至產品儲存庫。這會重新啟動發行管道。如需修正範本錯誤的詳細資訊，請參閱本指南中的[故障診斷](#using-factory-troubleshooting)一節。

1. 等到發行管道狀態為 `Succeeded`。

**驗證部署**

1. 登入組織中的消費者帳戶。

1. 擔任 `ServiceCatalogEndUserRole` IAM 角色。

1. 開啟 [Service Catalog 主控台](https://console.aws.amazon.com/servicecatalog/)。

1. 在導覽窗格中，選擇**產品**。

1. 確認新產品可在產品清單中取得。

## 更新藍圖
<a name="using-factory-update"></a>

如需組態管道和發行管道在建立藍圖時所執行動作的詳細資訊，請參閱本指南中的[藍圖更新](architecture-blueprint-lifecycle.md#architecture-blueprint-lifecycle-update)。

**更新藍圖**

1. 在產品儲存庫中，導覽至產品的 資料夾。

1. 貼上更新的 IaC 範本。請確定檔案名稱與先前的版本相同。

1. 開啟 **product\_config.json 檔案。**

1. 對於 `ProductVersionName`，請更新版本編號。

1. 如果您想要防止未來部署先前的產品版本，請在逗號分隔清單中`Deprecated_Versions`輸入先前的版本號碼。

1. 輸入下列命令，將變更合併到產品儲存庫：

   ```
   cd ServiceCatalog-BlueprintProductRepo
   git add <folder name>/<file name> <folder name>\product_config.json
   git commit -m "Version <number> of <file name> blueprint"
   git push origin main
   ```

   此提取請求的核准會啟動產品的發行管道。

**若要檢閱部署日誌**

1. 登入企業藍圖工廠管理帳戶。

1. 開啟 [AWS CodePipeline 主控台](https://console.aws.amazon.com/codesuite/codepipeline/home)。

1. 在**名稱**中，選擇發行管道。根據預設，管道名稱為 `BluePrint_<Product-Name>-<CloudFormation-Stack-Name>`。

1. 選擇 **View history (檢視歷程記錄)**。

1. 檢視管道和階段執行的狀態。如需狀態的詳細資訊，請參閱 CodePipeline 文件中的[檢視執行狀態](https://docs.aws.amazon.com/codepipeline/latest/userguide/executions-view.html#pipelines-executions-status-console)。

1. 如果管道失敗，請檢閱失敗的原因。如需如何設定管道監控的指示，請參閱 CodePipeline 文件中的[監控管道](https://docs.aws.amazon.com/codepipeline/latest/userguide/monitoring.html)。如果發行管道因為 cfn-lint 或 cfn\_nag 檢查而失敗，請更正範本中的錯誤。將另一個提取請求提交至產品儲存庫。這會重新啟動發行管道。如需修正範本錯誤的詳細資訊，請參閱本指南中的[故障診斷](#using-factory-troubleshooting)一節。

1. 等到發行管道狀態為 `Succeeded`。

**驗證更新**

1. 登入組織中的消費者帳戶。

1. 擔任 `ServiceCatalogEndUserRole` IAM 角色。

1. 開啟 [Service Catalog 主控台](https://console.aws.amazon.com/servicecatalog/)。

1. 在導覽窗格中，選擇**產品**。

1. 確認新的產品版本可在產品清單中取得。

## 刪除藍圖
<a name="using-factory-delete"></a>

當您刪除產品時，Service Catalog 會從包含產品的每個產品組合中移除所有產品版本。如需詳細資訊，請參閱 Service Catalog 文件中的[刪除產品](https://docs.aws.amazon.com/servicecatalog/latest/adminguide/productmgmt-delete.html)。如需組態管道和發行管道在建立藍圖時所執行動作的詳細資訊，請參閱本指南中的[藍圖刪除](architecture-blueprint-lifecycle.md#architecture-blueprint-lifecycle-delete)。

**刪除藍圖**

1. 在組態儲存庫中，開啟 **config.yml 檔案。**

1. 編輯產品區段，移除或評論您要刪除的產品。

1. 儲存並關閉 **config.yml 檔案。**

1. 輸入下列命令，將變更合併到組態儲存庫：

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

   此提取請求的核准會啟動組態管道。組態管道會刪除產品及其發行管道。

1. 在產品儲存庫中，刪除產品的資料夾，包括其內容。

1. 輸入下列命令，將變更合併到產品儲存庫：

   ```
   cd ServiceCatalog-BlueprintProductRepo
   git add .
   git commit -m "Delete <file name> blueprint"
   git push origin main
   ```

**驗證刪除**

1. 登入組織中的消費者帳戶。

1. 擔任 `ServiceCatalogEndUserRole` IAM 角色。

1. 開啟 [Service Catalog 主控台](https://console.aws.amazon.com/servicecatalog/)。

1. 在導覽窗格中，選擇**產品**。

1. 確認已刪除的產品不再可用。

## 故障診斷
<a name="using-factory-troubleshooting"></a>

當您建立或更新藍圖時，cfn-lint 和 cfn-nag 工具會驗證藍圖。如需發行管道中驗證的詳細資訊，請參閱本指南中的[發行管道](architecture-components.md#architecture-release-pipeline)。任何報告的語法或安全錯誤都會導致管道失敗。若要透過發行管道成功部署藍圖，您必須更正藍圖中的錯誤。

以下是顯示兩個安全相關錯誤的範例輸出：失敗和警告。

```
BP-SNS.yml
---------------------------------------------------
BP-SNS.yml
---------------------------------------------------
| WARN W47
|
| Resource: ["ExampleTopic"]
| Line numbers: [5]
|
| SNS Topic should specify KmsMasterKeyId property
---------------------------------------------------
| FAIL F18
|
| Resource: ["ExampleTopicPolicy"]
| Line numbers: [10]
|
| SNS topic policy should not allow * principal

Failures count: 1
Warnings count: 1
```

若要更正這些錯誤，請在藍圖檔案中取代 Amazon Simple Notification Service (Amazon SNS) 主題政策中的`*`主體，並將 AWS Key Management Service (AWS KMS) 金鑰與主題建立關聯。下列程式碼範例示範這些更新。

```
ExampleTopic:
    Type: AWS::SNS::Topic  
    Properties:
      TopicName: ExampleTopic
  ExampleTopicPolicy:
    Type: AWS::SNS::TopicPolicy
    Properties: 
      KmsMasterKeyId: alias/aws/sns  # Added KMS key
      PolicyDocument:
        Id: Id1
        Version: '2012-10-17'
        Statement:
        - Sid: Sid2
          Effect: Allow
          Principal:            
            "Service" : "s3.amazonaws.com"  # Replaced "AWS": '*'
          Action: 'sns:Publish'
          Resource: !Ref ExampleTopic
      Topics: 
        - !Ref ExampleTopic
```