

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

# 教程：创建带有输入参数的自定义组件
<a name="tutorial-component-parameters"></a>

您可以直接从 Image Builder 控制台、Im EC2 age Builder API 或 Image Builder API 管理图像生成器组件 AWS CLI，包括创建和设置组件参数 SDKs。在本节中，我们将介绍在组件中创建和使用参数，以及在运行时通过 Image Builder 控制台和 AWS CLI 命令设置组件参数。

**重要**  
组件参数是纯文本值，并且已记录在 AWS CloudTrail中。我们建议您使用 AWS Secrets Manager 或 P AWS Systems Manager arameter Store 来存储您的密钥。有关 Secrets Manager 的更多信息，请参阅*AWS Secrets Manager 用户指南*中的[什么是 Secrets Manager?](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html)。有关 AWS Systems Manager Parameter Store 的更多信息，请参阅 *AWS Systems Manager 用户指南*中的 [AWS Systems Manager Parameter Store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html)。

## 在 YAML 组件文档中使用参数
<a name="component-params-yaml"></a>

要构建组件，必须提供 YAML 或者 JSON 应用程序组件文档。该文档包含了在您为提供映像自定义而定义的阶段和步骤中运行的代码。引用组件的配方可以设置参数以在运行时自定义值，如果参数未设置为特定值，则默认值将生效。

**使用输入参数创建组件文档**  
本节将介绍如何在 YAML 组件文档中定义和使用输入参数。

要在 Image Builder 构建或测试实例中创建使用参数并运行命令的 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 Validat](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 Validat](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 Validat](https://jsonformatter.org/yaml-validator) 或 YAML lint 扩展这样的工具来验证 YAML 格式是否正确。

------

有关 AWSTOE 应用程序组件文档的阶段、步骤和语法的更多信息，请参阅[在 AWSTOE中使用文档](https://docs.aws.amazon.com/imagebuilder/latest/userguide/toe-use-documents.html)。有关参数及其要求的更多信息，请参阅**在 AWSTOE中定义和引用变量**页面的 [参数](toe-user-defined-variables.md#user-defined-vars-parameters) 部分。

**从 YAML 组件文档中创建组件**  
无论您使用什么方法来创建 AWSTOE 组件，YAML 应用程序组件文档都必须作为基准。
+ 要使用 Image Builder 控制台直接从 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`)。

## 通过控制台在 Image Builder 配方中设置组件参数
<a name="recipe-set-component-params"></a>

对于映像配方和容器配方，设置组件参数的作用相同。创建新配方或配方的新版本时，可以从**构建组件**和**测试组件**列表中选择要包含的组件。组件列表包括适用于您为映像选择的基本操作系统的组件。

选择组件后，该组件将显示在组件列表正下方的**选定组件**部分中。将显示每个选定组件的配置选项。如果您的组件定义了输入参数，则它们将显示为名为**输入参数**的可扩展部分。

为组件定义的每个参数都会显示以下参数设置：
+ **参数名称**（*不可编辑*）- 参数的名称。
+ **描述**（*不可编辑*）- 参数描述
+ **类型**（*不可编辑*）- 参数值的数据类型。
+ **值** – 参数的值。如果您在此配方中首次使用此组件，并且已为该输入参数定义了默认值，则默认值以灰色文本显示在**值**框中。如果未输入其他值，Image Builder 将使用默认值。