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