教程:使用输入参数创建自定义组件 - EC2Image Builder
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应用程序组件文档。该文档包含在您为自定义图像而定义的各个阶段和步骤中运行的代码。引用组件的配方可以设置参数以在运行时自定义值,如果参数未设置为特定值,则默认值将生效。

使用输入参数创建组件文档

本节介绍如何在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

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

从组件文档创建组YAML件

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

  • 要使用 Image Builder 控制台直接从您的YAML文档创建组件,请参阅从控制台创建自定义组件

  • 要使用 Image Builder 命令从create-component命令行创建组件,请参见从中创建自定义组件 Amazon CLI。将这些示例中的YAML文档名称替换为您的 Hello World YAML 文档的名称 (hello-world-test.yaml)。

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

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

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

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

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

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

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

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