本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 AWS CloudFormation 主控台建立 Amazon ECS 資源
搭配 使用 Amazon ECS 的一種方法是 AWS CloudFormation 透過 AWS Management Console。在這裡,您可以為任務定義、叢集和服務等 Amazon ECS 元件建立 AWS CloudFormation 堆疊,並直接從主控台部署它們。下列教學課程說明如何使用 AWS CloudFormation 主控台來建立 Amazon ECS 服務、任務定義和叢集。
先決條件
本教學假設下列先決條件已完成。
-
已完成「設定以使用 Amazon ECS。」中的步驟。
-
您的 IAM 使用者具有 IAM AmazonECS_FullAccess 政策範例中指定的必要許可。
步驟 1:建立堆疊範本
使用下列步驟為 Amazon ECS 服務和其他相關資源建立 AWS CloudFormation 堆疊範本。
-
使用您選擇的文字編輯器,建立名為 的檔案
ecs-tutorial-template.yaml
。 -
在
ecs-tutorial-template.yaml
檔案中,貼上下列範本並儲存變更。AWSTemplateFormatVersion: 2010-09-09 Description: A template that deploys an application that is built on an Apache web server Docker image by creating an Amazon ECS cluster, task definition, and service. The template also creates networking and logging resources, and an Amazon ECS task execution role. Parameters: ClusterName: Type: String Default: CFNCluster Description: Name of the ECS Cluster TaskFamily: Type: String Default: task-definition-cfn Description: Family name for the Task Definition ServiceName: Type: String Default: cfn-service Description: Name of the ECS Service ContainerImage: Type: String Default: public.ecr.aws/docker/library/httpd:2.4 Description: Container image to use for the task TaskCpu: Type: Number Default: 256 AllowedValues: [256, 512, 1024, 2048, 4096] Description: CPU units for the task TaskMemory: Type: Number Default: 512 AllowedValues: [512, 1024, 2048, 4096, 8192, 16384] Description: Memory (in MiB) for the task DesiredCount: Type: Number Default: 1 Description: Desired number of tasks to run LogGroupName: Type: String Default: /ecs/fargate-task-definition Description: CloudWatch Log Group name VpcCidr: Type: String Default: 10.0.0.0/16 Description: CIDR block for the VPC PublicSubnet1Cidr: Type: String Default: 10.0.0.0/24 Description: CIDR block for public subnet 1 PublicSubnet2Cidr: Type: String Default: 10.0.1.0/24 Description: CIDR block for public subnet 2 Resources: # VPC and Networking Resources VPC: Type: AWS::EC2::VPC Properties: CidrBlock: !Ref VpcCidr EnableDnsSupport: true EnableDnsHostnames: true Tags: - Key: Name Value: !Sub ${AWS::StackName}-VPC InternetGateway: Type: AWS::EC2::InternetGateway Properties: Tags: - Key: Name Value: !Sub ${AWS::StackName}-IGW InternetGatewayAttachment: Type: AWS::EC2::VPCGatewayAttachment Properties: InternetGatewayId: !Ref InternetGateway VpcId: !Ref VPC PublicSubnet1: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC AvailabilityZone: !Select [0, !GetAZs ''] CidrBlock: !Ref PublicSubnet1Cidr MapPublicIpOnLaunch: true Tags: - Key: Name Value: !Sub ${AWS::StackName}-PublicSubnet1 PublicSubnet2: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC AvailabilityZone: !Select [1, !GetAZs ''] CidrBlock: !Ref PublicSubnet2Cidr MapPublicIpOnLaunch: true Tags: - Key: Name Value: !Sub ${AWS::StackName}-PublicSubnet2 PublicRouteTable: Type: AWS::EC2::RouteTable Properties: VpcId: !Ref VPC Tags: - Key: Name Value: !Sub ${AWS::StackName}-PublicRouteTable DefaultPublicRoute: Type: AWS::EC2::Route DependsOn: InternetGatewayAttachment Properties: RouteTableId: !Ref PublicRouteTable DestinationCidrBlock: 0.0.0.0/0 GatewayId: !Ref InternetGateway PublicSubnet1RouteTableAssociation: Type: AWS::EC2::SubnetRouteTableAssociation Properties: RouteTableId: !Ref PublicRouteTable SubnetId: !Ref PublicSubnet1 PublicSubnet2RouteTableAssociation: Type: AWS::EC2::SubnetRouteTableAssociation Properties: RouteTableId: !Ref PublicRouteTable SubnetId: !Ref PublicSubnet2 # Security Group ECSSecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: Security group for ECS tasks VpcId: !Ref VPC SecurityGroupIngress: - IpProtocol: tcp FromPort: 80 ToPort: 80 CidrIp: 0.0.0.0/0 - IpProtocol: tcp FromPort: 443 ToPort: 443 CidrIp: 0.0.0.0/0 # IAM Roles ECSTaskExecutionRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: ecs-tasks.amazonaws.com Action: sts:AssumeRole ManagedPolicyArns: - arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy # CloudWatch Logs TaskLogGroup: Type: AWS::Logs::LogGroup DeletionPolicy: Retain UpdateReplacePolicy: Retain Properties: LogGroupName: !Ref LogGroupName RetentionInDays: 30 # ECS Resources ECSCluster: Type: AWS::ECS::Cluster Properties: ClusterName: !Ref ClusterName 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"s EntryPoint: - sh - '-c' Essential: true Image: !Ref ContainerImage LogConfiguration: LogDriver: awslogs Options: mode: non-blocking max-buffer-size: 25m awslogs-create-group: 'true' awslogs-group: !Ref LogGroupName awslogs-region: !Ref 'AWS::Region' awslogs-stream-prefix: ecs Name: sample-fargate-app PortMappings: - ContainerPort: 80 HostPort: 80 Protocol: tcp Cpu: !Ref TaskCpu ExecutionRoleArn: !GetAtt ECSTaskExecutionRole.Arn Family: !Ref TaskFamily Memory: !Ref TaskMemory NetworkMode: awsvpc RequiresCompatibilities: - FARGATE RuntimePlatform: OperatingSystemFamily: LINUX ECSService: Type: AWS::ECS::Service DependsOn: - PublicSubnet1RouteTableAssociation - PublicSubnet2RouteTableAssociation Properties: ServiceName: !Ref ServiceName Cluster: !Ref ECSCluster DesiredCount: !Ref DesiredCount LaunchType: FARGATE NetworkConfiguration: AwsvpcConfiguration: AssignPublicIp: ENABLED SecurityGroups: - !Ref ECSSecurityGroup Subnets: - !Ref PublicSubnet1 - !Ref PublicSubnet2 TaskDefinition: !Ref ECSTaskDefinition Outputs: ClusterName: Description: The name of the ECS cluster Value: !Ref ECSCluster TaskDefinitionArn: Description: The ARN of the task definition Value: !Ref ECSTaskDefinition ServiceName: Description: The name of the ECS service Value: !Ref ECSService VpcId: Description: The ID of the VPC Value: !Ref VPC PublicSubnet1: Description: The ID of public subnet 1 Value: !Ref PublicSubnet1 PublicSubnet2: Description: The ID of public subnet 2 Value: !Ref PublicSubnet2 SecurityGroup: Description: The ID of the security group Value: !Ref ECSSecurityGroup ExecutionRoleArn: Description: The ARN of the task execution role Value: !GetAtt ECSTaskExecutionRole.Arn
步驟 2:建立 Amazon ECS 資源的堆疊
為範本建立檔案後,您可以依照下列步驟,使用 AWS CloudFormation 主控台建立具有範本的堆疊。
登入 AWS Management Console ,並在 https://https://console.aws.amazon.com/cloudformation
開啟 AWS CloudFormation 主控台。 -
在堆疊頁面的右上角,選擇建立堆疊 ,然後使用新資源選擇 (標準)。
-
選擇選擇現有的範本。
-
選擇上傳範本檔案,然後選擇選擇檔案以挑選
ecs-tutorial-template
檔案。檔案上傳至 Amazon S3 儲存貯體後,您可以選擇在 Infrastructure Composer 中檢視,以在 Infrastructure Composer 中視覺化範本。如需 AWS CloudFormation 範本和 Infrastructure Composer 的詳細資訊,請參閱AWS CloudFormation 《 使用者指南》中的使用 Infrastructure Composer 以視覺化方式建立範本。
-
選擇下一步。
-
在指定堆疊詳細資訊頁面的堆疊名稱下,提供堆疊的下列名稱:
ecs-tutorial-stack
。將參數下參數的所有值保留為預設值,然後選擇下一步。 -
在設定堆疊選項頁面的功能下,選取核取方塊以確認 AWS CloudFormation 建立 IAM 資源。建立範本中定義的 Amazon ECS 任務執行角色時需要此確認。將其他設定保留為預設值,然後選擇下一步。
-
檢閱檢閱和建立頁面上的堆疊詳細資訊,然後選擇提交以啟動堆疊建立。
步驟 3:驗證
請使用下列步驟,使用提供的範本來驗證 Amazon ECS 資源的建立。
登入 AWS Management Console 並在 https://https://console.aws.amazon.com/cloudformation
開啟 AWS CloudFormation 主控台。 -
在堆疊頁面上,選擇 ecs-tutorial-stack。
-
選擇事件索引標籤。如果事件狀態顯示
CREATE_IN_PROGRESS
,請等到建立完成且狀態變更為CREATE_COMPLETE
。 -
事件狀態翻轉至 後
CREATE_COMPLETE
,選擇資源索引標籤。您將ECSService
分別看到邏輯 ID 為ECSCluster
、ECSTaskDefinition
和 的資源。 -
若要驗證 Amazon ECS 叢集的建立,請選擇與 相關聯的實體 ID
ECSCluster
。系統會將您重新導向至 Amazon ECS 主控台,您可以在其中看到名為 的建立叢集CFNCluster
。 -
若要驗證 Amazon ECS 服務的建立,請選擇與 相關聯的實體 ID
ECSService
。系統會將您重新導向至 Amazon ECS 主控台,您可以在其中看到叢集 中建立cfn-service
的名為 的服務cfnCluster
。 -
若要驗證 Amazon ECS 任務定義的建立,請選擇與 相關聯的實體 ID
ECSTaskDefinition
。系統會將您重新導向至 Amazon ECS 主控台,您可以在其中看到名稱為 的任務定義修訂task-definition-cfn
。
步驟 4:清除資源
若要清理資源並避免產生進一步的成本,請遵循下列步驟。
登入 AWS Management Console ,並在 https://https://console.aws.amazon.com/cloudformation
開啟 AWS CloudFormation 主控台。 -
在堆疊頁面上,選擇 ecs-tutorial-stack。
-
選擇 刪除。
-
出現確認提示時,請再次選擇刪除。
-
選擇事件索引標籤。刪除或取消註冊資源後,
ecs-tutorial-stack
變更為 DELETE_IN_PROGRESS 和 DELETE_COMPLETE 的狀態。刪除需要幾分鐘的時間。 -
選擇 Resources (資源) 標籤。您現在將看到邏輯 ID 的清單,狀態已更新為 DELETE_COMPLETE。