使用 EC2 Image Builder 建置自訂的 Amazon ECS 最佳化 AMIs - Amazon Elastic Container Service

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

使用 EC2 Image Builder 建置自訂的 Amazon ECS 最佳化 AMIs

AWS 建議您使用 Amazon ECS 最佳化 AMIs因為它們已預先設定執行容器工作負載的需求和建議。有時您可能需要自訂 AMI 來新增其他軟體。您可以使用 EC2 Image Builder 來建立、管理和部署伺服器映像。您保留帳戶中建立之自訂映像的所有權。您可以使用 EC2 Image Builder 管道來自動化映像的更新和系統修補,或使用獨立命令來建立具有已定義組態資源的映像。

您可以為映像建立配方。配方包含父系映像,以及任何其他元件。您也可以建立分發自訂 AMI 的管道。

您可以為映像建立配方。映像建置器映像配方是一種文件,可定義基礎映像和套用至基礎映像的元件,以產生輸出 AMI 映像所需的組態。您也可以建立分發自訂 AMI 的管道。如需詳細資訊,請參閱 EC2 Image Builder 使用者指南中的 EC2 Image Builder 的運作方式EC2

我們建議您使用下列其中一個 Amazon ECS 最佳化 AMIs 做為 EC2 Image Builder 中的「父映像」:

  • Linux

    • Amazon ECS 最佳化 AL2023 x86

    • Amazon ECS 最佳化 Amazon Linux 2023 (arm64) AMI

    • Amazon ECS 最佳化 Amazon Linux 2 核心 5 AMI

    • Amazon ECS 最佳化 Amazon Linux 2 x86 AMI

  • Windows

    • Amazon ECS 最佳化 Windows 2022 Full x86

    • Amazon ECS 最佳化 Windows 2022 Core x86

    • Amazon ECS 最佳化 Windows 2019 Full x86

    • Amazon ECS 最佳化 Windows 2019 Core x86

    • Amazon ECS 最佳化 Windows 2016 Full x86

我們也建議您選取「使用最新的可用作業系統版本」。管道將為父映像使用語意版本控制,這有助於偵測自動排程任務中的相依性更新。如需詳細資訊,請參閱 EC2 Image Builder 使用者指南中的語意版本控制

AWS 會使用安全性修補程式和新的容器代理程式版本定期更新 Amazon ECS 最佳化 AMI 映像。當您在映像配方中使用 AMI ID 做為父映像時,您需要定期檢查父映像的更新。如果有更新,您必須使用更新的 AMI 建立新的配方版本。這可確保您的自訂映像包含最新版本的父映像。如需如何建立工作流程以使用新建立AMIs 自動更新 Amazon ECS 叢集中的 EC2 執行個體的相關資訊,請參閱如何建立 AMI 強化管道並自動更新 ECS 執行個體機群

您也可以指定透過受管 EC2 Image Builder 管道發佈之父映像的 Amazon Resource Name (ARN)。Amazon 會透過受管管道定期發佈 Amazon ECS 最佳化 AMI 映像。這些映像可公開存取。您必須擁有正確的許可才能存取映像。當您在映像建置器配方中使用映像 ARN 而非 AMI 時,您的管道會在每次執行時自動使用最新版本的父映像。此方法不需要為每個更新手動建立新的配方版本。

使用映像 ARN 搭配基礎設施做為程式碼 (IaC)

您可以使用 EC2 Image Builder 主控台或基礎設施做為程式碼 (例如 AWS CloudFormation) 或 AWS SDK 來設定配方。當您在配方中指定父映像時,您可以指定 EC2 AMI ID、映像建置器映像 ARN、 AWS Marketplace 產品 ID 或容器映像。 AWS 會公開發佈 Amazon ECS 最佳化 AMI 的 AMI IDs 和映像建置器映像 AMIs ARNs。以下是映像的 ARN 格式:

arn:${Partition}:imagebuilder:${Region}:${Account}:image/${ImageName}/${ImageVersion}

ImageVersion 具有下列格式。將主要次要修補程式取代為最新的值。

<major>.<minor>.<patch>

您可以將 majorminor和 取代patch為特定值,也可以使用映像的無版本 ARN,讓您的管道與父映像的最新版本保持up-to-date狀態。無版本 ARN 使用萬用字元格式「x.x.x」來表示映像版本。此方法可讓映像建置器服務自動解析為映像的最新版本。使用無版本 ARN 可確保您的參考一律指向可用的最新映像,簡化在部署中維護最新映像的程序。當您使用主控台建立配方時,EC2 Image Builder 會自動識別父映像的 ARN。當您使用 IaC 建立配方時,您必須識別 ARN 並選取所需的映像版本,或使用無版本 arn 指示最新的可用映像。我們建議您建立自動化指令碼來篩選並僅顯示符合您條件的影像。下列 Python 指令碼說明如何擷取 Amazon ECS 最佳化 AMIs的清單。

指令碼接受兩個選用引數: ownerplatform,預設值分別為「Amazon」和「Windows」。擁有者引數的有效值為:SelfAmazonSharedThirdParty。平台引數的有效值為 WindowsLinux。例如,如果您執行的指令碼引owner數設為 Amazon,而 引數platform設為 Linux,則指令碼會產生 Amazon 發佈的映像清單,包括 Amazon ECS 最佳化映像。

import boto3 import argparse def list_images(owner, platform): # Create a Boto3 session session = boto3.Session() # Create an EC2 Image Builder client client = session.client('imagebuilder') # Define the initial request parameters request_params = { 'owner': owner, 'filters': [ { 'name': 'platform', 'values': [platform] } ] } # Initialize the results list all_images = [] # Get the initial response with the first page of results response = client.list_images(**request_params) # Extract images from the response all_images.extend(response['imageVersionList']) # While 'nextToken' is present, continue paginating while 'nextToken' in response and response['nextToken']: # Update the token for the next request request_params['nextToken'] = response['nextToken'] # Get the next page of results response = client.list_images(**request_params) # Extract images from the response all_images.extend(response['imageVersionList']) return all_images def main(): # Initialize the parser parser = argparse.ArgumentParser(description="List AWS images based on owner and platform") # Add the parameters/arguments parser.add_argument("--owner", default="Amazon", help="The owner of the images. Default is 'Amazon'.") parser.add_argument("--platform", default="Windows", help="The platform type of the images. Default is 'Windows'.") # Parse the arguments args = parser.parse_args() # Retrieve all images based on the provided owner and platform images = list_images(args.owner, args.platform) # Print the details of the images for image in images: print(f"Name: {image['name']}, Version: {image['version']}, ARN: {image['arn']}") if __name__ == "__main__": main()

搭配 使用映像 ARN AWS CloudFormation

映像建置器映像配方是一種藍圖,指定實現輸出映像的預期組態所需的父映像和元件。您可以使用 AWS::ImageBuilder::ImageRecipe 資源。將 ParentImage值設定為影像 ARN。使用所需映像的無版本 ARN,確保您的管道一律使用最新版本的映像。例如 arn:aws:imagebuilder:us-east-1:aws:image/amazon-linux-2023-ecs-optimized-x86/x.x.x。下列AWS::ImageBuilder::ImageRecipe資源定義使用 Amazon 受管映像 ARN:

ECSRecipe: Type: AWS::ImageBuilder::ImageRecipe Properties: Name: MyRecipe Version: '1.0.0' Components: - ComponentArn: [<The component arns of the image recipe>] ParentImage: "arn:aws:imagebuilder:us-east-1:aws:image/amazon-linux-2023-ecs-optimized-x86/x.x.x"

如需 AWS::ImageBuilder::ImageRecipe 資源的詳細資訊,請參閱AWS CloudFormation 《 使用者指南》中的 。

您可以設定 AWS::ImageBuilder::ImagePipeline 資源的 Schedule 屬性,在管道中自動建立新映像。排程包含開始條件和 Cron 表達式。如需詳細資訊,請參閱《AWS CloudFormation 使用者指南》中的 AWS::ImageBuilder::ImagePipeline

下列AWS::ImageBuilder::ImagePipeline範例會讓管道在每天上午 10:00 國際標準時間 (UTC) 執行組建。設定下列Schedule值:

  • PipelineExecutionStartCondition 設定為 EXPRESSION_MATCH_AND_DEPENDENCY_UPDATES_AVAILABLE。只有在父系映像或元件等相依資源在其語意版本中使用萬用字元 ‘x’ 時,組建才會啟動。這可確保建置包含這些資源的最新更新。

  • 將 ScheduleExpression 設定為 Cron 表達式 (0 10 * * ? *)

ECSPipeline: Type: AWS::ImageBuilder::ImagePipeline Properties: Name: my-pipeline ImageRecipeArn: <arn of the recipe you created in previous step> InfrastructureConfigurationArn: <ARN of the infrastructure configuration associated with this image pipeline> Schedule: PipelineExecutionStartCondition: EXPRESSION_MATCH_AND_DEPENDENCY_UPDATES_AVAILABLE ScheduleExpression: 'cron(0 10 * * ? *)'

搭配 Terraform 使用映像 ARN

在 Terraform 中指定管道的父系映像和排程的方法與其相符 AWS CloudFormation。您可以使用 aws_imagebuilder_image_recipe 資源。將 parent_image值設定為影像 ARN。使用所需映像的無版本 ARN,確保您的管道一律使用最新版本的映像。如需詳細資訊,請參閱 Terraform 文件aws_imagebuilder_image_recipe中的 。

在 的排程組態區塊中aws_imagebuilder_image_pipeline resource,將schedule_expression引數值設定為您選擇的 Cron 表達式,以指定管道執行的頻率,並將 pipeline_execution_start_condition 設定為 EXPRESSION_MATCH_AND_DEPENDENCY_UPDATES_AVAILABLE。如需詳細資訊,請參閱 Terraform 文件aws_imagebuilder_image_pipeline中的 。