在映像建置器中為自訂元件建立 YAML 元件文件 - EC2 Image Builder

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

在映像建置器中為自訂元件建立 YAML 元件文件

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

本節中的一些範例會建立建置元件,在 AWS TOE 元件管理應用程式中呼叫 UpdateOS 動作模組。模組會更新作業系統。如需 UpdateOS動作模組的詳細資訊,請參閱 UpdateOS

macOS 作業系統範例使用 ExecuteBash動作模組來安裝和驗證wget公用程式。UpdateOS 動作模組不支援 macOS。如需 ExecuteBash動作模組的詳細資訊,請參閱 ExecuteBash。如需應用程式元件文件的階段、步驟和語法 AWS TOE 的詳細資訊,請參閱使用 文件 AWS TOE

注意

Image Builder 會從元件文件中定義的階段決定元件類型,如下所示:

  • Build – 這是預設元件類型。任何未分類為測試元件的項目,都是建置元件。此類型的元件會在映像建置階段期間執行。如果此建置元件已定義test階段,則該階段會在測試階段執行。

  • 測試 – 若要符合測試元件的資格,元件文件只能包含一個名為 的階段test。對於與建置元件組態相關的測試,我們建議您不要使用獨立測試元件。反之,請在相關聯的建置元件中使用 test階段。

如需 Image Builder 如何在其建置程序中使用階段和階段來管理元件工作流程的詳細資訊,請參閱 使用 元件來自訂映像建置器映像

若要為範例應用程式建立 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 驗證器等工具,或在您的程式碼環境中使用 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 驗證器等工具,或在您的程式碼環境中使用 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 驗證器等工具,或在您的程式碼環境中使用 YAML lint 延伸模組來驗證 YAML 的格式是否良好。