本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
教學課程:使用輸入參數建立自訂元件
您可以管理 Image Builder 元件,包括直接從 EC2 Image Builder 主控台、 AWS CLI或 Image Builder API 或 SDKs 建立和設定元件參數。在本節中,我們將介紹在元件中建立和使用參數,以及在執行時間透過 Image Builder 主控台和 AWS CLI 命令設定元件參數。
元件參數是純文字值,並會登入 AWS CloudTrail。建議您使用 AWS Secrets Manager 或 AWS Systems Manager 參數存放區來存放秘密。如需 Secrets Manager 的詳細資訊,請參閱AWS Secrets Manager 《 使用者指南》中的什麼是 Secrets Manager?。如需 AWS Systems Manager 參數存放區的詳細資訊,請參閱AWS Systems Manager 《 使用者指南》中的AWS Systems Manager 參數存放區。
在 YAML 元件文件中使用參數
若要建置元件,您必須提供 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 驗證器等工具,或在您的程式碼環境中使用 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 驗證器等工具,或在您的程式碼環境中使用 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 驗證器等工具,或在您的程式碼環境中使用 YAML lint 延伸模組來驗證 YAML 的格式是否良好。
如需應用程式元件文件的階段、步驟和語法 AWS TOE 的詳細資訊,請參閱使用 文件 AWS TOE。如需參數及其需求的詳細資訊,請參閱 頁面中的定義和參考變數 AWS TOE參數一節。
從 YAML 元件文件建立元件
無論您使用何種方法來建立 AWS TOE 元件,YAML 應用程式元件文件一律需要做為基準。
從主控台在映像建置器配方中設定元件參數
設定元件參數對映像配方和容器配方的運作方式相同。當您建立新的配方或新版本的配方時,您可以從建置元件和測試元件清單中選擇要包含的元件。元件清單包含適用於您為映像選擇之基礎作業系統的元件。
選取元件後,該元件會顯示在元件清單下的所選元件區段中。組態選項會針對選取的每個元件顯示。如果您的元件已定義輸入參數,則會顯示為稱為輸入參數的可擴展區段。
為元件定義的每個參數會顯示下列參數設定: