本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
Amazon ECS 已整合,這是一項服務 AWS CloudFormation,您可以使用該服務使用您定義的範本建立 AWS
資源模型和設定資源。如此一來,您可以花更少的時間建立並管理資源和基礎設施。您可以使用 AWS CloudFormation建立範本來描述所需的所有 AWS 資源,例如特定的 Amazon ECS 叢集。然後, AWS CloudFormation 會負責佈建和設定這些資源。
使用時 AWS CloudFormation,您可以重複使用範本,以一致且可重複的方式設定 Amazon ECS 資源。您一次描述您的資源,然後在多 AWS 帳戶 個和之間再次佈建相同的資源 AWS 區域。
AWS CloudFormation 模板
若要佈建和設定 Amazon ECS 和相關服務的資源,請確定您熟悉AWS CloudFormation
範本。 AWS CloudFormation 範本是 JSON 或 YAML 格式的文字檔案,用來描述您要在 AWS CloudFormation 堆疊中佈建的資源。如果您不熟悉 JSON 或 YAML 格式,或兩者都不熟悉,可以使用 AWS CloudFormation 設計師開始使 AWS CloudFormation 用範本。如需詳細資訊,請參閱什麼是 AWS CloudFormation 設計師? 在《AWS CloudFormation 使用者指南》中。
Amazon ECS 支援在 AWS CloudFormation中建立叢集、任務定義、服務和任務集。下列範例示範如何使用 AWS CLI來以範本建立資源。您也可以使用 AWS CloudFormation 主控台建立這些資源。如欲進一步了解如何使用 AWS CloudFormation
主控台建立資源,請參閱 AWS CloudFormation
使用者指南。
範例範本
使用獨立堆疊建立 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
叢集
您可以使用下列範本來建立一個空叢集。
- 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
使用 AWS 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
若要進一步了解 AWS CloudFormation,請參閱下列資源: