

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

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

若要從 建立映像管道 AWS CLI，請使用適用於管道的組態選項執行 **create-image-pipeline**命令。您可以選擇建立包含所有管道組態的 JSON 檔案，或在執行時間指定組態。本節使用 JSON 組態檔案方法來簡化命令。

管道建置新映像以納入基礎映像和元件中任何待定更新的頻率取決於`schedule`您設定的 。`schedule` 具有下列屬性：
+ `scheduleExpression` – 設定管道執行時間的排程，以評估 `pipelineExecutionStartCondition` 並判斷是否應啟動建置。排程是以 cron 表達式設定。如需如何在映像建置器中格式化 cron 表達式的詳細資訊，請參閱 [在映像建置器中使用 Cron 表達式](cron-expressions.md)。
+ `pipelineExecutionStartCondition` – 判斷您的管道是否應該啟動建置。有效值包含：
  + `EXPRESSION_MATCH_ONLY` – 每次 Cron 表達式符合目前時間時，您的管道都會建立新的映像。
  + `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`命令的輸入。或者，您可以在執行 命令時直接指定管道的設定和資源。如需詳細資訊，請參閱《 *AWS CLI 參考*》中的 [create-image-pipeline](https://docs.aws.amazon.com/cli/latest/reference/imagebuilder/create-image-pipeline.html)。

   ```
   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`。此檔案會定義已啟用映像掃描和自訂測試工作流程的管道組態。將預留位置值取代為您自己的資源 ARNs。

   ```
   {
   	"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
   ```