使用 Amazon CloudFormation 创建 Amazon ECS 资源 - Amazon Elastic Container Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

使用 Amazon CloudFormation 创建 Amazon ECS 资源

Amazon ECS 与 Amazon CloudFormation 集成,该服务可用于使用您定义的模板对 Amazon 资源进行建模和设置。这样,您可以花费更少的时间来创建和管理您的资源和基础设施。使用 Amazon CloudFormation,您可以创建一个模板来描述您想要的所有 Amazon 资源,例如特定的 Amazon ECS 集群。然后,Amazon CloudFormation 会负责为您预置和配置这些资源。

当您使用 Amazon CloudFormation 时,您可以重复使用您的模板以一致且可重复的方式设置您的 Amazon ECS 资源。您仅描述您的资源一次,然后跨多个 Amazon Web Services 账户 和 Amazon Web Services 区域 再次预置相同的资源。

Amazon ECS 和 Amazon CloudFormation 模板

要为 Amazon ECS 和相关服务预置和配置资源,请确保您熟悉 Amazon CloudFormation 模板。Amazon CloudFormation 模板是 JSON 或者 YAML 格式的文本文件,描述您要在 Amazon CloudFormation 堆栈中预置的资源。如果您不熟悉 JSON 或 YAML 格式,或两者都不熟悉,则可以使用 Amazon CloudFormation Designer 开始使用 Amazon CloudFormation 模板。有关更多信息,请参阅《Amazon CloudFormation 用户指南》中的什么是 Amazon CloudFormation Designer?

Amazon ECS支持在 Amazon CloudFormation 中创建集群、任务定义、服务和任务集。以下示例演示如何使用 Amazon CLI 通过这些模板创建资源。您也可以使用 Amazon CloudFormation 控制台创建这些资源。有关如何使用 Amazon CloudFormation 控制台创建资源的更多信息,请参阅《Amazon CloudFormation 用户指南》。

示例模板

使用单独的堆栈创建 Amazon ECS 资源

以下示例介绍如何通过对每个资源使用单独的堆栈来创建 Amazon ECS 资源。

Amazon ECS 任务定义

您可以使用以下模板来创建 Fargate Linux 任务。

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "ECSTaskDefinition": { "Type": "AWS::ECS::TaskDefinition", "Properties": { "ContainerDefinitions": [ { "Command": [ "/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\"" ], "EntryPoint": [ "sh", "-c" ], "Essential": true, "Image": "httpd:2.4", "LogConfiguration": { "LogDriver": "awslogs", "Options": { "awslogs-group": "/ecs/fargate-task-definition", "awslogs-region": "us-east-1", "awslogs-stream-prefix": "ecs" } }, "Name": "sample-fargate-app", "PortMappings": [ { "ContainerPort": 80, "HostPort": 80, "Protocol": "tcp" } ] } ], "Cpu": 256, "ExecutionRoleArn": "arn:aws:iam::aws_account_id:role/ecsTaskExecutionRole", "Family": "task-definition-cfn", "Memory": 512, "NetworkMode": "awsvpc", "RequiresCompatibilities": [ "FARGATE" ], "RuntimePlatform": { "OperatingSystemFamily": "LINUX" } } } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Resources: ECSTaskDefinition: Type: 'AWS::ECS::TaskDefinition' Properties: ContainerDefinitions: - Command: - >- /bin/sh -c "echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground" EntryPoint: - sh - '-c' Essential: true Image: 'httpd:2.4' LogConfiguration: LogDriver: awslogs Options: awslogs-group: /ecs/fargate-task-definition awslogs-region: us-east-1 awslogs-stream-prefix: ecs Name: sample-fargate-app PortMappings: - ContainerPort: 80 HostPort: 80 Protocol: tcp Cpu: 256 ExecutionRoleArn: 'arn:aws:iam::aws_account_id:role/ecsTaskExecutionRole' Family: task-definition-cfn Memory: 512 NetworkMode: awsvpc RequiresCompatibilities: - FARGATE RuntimePlatform: OperatingSystemFamily: LINUX

Amazon ECS 集群

您可以使用以下模板来创建空集群。

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "ECSCluster": { "Type": "AWS::ECS::Cluster", "Properties": { "ClusterName": "MyEmptyCluster" } } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Resources: ECSCluster: Type: 'AWS::ECS::Cluster' Properties: ClusterName: MyEmptyCluster

在一个堆栈中创建多个 Amazon ECS 资源

您可以使用以下示例模板来在一个堆栈中创建多个 Amazon ECS 资源。该模板将创建名为 CFNCluster 的 Amazon ECS 集群。该集群包含设置 Web 服务器的 Linux Fargate 任务定义。该模板还会创建一个名为 cfn-service 的服务,其将启动并维护任务定义所定义的任务。在使用此模板之前,请确保服务的 NetworkConfiguration 中的子网和安全组 ID 全部都属于同一 VPC,且安全组具有必要的规则。有关安全组规则的更多信息,请参阅《Amazon VPC 用户指南》中的安全组规则

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "ECSCluster": { "Type": "AWS::ECS::Cluster", "Properties": { "ClusterName": "CFNCluster" } }, "ECSTaskDefinition": { "Type": "AWS::ECS::TaskDefinition", "Properties": { "ContainerDefinitions": [ { "Command": [ "/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\"" ], "EntryPoint": [ "sh", "-c" ], "Essential": true, "Image": "httpd:2.4", "LogConfiguration": { "LogDriver": "awslogs", "Options": { "awslogs-group": "/ecs/fargate-task-definition", "awslogs-region": "us-east-1", "awslogs-stream-prefix": "ecs" } }, "Name": "sample-fargate-app", "PortMappings": [ { "ContainerPort": 80, "HostPort": 80, "Protocol": "tcp" } ] } ], "Cpu": 256, "ExecutionRoleArn": "arn:aws:iam::aws_account_id::role/ecsTaskExecutionRole", "Family": "task-definition-cfn", "Memory": 512, "NetworkMode": "awsvpc", "RequiresCompatibilities": [ "FARGATE" ], "RuntimePlatform": { "OperatingSystemFamily": "LINUX" } } }, "ECSService": { "Type": "AWS::ECS::Service", "Properties": { "ServiceName": "cfn-service", "Cluster": { "Ref": "ECSCluster" }, "DesiredCount": 1, "LaunchType": "FARGATE", "NetworkConfiguration": { "AwsvpcConfiguration": { "AssignPublicIp": "ENABLED", "SecurityGroups": [ "sg-abcdef01234567890" ], "Subnets": [ "subnet-abcdef01234567890" ] } }, "TaskDefinition": { "Ref": "ECSTaskDefinition" } } } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Resources: ECSCluster: Type: 'AWS::ECS::Cluster' Properties: ClusterName: CFNCluster ECSTaskDefinition: Type: 'AWS::ECS::TaskDefinition' Properties: ContainerDefinitions: - Command: - >- /bin/sh -c "echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground" EntryPoint: - sh - '-c' Essential: true Image: 'httpd:2.4' LogConfiguration: LogDriver: awslogs Options: awslogs-group: /ecs/fargate-task-definition awslogs-region: us-east-1 awslogs-stream-prefix: ecs Name: sample-fargate-app PortMappings: - ContainerPort: 80 HostPort: 80 Protocol: tcp Cpu: 256 ExecutionRoleArn: 'arn:aws:iam::aws_account_id:role/ecsTaskExecutionRole' Family: task-definition-cfn Memory: 512 NetworkMode: awsvpc RequiresCompatibilities: - FARGATE RuntimePlatform: OperatingSystemFamily: LINUX ECSService: Type: 'AWS::ECS::Service' Properties: ServiceName: cfn-service Cluster: !Ref ECSCluster DesiredCount: 1 LaunchType: FARGATE NetworkConfiguration: AwsvpcConfiguration: AssignPublicIp: ENABLED SecurityGroups: - sg-abcdef01234567890 Subnets: - subnet-abcdef01234567890 TaskDefinition: !Ref ECSTaskDefinition

使用 Amazon CLI 从模板创建资源

以下命令将使用名为 ecs-template-body.json 的模板正文文件创建名为 ecs-stack 的堆栈。确保模板正文文件为 JSON 或 YAML 格式。文件的位置在 --template-body 参数中指定。在这种情况下,模板正文文件位于当前目录中。

aws cloudformation create-stack \ --stack-name ecs-stack \ --template-body file://ecs-template-body.json

要确保正确创建资源,请检查 Amazon ECS 控制台或者使用以下命令:

  • 以下命令将列出所有任务定义。

    aws ecs list-task-definitions
  • 以下命令将列出所有集群。

    aws ecs list-clusters
  • 以下命令将列出集群 CFNCluster 中定义的所有服务。将 CFNCluster 替换为您要在其中创建服务的集群的名称。

    aws ecs list-services \ --cluster CFNCluster

了解有关 Amazon CloudFormation 的更多信息

要了解有关 Amazon CloudFormation 的更多信息,请参阅以下资源: