在 EC2 映像生成器 中使用文档
要生成组件,您必须提供基于 YAML 的文档,该文档表示用于创建组件的阶段和步骤。
文档部分
文档的部分如下所示。
-
阶段。阶段是步骤的逻辑分组形式。
-
每个阶段名称在文档中必须是唯一的。
-
您可以在文档中定义多个阶段。
-
镜像生成器 在镜像生成管道中执行称为“生成”、“验证”和“测试”的阶段。
-
-
步骤。步骤是组成每个阶段的工作流程的各个工作单元。
-
每个步骤必须定义要执行的操作。
-
对于每个阶段,每个步骤必须具有唯一的名称。
-
步骤按顺序执行。
-
可以将步骤的输入和输出作为后续步骤的输入(“链”)。
-
每个步骤使用一个操作模块,该模块返回一个退出代码。
-
-
支持的操作。支持的操作是每个步骤必须在文档中包含的操作。每个支持的操作与一个操作模块相关。有关支持的操作模块的完整列表(包括功能详细信息以及输入/输出值和示例),请参阅组件管理器支持的操作模块。
-
输出文件。配置管理应用程序为每次执行创建以下输出文件:
-
detailedOutput.json:描述有关编排的所有详细信息的文件。包含有关执行的每个阶段、步骤和操作的信息。
-
document.yaml:发送到该应用程序以执行的文件。存储为执行的构件。
-
console.log:包含在执行期间捕获的所有标准输出 (stdout) 和标准错误 (stderr) 信息。
-
application.log:包含调试执行生成的日志。
-
输入和输出链
配置管理应用程序提供了一种功能,它使用以下格式编写引用以将输入和输出链在一起:
{{ phase_name.step_name.inputs/outputs.variable
}}
或
{{ phase_name.step_name.inputs/outputs[index].variable
}}
通过使用链功能,您可以回收代码并提高文档的可维护性。
链的使用要求如下所示:
-
只能在每个步骤的输入部分中使用链表达式。
-
具有链表达式的语句必须用引号引起来。例如:
-
无效的表达式:
echo {{ phase.step.inputs.variable }}
-
有效的表达式:
"echo {{ phase.step.inputs.variable }}"
-
有效的表达式:
'echo {{ phase.step.inputs.variable }}'
-
-
链表达式可以引用同一文档中的其他步骤和阶段的变量。
-
链表达式中的索引从 0 开始编制(第一个索引为 0)。
示例
要在以下示例步骤的第二个条目中引用源变量,链模式为 {{
build.
。
SampleS3Download
.inputs[1].source
}}
phases: - name: 'build' steps: - name:
SampleS3Download
action: S3Download timeoutSeconds: 60 onFailure: Abort maxAttempts: 3 inputs: - source: 's3://sample-bucket
/sample1
.ps1' destination: 'C:\Temp\sample1
.ps1' - source: 's3://sample-bucket
/sample2
.ps1' destination: 'C:\Temp\sample2
.ps1'
要引用以下示例步骤的输出变量(等于“Hello”),链模式为 {{
build.
。
SamplePowerShellStep
.outputs.stdout
}}
phases: - name: 'build' steps: - name:
SamplePowerShellStep
action: ExecutePowerShell timeoutSeconds: 120 onFailure: Abort maxAttempts: 3 inputs: commands: - 'echo "Hello"'
文档架构和定义
以下是文档的 YAML 架构。
name: (optional) description: (optional) schemaVersion: "string" phases: - name: "string" steps: - name: "string" action: "string" timeoutSeconds: integer onFailure: "Abort|Continue" maxAttempts: integer inputs:
文档的架构定义如下所示。
字段 | 描述 | 类型 | 必需 |
---|---|---|---|
name | 文档的名称。 | 字符串 | 否 |
description | 文档的描述。 | 字符串 |
否 |
schemaVersion | 文档的架构版本,当前为 1.0。 | 字符串 |
是 |
phases | 阶段及其步骤的列表。 |
列表 |
是 |
阶段的架构定义如下所示。
字段 | 描述 | 类型 | 必需 |
---|---|---|---|
name | 阶段的名称。 | 字符串 | 是 |
steps | 阶段中的步骤列表。 | 列表 |
是 |
步骤的架构定义如下所示。
字段 | 描述 | 类型 | 必需 | 默认值 |
---|---|---|---|---|
name | 用户定义的步骤名称。 | 字符串 | ||
操作 | 与执行步骤的模块相关的关键字。 | 字符串 | ||
timeoutSeconds |
在失败/重试之前,步骤运行的秒数。 还支持 -1 值,它表示无限超时。不允许使用 0 和其他负值。 |
整数 |
是 |
7,200 秒(120 分钟) |
onFailure | 失败时的执行决定。该步骤可能会中止或继续执行下一步。 |
字符串 |
是 |
Abort |
maxAttempts | 在步骤失败之前允许的最大尝试次数。 | 整数 |
否 |
1 |
inputs | 包含操作模块执行步骤所需的参数。 | 字典 |
是 |
文档示例架构
以下是一个示例文档架构,用于安装所有可用的 Windows 更新,执行配置脚本,在创建 AMI 之前验证更改以及在创建 AMI 后测试更改。
name: RunConfig_UpdateWindows description: 'This document will install all available Windows updates and execute a config script. It will then validate the changes before an AMI is created. Then after AMI creation, it will test all the changes.' schemaVersion: 1.0 phases: - name: build steps: - name: DownloadConfigScript action: S3Download timeoutSeconds: 60 onFailure: Abort maxAttempts: 3 inputs: - source: 's3://
customer-bucket
/config.ps1' destination: 'C:\Temp\config.ps1' - name: RunConfigScript action: ExecutePowerShell timeoutSeconds: 120 onFailure: Abort maxAttempts: 3 inputs: commands: - '{{build.DownloadConfigScript.inputs[0].destination}}' - name: Cleanup action: ExecutePowerShell timeoutSeconds: 120 onFailure: Abort maxAttempts: 3 inputs: commands: - 'Remove-Item {{build.DownloadConfigScript.inputs[0].destination}}' - name: RebootAfterConfigApplied action: Reboot inputs: delaySeconds: 60 - name: InstallWindowsUpdates action: UpdateOS - name: validate steps: - name: DownloadTestConfigScript action: S3Download timeoutSeconds: 60 onFailure: Abort maxAttempts: 3 inputs: - source: 's3://customer-bucket
/testConfig.ps1' destination: 'C:\Temp\testConfig.ps1' - name: ValidateConfigScript action: ExecutePowerShell timeoutSeconds: 120 onFailure: Abort maxAttempts: 3 inputs: commands: - '{{validate.DownloadTestConfigScript.inputs[0].destination}}' - name: Cleanup action: ExecutePowerShell timeoutSeconds: 120 onFailure: Abort maxAttempts: 3 inputs: commands: - 'Remove-Item {{validate.DownloadTestConfigScript.inputs[0].destination}}' - name: test steps: - name: DownloadTestConfigScript action: S3Download timeoutSeconds: 60 onFailure: Abort maxAttempts: 3 inputs: - source: 's3://customer-bucket
/testConfig.ps1' destination: 'C:\Temp\testConfig.ps1' - name: ValidateConfigScript action: ExecutePowerShell timeoutSeconds: 120 onFailure: Abort maxAttempts: 3 inputs: commands: - '{{test.DownloadTestConfigScript.inputs[0].destination}}'
以下是下载和执行自定义 Linux 二进制文件的示例文档架构。
name: LinuxBin description: Download and execute a custom Linux binary file. schemaVersion: 1.0 phases: - name: build steps: - name: Download action: S3Download inputs: - source: s3://
mybucket
/myapplication
destination: /tmp/myapplication
- name: Enable action: ExecuteBash onFailure: Continue inputs: commands: - 'chmod u+x {{ build.Download.inputs[0].destination }}' - name: Install action: ExecuteBinary onFailure: Continue inputs: path: '{{ build.Download.inputs[0].destination }}' arguments: - '--install' - name: Delete action: ExecuteBash inputs: commands: - 'rm {{ build.Download.inputs[0].destination }}'
以下是使用安装文件安装 AWS CLI 的示例文档架构。
name: InstallCLISetUp description: Install AWS CLI using the setup file schemaVersion: 1.0 phases: - name: build steps: - name: Download action: S3Download inputs: - source: s3://aws-cli/AWSCLISetup.exe destination: C:\Windows\temp\AWSCLISetup.exe - name: Install action: ExecuteBinary onFailure: Continue inputs: path: '{{ build.Download.inputs[0].destination }}' arguments: - '/install' - '/quiet' - '/norestart' - name: Delete action: ExecutePowerShell inputs: commands: - Remove-Item -Path '{{ build.Download.inputs[0].destination }}' -Force
以下是使用 MSI 安装程序安装 AWS CLI 的示例文档架构。
name: InstallCLIMSI description: Install AWS CLI using the MSI installer schemaVersion: 1.0 phases: - name: build steps: - name: Download action: S3Download inputs: - source: s3://aws-cli/AWSCLI64PY3.msi destination: C:\Windows\temp\AWSCLI64PY3.msi - name: Install action: ExecuteBinary onFailure: Continue inputs: path: 'C:\Windows\System32\msiexec.exe' arguments: - '/i' - '{{ build.Download.inputs[0].destination }}' - '/quiet' - '/norestart' - name: Delete action: ExecutePowerShell inputs: commands: - Remove-Item -Path '{{ build.Download.inputs[0].destination }}' -Force