

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

# 在映像建置器中為自訂元件建立 YAML 元件文件
<a name="create-component-yaml"></a>

若要建置元件，您必須提供 YAML 或 JSON 應用程式元件文件。文件包含您在為映像提供自訂所定義的階段和步驟期間執行的程式碼。

本節中的一些範例會建立建置元件，在 AWS TOE 元件管理應用程式中呼叫 `UpdateOS`動作模組。模組會更新作業系統。如需 `UpdateOS`動作模組的詳細資訊，請參閱 [UpdateOS](toe-action-modules.md#action-modules-updateos)。

macOS 作業系統範例使用 `ExecuteBash`動作模組來安裝和驗證`wget`公用程式。`UpdateOS` 動作模組不支援 macOS。如需 `ExecuteBash`動作模組的詳細資訊，請參閱 [ExecuteBash](toe-action-modules.md#action-modules-executebash)。如需應用程式元件文件之階段、步驟和語法 AWS TOE 的詳細資訊，請參閱[使用 文件 AWS TOE](https://docs.aws.amazon.com/imagebuilder/latest/userguide/toe-use-documents.html)。

**注意**  
Image Builder 會從元件文件中定義的階段決定元件類型，如下所示：  
**組建** – 這是預設元件類型。任何未分類為測試元件的項目，都是建置元件。這種類型的元件會在映像*建置階段*期間執行。如果此建置元件已定義`test`階段，則該階段會在*測試階段*期間執行。
**測試** – 若要符合測試元件的資格，元件文件只能包含一個名為 的階段`test`。對於與建置元件組態相關的測試，我們建議您不要使用獨立測試元件。反之，請在相關聯的建置元件中使用 `test`階段。
如需 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 驗證器](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 驗證器](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 驗證器](https://jsonformatter.org/yaml-validator)之類的工具，或在您的程式碼環境中使用 YAML lint 延伸模組來驗證您的 YAML 格式是否正確。

------