

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

# 从中创建 AMI 映像管道 AWS CLI
<a name="cli-create-image-pipeline"></a>

要从中创建映像管道 AWS CLI，请使用适用于您的管道的配置选项运行**create-image-pipeline**命令。您可以选择创建包含所有工作流配置的 JSON 文件，或者在运行时指定配置。本节使用 JSON 配置文件方法来简化命令。

您的管道构建新映像以合并基础映像和组件中所有待处理更新的频率取决于您所配置的 `schedule`。每个 `schedule` 都具有以下属性：
+ `scheduleExpression`— 设置管道运行的时间计划，以评估 `pipelineExecutionStartCondition` 并确定是否应开始构建。该计划是使用 cron 表达式配置的。有关如何在 Image Builder 中设置 cron 表达式格式的更多信息，请参阅 [在 Image Builder 中使用 cron 表达式](cron-expressions.md)。
+ `pipelineExecutionStartCondition`— 确定您的管道是否应该开始构建。有效值包括：
  + `EXPRESSION_MATCH_ONLY`— 每次 cron 表达式与当前时间匹配时，Pipeline 都会生成一个新映像。
  + `EXPRESSION_MATCH_AND_DEPENDENCY_UPDATES_AVAILABLE`— 只有当计划表达式匹配并且您的基础映像或组件有待更新时，您的管道才会生成新映像。如果没有待处理的更新，则会跳过构建。要在不等待更新的情况下立即构建，请手动运行管道。

在中运行**create-image-pipeline**命令时 AWS CLI，许多配置资源都是可选的。但是，有些资源有条件性要求，具体取决于管道创建的映像类型。AMI 图像管道需要以下资源标识符：
+ 映像配方 ARN
+ 基础设施配置 ARN

 

**示例：创建 Windows 2019 镜像**  
此示例配置了一个定于每周星期日运行一次的管道。第一步中显示的配置文件使用映像配方、基础架构和分发配置的现有资源以及其他设置来创建 Windows 2019 映像。

1. 

**创建配置文件（可选）**

   此示例使用名`create-image-pipeline.json`为的配置文件在一个位置配置设置。或者，您可以在运行命令时使用命令行选项来指定配置文件中此处显示的所有详细信息。

   ```
   {
   	"name": "{{ExampleWindows2019Pipeline}}",
   	"description": "{{Builds Windows 2019 Images}}",
   	"enhancedImageMetadataEnabled": true,
   	"imageRecipeArn": "arn:aws:imagebuilder:us-west-{{2:123456789012}}:image-recipe/{{my-example-recipe}}/2020.12.03",
   	"infrastructureConfigurationArn": "arn:aws:imagebuilder:us-west-{{2:123456789012}}:infrastructure-configuration/{{my-example-infrastructure-configuration}}",
   	"distributionConfigurationArn": "arn:aws:imagebuilder:us-west-{{2:123456789012}}:distribution-configuration/{{my-example-distribution-configuration}}",
   	"imageTestsConfiguration": {
   		"imageTestsEnabled": true,
   		"timeoutMinutes": 60
   	},
   	"schedule": {
   		"scheduleExpression": "cron(0 0 * * SUN *)",
   		"pipelineExecutionStartCondition": "EXPRESSION_MATCH_AND_DEPENDENCY_UPDATES_AVAILABLE"
   	},
   	"status": "ENABLED"
   }
   ```
**注意**  
JSON 文件路径开头必须包含 `file://` 符号。
JSON 文件的路径应遵循运行命令的基本操作系统的相应约定。例如，Windows 使用反斜杠 (\\) 引用目录路径，而 Linux 和 macOS 使用正斜杠 (/)。

1. 

**运行命令创建镜像**

   此示例使用在第一步中创建的配置文件作为`create-image-pipeline`命令的输入。或者，您可以在运行命令时直接为管道指定设置和资源。*有关更多信息，请参阅《参考资料》[中的 “创建映像管道](https://docs.aws.amazon.com/cli/latest/reference/imagebuilder/create-image-pipeline.html)”。AWS CLI *

   ```
   aws imagebuilder create-image-pipeline --cli-input-json file://create-image-pipeline.json
   ```

**示例：使用图像扫描和自定义工作流程创建管道**  
此示例配置了一个管道，该管道每七天检查一次，并且仅在依赖项更新可用时运行。配置文件使用镜像配方和基础架构的现有资源。它支持使用 Amazon Inspector 进行[漏洞扫描](https://docs.aws.amazon.com/imagebuilder/latest/userguide/integ-inspector.html)。该配置还指定了具有执行角色的[自定义工作流程](https://docs.aws.amazon.com/imagebuilder/latest/userguide/manage-image-workflows.html)。并行组同时运行安全测试和功能测试。管道日志将发送到自定义[CloudWatch 日志组](https://docs.aws.amazon.com/imagebuilder/latest/userguide/monitor-cwlogs.html)。

1. 

**创建配置文件**

   创建名为的 JSON 文件`create-pipeline-with-workflows.json`。此文件定义了启用图像扫描和自定义测试工作流程的管道配置。将占位符值替换为您自己的资源 ARN。

   ```
   {
   	"name": "{{MyPipelineWithScanning}}",
   	"description": "{{Pipeline with vulnerability scanning and custom workflows}}",
   	"imageRecipeArn": "arn:aws:imagebuilder:{{us-west-2}}:{{123456789012}}:image-recipe/{{my-recipe}}/1.0.0",
   	"infrastructureConfigurationArn": "arn:aws:imagebuilder:{{us-west-2}}:{{123456789012}}:infrastructure-configuration/{{my-infra-config}}",
   	"distributionConfigurationArn": "arn:aws:imagebuilder:{{us-west-2}}:{{123456789012}}:distribution-configuration/{{my-dist-config}}",
   	"imageScanningConfiguration": {
   		"imageScanningEnabled": true
   	},
   	"workflows": [
   		{
   			"workflowArn": "arn:aws:imagebuilder:{{us-west-2}}:{{123456789012}}:workflow/build/{{my-build-workflow}}/1.0.0"
   		},
   		{
   			"workflowArn": "arn:aws:imagebuilder:{{us-west-2}}:{{123456789012}}:workflow/test/{{my-security-scan}}/1.0.0",
   			"onFailure": "ABORT",
   			"parallelGroup": "security"
   		},
   		{
   			"workflowArn": "arn:aws:imagebuilder:{{us-west-2}}:{{123456789012}}:workflow/test/{{my-compliance-check}}/1.0.0",
   			"onFailure": "ABORT",
   			"parallelGroup": "security"
   		},
   		{
   			"workflowArn": "arn:aws:imagebuilder:{{us-west-2}}:{{123456789012}}:workflow/test/{{my-functional-test}}/1.0.0",
   			"onFailure": "CONTINUE",
   			"parallelGroup": "functional"
   		},
   		{
   			"workflowArn": "arn:aws:imagebuilder:{{us-west-2}}:{{123456789012}}:workflow/test/{{my-performance-test}}/1.0.0",
   			"onFailure": "CONTINUE",
   			"parallelGroup": "functional"
   		}
   	],
   	"executionRole": "arn:aws:iam::{{123456789012}}:role/{{ImageBuilderExecutionRole}}",
   	"loggingConfiguration": {
   		"imageLogGroupName": "/aws/imagebuilder/{{my-pipeline-image-logs}}",
   		"pipelineLogGroupName": "/aws/imagebuilder/{{my-pipeline-execution-logs}}"
   	},
   	"imageTestsConfiguration": {
   		"imageTestsEnabled": true,
   		"timeoutMinutes": 60
   	},
   	"schedule": {
   		"scheduleExpression": "rate(7 days)",
   		"pipelineExecutionStartCondition": "EXPRESSION_MATCH_AND_DEPENDENCY_UPDATES_AVAILABLE"
   	},
   	"status": "ENABLED"
   }
   ```
**注意**  
JSON 文件路径开头必须包含 `file://` 符号。
JSON 文件的路径应遵循运行命令的基本操作系统的相应约定。例如，Windows 使用反斜杠 (\\) 引用目录路径，而 Linux 和 macOS 使用正斜杠 (/)。

1. 

**运行 命令**

   ```
   aws imagebuilder create-image-pipeline --cli-input-json file://create-pipeline-with-workflows.json
   ```