

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

# 在 Image Builder 中为自定义组件创建 YAML 组件文档
<a name="create-component-yaml"></a>

要构建组件，必须提供 YAML 或者 JSON 应用程序组件文档。该文档包含了在您为提供映像自定义而定义的阶段和步骤中运行的代码。

本节中的一些示例创建了一个构建组件，该组件在 AWSTOE 组件管理应用程序中调用`UpdateOS`操作模块。该模块更新操作系统。有关 `UpdateOS` 操作模块的更多信息，请参阅 [UpdateOS](toe-action-modules.md#action-modules-updateos)。

macOS 操作系统示例使用 `ExecuteBash` 操作模块来安装和验证 `wget` 实用程序。`UpdateOS` 操作模块不支持 macOS。有关 `ExecuteBash` 操作模块的更多信息，请参阅 [ExecuteBash](toe-action-modules.md#action-modules-executebash)。有关 AWSTOE 应用程序组件文档的阶段、步骤和语法的更多信息，请参阅[在 AWSTOE中使用文档](https://docs.aws.amazon.com/imagebuilder/latest/userguide/toe-use-documents.html)。

**注意**  
Image Builder 根据组件文档中定义的阶段确定组件类型，如下所示：  
**构建** - 这是默认的组件类型。任何未归类为测试组件的组件都是构建组件。这种类型的组件在映像*构建阶段*运行。如果此构建组件已定义 `test` 阶段，则该阶段在*测试阶段*运行。
**测试** - 要获得测试组件资格，组件文档必须仅包含一个名为 `test` 的阶段。对于与构建组件配置相关的测试，我们建议您不要使用独立的测试组件。相反，在关联的构建组件中使用 `test` 阶段。
有关 Image Builder 如何在其构建过程中使用阶段 (stage) 和时段 (phase) 来管理组件工作流的更多信息，请参阅 [使用组件自定义 Image Builder 映像](manage-components.md)。

要为示例应用程序创建 YAML 应用程序组件文档，请按照与您的映像操作系统匹配的选项卡上的步骤进行操作。

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

**创建 YAML 组件文件**  
使用文件编辑工具创建您的组件文档。文档示例使用名为 `update-linux-os.yaml` 的文件，包含如下内容：

```
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
name: update-linux-os
description: Updates Linux with the latest security updates.
schemaVersion: 1
phases:
  - name: build
    steps:
    - name: UpdateOS
      action: UpdateOS
# Document End
```

**提示**  
在代码环境中使用像在线 [YAML Validat](https://jsonformatter.org/yaml-validator) 或 YAML lint 扩展这样的工具来验证 YAML 格式是否正确。

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

**创建 YAML 组件文件**  
使用文件编辑工具创建您的组件文档。文档示例使用名为 `update-windows-os.yaml` 的文件，包含如下内容：

```
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
name: update-windows-os
description: Updates Windows with the latest security updates.
schemaVersion: 1.0
phases:
  - name: build
    steps:
      - name: UpdateOS
        action: UpdateOS
# Document End
```

**提示**  
在代码环境中使用像在线 [YAML Validat](https://jsonformatter.org/yaml-validator) 或 YAML lint 扩展这样的工具来验证 YAML 格式是否正确。

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

**创建 YAML 组件文件**  
使用文件编辑工具创建您的组件文档。文档示例使用名为 `wget-macos.yaml` 的文件，包含如下内容：

```
name: WgetInstallDocument
description: This is wget installation document.
schemaVersion: 1.0

phases:
  - name: build
    steps:
      - name: WgetBuildStep
        action: ExecuteBash
        inputs:
          commands:
            - |
              PATH=/usr/local/bin:$PATH
              sudo -u ec2-user brew install wget


  - name: validate
    steps:
      - name: WgetValidateStep
        action: ExecuteBash
        inputs:
          commands:
            - |
              function error_exit {
                echo $1
                echo "{\"failureMessage\":\"$2\"}"
                exit 1
              }

              type wget
              if [ $? -ne 0 ]; then
                error_exit "$stderr" "Wget installation failed!"
              fi

  - name: test
    steps:
      - name: WgetTestStep
        action: ExecuteBash
        inputs:
          commands:
            - wget -h
```

**提示**  
在代码环境中使用像在线 [YAML Validat](https://jsonformatter.org/yaml-validator) 或 YAML lint 扩展这样的工具来验证 YAML 格式是否正确。

------