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

使用 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": { "taskdefinition": { "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": "fargate-task-definition-cfn", "memory":"512", "networkMode": "awsvpc", "requiresCompatibilities": [ "FARGATE" ], "runtimePlatform": { "OperatingSystemFamily": "LINUX" } } } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Resources: taskdefinition: 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: fargate-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 集群。该集群包含 Linux Fargate 任务定义。该模板还会创建一个名为 cfn-service 的服务,其将启动并维护 Fargate 任务定义。在使用此命令之前,请确保服务的 NetworkConfiguration 中的子网和安全组 ID 全部都属于同一 VPC。

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "ECSCluster": { "Type": "AWS::ECS::Cluster", "Properties": { "ClusterName": "CFNCluster" } }, "taskdefinition": { "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": "taskdefinition" } } } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Resources: ECSCluster: Type: 'AWS::ECS::Cluster' Properties: ClusterName: CFNCluster taskdefinition: 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 taskdefinition

使用 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 的更多信息,请参阅以下资源: