教程:创建带有输入参数的自定义组件 - EC2 Image Builder
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

教程:创建带有输入参数的自定义组件

您可以直接从 Image Builder 控制台、Im EC2 age Builder API 或 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
提示

在代码环境中使用像在线 YAML Validat 或 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 Validat 或 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 Validat 或 YAML lint 扩展这样的工具来验证 YAML 格式是否正确。

有关 EC2 TOE 应用程序组件文档的阶段、步骤和语法的更多信息,请参阅在 EC2 TOE中使用文档。有关参数及其要求的更多信息,请参阅在 EC2 TOE中定义和引用变量页面的 参数 部分。

从 YAML 组件文档中创建组件

无论您使用什么方法来创建 EC2 TOE 组件,YAML 应用程序组件文档都必须作为基准。

通过控制台在 Image Builder 配方中设置组件参数

对于映像配方和容器配方,设置组件参数的作用相同。创建新配方或配方的新版本时,可以从构建组件测试组件列表中选择要包含的组件。组件列表包括适用于您为映像选择的基本操作系统的组件。

选择组件后,该组件将显示在组件列表正下方的选定组件部分中。将显示每个选定组件的配置选项。如果您的组件定义了输入参数,则它们将显示为名为输入参数的可扩展部分。

为组件定义的每个参数都会显示以下参数设置:

  • 参数名称不可编辑)- 参数的名称。

  • 描述不可编辑)- 参数描述

  • 类型不可编辑)- 参数值的数据类型。

  • – 参数的值。如果您在此配方中首次使用此组件,并且已为该输入参数定义了默认值,则默认值以灰色文本显示在框中。如果未输入其他值,Image Builder 将使用默认值。