

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

# 教學課程：使用輸入參數建立自訂元件
<a name="tutorial-component-parameters"></a>

您可以直接從 EC2 Image Builder 主控台、 或從 Image Builder API 或 SDKs 管理 Image Builder 元件 AWS CLI，包括建立和設定元件參數。在本節中，我們將介紹在您的元件中建立和使用參數，以及在執行時間透過 Image Builder 主控台和 AWS CLI 命令設定元件參數。

**重要**  
元件參數是純文字值，並會登入 AWS CloudTrail。建議您使用 AWS Secrets Manager 或 AWS Systems Manager 參數存放區來存放秘密。如需 Secrets Manager 的詳細資訊，請參閱*AWS Secrets Manager 《 使用者指南*》中的[什麼是 Secrets Manager？](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html)。如需 AWS Systems Manager 參數存放區的詳細資訊，請參閱*AWS Systems Manager 《 使用者指南*》中的[AWS Systems Manager 參數存放區](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html)。

## 在 YAML 元件文件中使用參數
<a name="component-params-yaml"></a>

若要建置元件，您必須提供 YAML 或 JSON 應用程式元件文件。文件包含您在為映像提供自訂所定義的階段和步驟期間執行的程式碼。參考元件的配方可以設定參數，以在執行時間自訂值，預設值會在參數未設定為特定值時生效。

**使用輸入參數建立元件文件**  
本節說明如何在 YAML 元件文件中定義和使用輸入參數。

若要建立使用參數並在映像建置器建置或測試執行個體中執行命令的 YAML 應用程式元件文件，請遵循符合您映像作業系統的步驟：

------
#### [ Linux ]

**建立 YAML 元件文件**  
使用檔案編輯工具來建立元件文件檔案。文件範例使用名為 的檔案`hello-world-test.yaml`，其中包含下列內容：

```
# Document Start
# 
name: "HelloWorldTestingDocument-Linux"
description: "Hello world document to demonstrate parameters."
schemaVersion: 1.0
parameters:
  - MyInputParameter:
      type: string
      default: "It's me!"
      description: This is an input parameter.
phases:
  - name: build
    steps:
      - name: HelloWorldStep
        action: ExecuteBash
        inputs:
          commands:
            - echo "Hello World! Build phase. My input parameter value is {{ MyInputParameter }}"

  - name: validate
    steps:
      - name: HelloWorldStep
        action: ExecuteBash
        inputs:
          commands:
            - echo "Hello World! Validate phase. My input parameter value is {{ MyInputParameter }}"

  - name: test
    steps:
      - name: HelloWorldStep
        action: ExecuteBash
        inputs:
          commands:
            - echo "Hello World! Test phase. My input parameter value is {{ MyInputParameter }}"
# Document End
```

**提示**  
使用此線上 [YAML 驗證器](https://jsonformatter.org/yaml-validator)之類的工具，或在您的程式碼環境中使用 YAML lint 延伸模組，以驗證您的 YAML 格式是否正確。

------
#### [ Windows ]

**建立 YAML 元件文件**  
使用檔案編輯工具來建立元件文件檔案。文件範例使用名為 的檔案`hello-world-test.yaml`，其中包含下列內容：

```
# Document Start
# 
name: "HelloWorldTestingDocument-Windows"
description: "Hello world document to demonstrate parameters."
schemaVersion: 1.0
parameters:
  - MyInputParameter:
      type: string
      default: "It's me!"
      description: This is an input parameter.
phases:
  - name: build
    steps:
      - name: HelloWorldStep
        action: ExecutePowerShell
        inputs:
          commands:
            - Write-Host "Hello World! Build phase. My input parameter value is {{ MyInputParameter }}"

  - name: validate
    steps:
      - name: HelloWorldStep
        action: ExecutePowerShell
        inputs:
          commands:
            - Write-Host "Hello World! Validate phase. My input parameter value is {{ MyInputParameter }}"

  - name: test
    steps:
      - name: HelloWorldStep
        action: ExecutePowerShell
        inputs:
          commands:
            - Write-Host "Hello World! Test phase. My input parameter value is {{ MyInputParameter }}"
# Document End
```

**提示**  
使用此線上 [YAML 驗證器](https://jsonformatter.org/yaml-validator)等工具，或在您的程式碼環境中使用 YAML lint 延伸模組，以確認您的 YAML 格式正確。

------
#### [ macOS ]

**建立 YAML 元件文件**  
使用檔案編輯工具來建立元件文件檔案。文件範例使用名為 的檔案`hello-world-test.yaml`，其中包含下列內容：

```
# Document Start
# 
name: "HelloWorldTestingDocument-macOS"
description: "Hello world document to demonstrate parameters."
schemaVersion: 1.0
parameters:
  - MyInputParameter:
      type: string
      default: "It's me!"
      description: This is an input parameter.
phases:
  - name: build
    steps:
      - name: HelloWorldStep
        action: ExecuteBash
        inputs:
          commands:
            - echo "Hello World! Build phase. My input parameter value is {{ MyInputParameter }}"

  - name: validate
    steps:
      - name: HelloWorldStep
        action: ExecuteBash
        inputs:
          commands:
            - echo "Hello World! Validate phase. My input parameter value is {{ MyInputParameter }}"

  - name: test
    steps:
      - name: HelloWorldStep
        action: ExecuteBash
        inputs:
          commands:
            - echo "Hello World! Test phase. My input parameter value is {{ MyInputParameter }}"
# Document End
```

**提示**  
使用此線上 [YAML 驗證器](https://jsonformatter.org/yaml-validator)之類的工具，或在您的程式碼環境中使用 YAML lint 延伸，以驗證您的 YAML 格式正確。

------

如需應用程式元件文件之階段、步驟和語法 AWS TOE 的詳細資訊，請參閱[使用 文件 AWS TOE](https://docs.aws.amazon.com/imagebuilder/latest/userguide/toe-use-documents.html)。如需參數及其需求的詳細資訊，請參閱 **頁面中的定義和參考變數 AWS TOE**[Parameters](toe-user-defined-variables.md#user-defined-vars-parameters)一節。

**從 YAML 元件文件建立元件**  
無論您使用何種方法來建立 AWS TOE 元件，YAML 應用程式元件文件一律需要做為基準。
+ 若要使用映像建置器主控台直接從 YAML 文件建立元件，請參閱 [從主控台建立自訂元件](create-component.md#create-component-ib-console)。
+ 若要使用 Image Builder **create-component**命令從命令列建立元件，請參閱 [從 建立自訂元件 AWS CLI](create-component.md#create-component-ib-cli)。將這些範例中的 YAML 文件名稱取代為您的 Hello World YAML 文件名稱 (`hello-world-test.yaml`)。

## 從主控台在映像建置器配方中設定元件參數
<a name="recipe-set-component-params"></a>

設定元件參數對映像配方和容器配方的運作方式相同。當您建立新的配方或新版本的配方時，您可以從**建置元件**和**測試元件**清單中選擇要包含的元件。元件清單包含適用於您為映像選擇之基礎作業系統的元件。

選取元件後，該元件會顯示在元件清單下的**所選**元件區段中。系統會為每個選取的元件顯示組態選項。如果您的元件已定義輸入參數，則會顯示為稱為**輸入參數**的可擴展區段。

為元件定義的每個參數顯示下列參數設定：
+ **參數名稱** (*不可編輯*) – 參數的名稱。
+ **描述** (*不可編輯*) – 參數描述
+ **類型** (*不可編輯*) – 參數值的資料類型。
+ **值** – 參數的值。如果您在此配方中第一次使用此元件，且已為輸入參數定義預設值，則預設值會顯示在帶有灰色文字**的值**方塊中。如果未輸入其他值，Image Builder 會使用預設值。