Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅
中国的 Amazon Web Services 服务入门
(PDF)。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
教程:创建带有输入参数的自定义组件
您可以直接从 Image Builder 控制台、或从 Image Builder 或 EC2 Image Builder 中管理 Image Builder API 组件 Amazon CLI,包括创建和设置组件参数SDKs。在本节中,我们将介绍在组件中创建和使用参数,以及在运行时通过 Image Builder 控制台和 Amazon CLI 命令设置组件参数。
组件参数是纯文本值,并且已记录在 Amazon CloudTrail中。我们建议您使用 Amazon Secrets Manager 或 P Amazon Systems Manager arameter Store 来存储您的密钥。有关 Secrets Manager 的更多信息,请参阅Amazon Secrets Manager 用户指南中的什么是 Secrets Manager?。有关 Amazon Systems Manager Parameter Store 的更多信息,请参阅《Amazon Systems Manager 用户指南》中的Amazon Systems Manager Parameter Store。
在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
- 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
- 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
有关 EC2 TOE 应用程序组件文档的阶段、步骤和语法的更多信息,请参阅在 EC2 TOE中使用文档。有关参数及其要求的更多信息,请参阅在 EC2 TOE中定义和引用变量页面的 参数 部分。
从组件文档创建组YAML件
无论使用何种方法来创建 EC2 TOE 组件,都需要将YAML应用程序组件文档作为基准。
通过控制台在 Image Builder 配方中设置组件参数
对于映像配方和容器配方,设置组件参数的作用相同。创建新配方或配方的新版本时,可以从构建组件和测试组件列表中选择要包含的组件。组件列表包括适用于您为映像选择的基本操作系统的组件。
选择组件后,该组件将显示在组件列表正下方的选定组件部分中。将显示每个选定组件的配置选项。如果您的组件定义了输入参数,则它们将显示为名为输入参数的可扩展部分。
为组件定义的每个参数都会显示以下参数设置: